colos 0.0.7 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/colos.rb +19 -146
  3. data/lib/colos/version.rb +1 -1
  4. metadata +3 -17
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cfa71c2fe49fceab591edb2e9a5b8ceee220c807
4
- data.tar.gz: 808ced25842577be602ba36dd9a49411f40fc521
3
+ metadata.gz: 34e009fe2e98044a817d839b3f7aecb89e070ccc
4
+ data.tar.gz: 9f88ee3d7c39b9912fb3197c1bc43d15221cbc5b
5
5
  SHA512:
6
- metadata.gz: 67b32de676420cb93d9fd5bddc7ab68473c3181f5c3a4de19d8db4d1a901e763e94444aeae8d5d26bcf9feada37f4fc84f7a87c646ce51b549315e4a86318da7
7
- data.tar.gz: 55ffa8a1277e14e6f7cee299f2677fe0f4044923eee775a78a3be533d1c09ad4ef023e245e49cb6b56210a76586cf0b10bce2f277555548e4c7e80666de04e7f
6
+ metadata.gz: a989b20bc7fee993eacd5214e7730779e69bc58c127faca16b75abdc3ae90a555d7af2226d1007dffb88323c2c2e42a1dd73b85d10208eeebd465cf87382e27f
7
+ data.tar.gz: 615a018982cf97480ebfb3d4a3588a58e3c645aebafc9fa544150386e7305295992a5e370184b085a3cd9ce844890bb98d67ae55bf90ad99881c5043cdbda706
data/lib/colos.rb CHANGED
@@ -1,155 +1,28 @@
1
- require "cityhash"
2
-
3
1
  class Colos
4
-
5
- # Options store
6
- attr_reader :options
7
-
8
- # Initializer
9
- #
10
- # Example:
11
- # >> Colos.new frequency: 1.2, randomizr: 12, format: :hex
12
- # => Colos instance
13
- #
14
- # Arguments:
15
- # options: (Hash)
16
- def initialize options = {}
17
- default_options = {
18
- frequency: 1.4,
19
- randomizr: 0,
20
- format: :hex
21
- }
22
-
23
- # merge
24
- @options = default_options.merge options
25
-
26
- check_options
27
- end
28
-
29
- # Options setter
30
- def options= key, value
31
- @options[key] = value
32
-
33
- check_options
34
- end
35
-
36
- # Convert IPv4 to color
37
- #
38
- # Example:
39
- # >> Colos.ip '173.194.32.14'
40
- # => "395e9a"
41
- #
42
- # Arguments:
43
- # str: (String)
44
- def ip str
45
- ip = str.split '.'
46
- hex = ""
47
-
48
- raise "Invalid IP" unless ip.size == 4
49
-
50
- number = ip.permutation(3).to_a[@options[:randomizr]]
51
-
52
- if @options[:format] == :hex
53
- make_color number
54
- else
55
- to_rgb make_color number
56
- end
57
- end
58
-
59
- # Convert IPv4 to colors
60
- #
61
- # Example:
62
- # >> Colos.ips '173.194.32.14'
63
- # => ["adc220", ..., "ee20c2"]
64
- #
65
- # Arguments:
66
- # str: (String)
67
- def ips str
68
- ip = str.split '.'
69
- colors = []
70
-
71
- raise "Invalid IP" unless ip.size == 4
72
-
73
- ip.permutation(3).to_a.each do |number|
74
- colors << make_color(number)
75
- end
76
-
77
- if @options[:format] == :hex
78
- colors
79
- else
80
- colors.map do |hex|
81
- to_rgb hex
2
+ class << self
3
+ # Convert string to color
4
+ #
5
+ # Example:
6
+ # >> Colos.color("test")
7
+ # => "924436"
8
+ #
9
+ # Arguments:
10
+ # str: (String)
11
+ def color str
12
+ hash = 0
13
+
14
+ str.each_byte do |b|
15
+ hash = b + ((hash << 5) - hash)
82
16
  end
83
- end
84
- end
85
17
 
86
- # Convert string to color
87
- #
88
- # Example:
89
- # >> Colos.color("word")
90
- # => "395e9a"
91
- #
92
- # Arguments:
93
- # str: (String)
94
- def color str
95
- hash = CityHash.hash32 str
96
- hex = (hash**(1.0 / @options[:frequency])).to_i.to_s 16
97
-
98
- if @options[:format] == :hex
99
- hex
100
- else
101
- to_rgb hex
102
- end
103
- end
104
-
105
- # Convert string to colors
106
- #
107
- # Example:
108
- # >> Colos.colors("word")
109
- # => ["b7d989", "395e9a", "14e8c0"]
110
- #
111
- # Arguments:
112
- # str: (String)
113
- def colors str
114
- hash = CityHash.hash32 str
115
- colors = []
116
-
117
- for i in (12..16) do
118
- q = (hash**(1.0 / (i / 10.0))).to_i
119
- colors << q.to_s(16) if q.to_s(16).size == 6
120
- end
18
+ color = []
121
19
 
122
- if @options[:format] == :hex
123
- colors
124
- else
125
- colors.map do |hex|
126
- to_rgb hex
20
+ 3.times do |i|
21
+ value = (hash >> (i * 8)) & 0xFF
22
+ color << ('00' + value.to_s(16))[-2..-1]
127
23
  end
128
- end
129
- end
130
-
131
- private
132
-
133
- def check_options
134
- raise "Frequency option should be from 1.2 to 1.6" unless (1.2..1.6).include? @options[:frequency]
135
- raise "Randomizr option should be from 0 to 23" unless (0..23).include? @options[:randomizr]
136
- raise "Format can be hex or rgb" unless [:hex, :rgb].include? @options[:format]
137
- end
138
24
 
139
- def make_color number
140
- hex = ""
141
-
142
- number.map(&:to_i).each do |b|
143
- b = b.to_s 16
144
- b = "0#{b}" unless b.size == 2
145
- hex << b
25
+ color.join
146
26
  end
147
-
148
- hex
149
- end
150
-
151
- def to_rgb hex
152
- hex.match(/(..)(..)(..)/).to_a.map(&:hex)[1..-1]
153
27
  end
154
-
155
28
  end
data/lib/colos/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Colos
2
- VERSION = "0.0.7"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Artemev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-04-18 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: cityhash
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ~>
18
- - !ruby/object:Gem::Version
19
- version: 0.7.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ~>
25
- - !ruby/object:Gem::Version
26
- version: 0.7.0
11
+ date: 2013-06-19 00:00:00.000000000 Z
12
+ dependencies: []
27
13
  description: Convert Ruby string to color
28
14
  email:
29
15
  - i@artemeff.com