colos 0.0.6 → 0.0.7

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/version.rb +1 -1
  3. data/lib/colos.rb +52 -18
  4. metadata +20 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22f6c370ab9b0d006b87c6f269974bbd64864773
4
- data.tar.gz: 113de3b14624f5a6653325abadce2f2b0e622d06
3
+ metadata.gz: cfa71c2fe49fceab591edb2e9a5b8ceee220c807
4
+ data.tar.gz: 808ced25842577be602ba36dd9a49411f40fc521
5
5
  SHA512:
6
- metadata.gz: b900e17a5e29766a7bbb973a273fdde5eb4bdc1cbda00c3009cebb64d0705f3acb85105fe01263d46817201c6420d2ee95098b262bee547a75b78ff8d2f4ac3c
7
- data.tar.gz: d713e8000b3758f780e04f8ecc16c6264529f0fec0c5234fced053909a2776af46c52ff9b248ed009b10b5d969f57b440623bc9cc3b3ae0845fd947e714389c3
6
+ metadata.gz: 67b32de676420cb93d9fd5bddc7ab68473c3181f5c3a4de19d8db4d1a901e763e94444aeae8d5d26bcf9feada37f4fc84f7a87c646ce51b549315e4a86318da7
7
+ data.tar.gz: 55ffa8a1277e14e6f7cee299f2677fe0f4044923eee775a78a3be533d1c09ad4ef023e245e49cb6b56210a76586cf0b10bce2f277555548e4c7e80666de04e7f
data/lib/colos/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Colos
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
data/lib/colos.rb CHANGED
@@ -3,7 +3,7 @@ require "cityhash"
3
3
  class Colos
4
4
 
5
5
  # Options store
6
- attr_accessor :options
6
+ attr_reader :options
7
7
 
8
8
  # Initializer
9
9
  #
@@ -15,7 +15,6 @@ class Colos
15
15
  # options: (Hash)
16
16
  def initialize options = {}
17
17
  default_options = {
18
- hash: CityHash,
19
18
  frequency: 1.4,
20
19
  randomizr: 0,
21
20
  format: :hex
@@ -24,11 +23,14 @@ class Colos
24
23
  # merge
25
24
  @options = default_options.merge options
26
25
 
27
- # errors
28
- raise "Frequency option should be from 1.2 to 1.6" unless (1.2..1.6).include? @options[:frequency]
29
- raise "Randomizr option should be from 0 to 23" unless (0..23).include? @options[:randomizr]
30
- raise "Hash class should have `new` method" unless @options[:hash].respond_to?('hash32')
31
- raise "Format can be hex or rgb" unless [:hex, :rgb].include? @options[:format]
26
+ check_options
27
+ end
28
+
29
+ # Options setter
30
+ def options= key, value
31
+ @options[key] = value
32
+
33
+ check_options
32
34
  end
33
35
 
34
36
  # Convert IPv4 to color
@@ -45,9 +47,13 @@ class Colos
45
47
 
46
48
  raise "Invalid IP" unless ip.size == 4
47
49
 
48
- rgb = ip.permutation(3).to_a[@options[:randomizr]]
50
+ number = ip.permutation(3).to_a[@options[:randomizr]]
49
51
 
50
- hex_from_int rgb
52
+ if @options[:format] == :hex
53
+ make_color number
54
+ else
55
+ to_rgb make_color number
56
+ end
51
57
  end
52
58
 
53
59
  # Convert IPv4 to colors
@@ -60,15 +66,21 @@ class Colos
60
66
  # str: (String)
61
67
  def ips str
62
68
  ip = str.split '.'
63
- hexs = []
69
+ colors = []
64
70
 
65
71
  raise "Invalid IP" unless ip.size == 4
66
72
 
67
- ip.permutation(3).to_a.each do |color|
68
- hexs << hex_from_int(color)
73
+ ip.permutation(3).to_a.each do |number|
74
+ colors << make_color(number)
69
75
  end
70
76
 
71
- hexs
77
+ if @options[:format] == :hex
78
+ colors
79
+ else
80
+ colors.map do |hex|
81
+ to_rgb hex
82
+ end
83
+ end
72
84
  end
73
85
 
74
86
  # Convert string to color
@@ -81,7 +93,13 @@ class Colos
81
93
  # str: (String)
82
94
  def color str
83
95
  hash = CityHash.hash32 str
84
- (hash**(1.0 / @options[:frequency])).to_i.to_s 16
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
85
103
  end
86
104
 
87
105
  # Convert string to colors
@@ -101,21 +119,37 @@ class Colos
101
119
  colors << q.to_s(16) if q.to_s(16).size == 6
102
120
  end
103
121
 
104
- colors
122
+ if @options[:format] == :hex
123
+ colors
124
+ else
125
+ colors.map do |hex|
126
+ to_rgb hex
127
+ end
128
+ end
105
129
  end
106
130
 
107
131
  private
108
132
 
109
- def hex_from_int ints
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
+
139
+ def make_color number
110
140
  hex = ""
111
141
 
112
- ints.map(&:to_i).each do |b|
142
+ number.map(&:to_i).each do |b|
113
143
  b = b.to_s 16
114
144
  b = "0#{b}" unless b.size == 2
115
- hex += b
145
+ hex << b
116
146
  end
117
147
 
118
148
  hex
119
149
  end
120
150
 
151
+ def to_rgb hex
152
+ hex.match(/(..)(..)(..)/).to_a.map(&:hex)[1..-1]
153
+ end
154
+
121
155
  end
metadata CHANGED
@@ -1,16 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
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-01-15 00:00:00.000000000 Z
12
- dependencies: []
13
- description: convert ruby string to color
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
27
+ description: Convert Ruby string to color
14
28
  email:
15
29
  - i@artemeff.com
16
30
  executables: []
@@ -38,9 +52,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
38
52
  version: '0'
39
53
  requirements: []
40
54
  rubyforge_project:
41
- rubygems_version: 2.0.0
55
+ rubygems_version: 2.0.3
42
56
  signing_key:
43
57
  specification_version: 4
44
- summary: string to color
58
+ summary: String to color
45
59
  test_files: []
46
60
  has_rdoc: