colorable 0.0.8 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +98 -27
- data/lib/colorable.rb +1 -1
- data/lib/colorable/color.rb +125 -35
- data/lib/colorable/color_space.rb +260 -0
- data/lib/colorable/colorset.rb +59 -30
- data/lib/colorable/converter.rb +4 -39
- data/lib/colorable/system_extension.rb +6 -4
- data/lib/colorable/version.rb +1 -1
- data/spec/color_space_spec.rb +356 -0
- data/spec/color_spec.rb +220 -47
- data/spec/colorset_spec.rb +49 -44
- data/spec/converter_spec.rb +25 -44
- metadata +5 -2
data/spec/color_spec.rb
CHANGED
@@ -1,100 +1,273 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
include Colorable
|
4
|
+
describe Color do
|
5
5
|
describe ".new" do
|
6
6
|
context "with a string" do
|
7
7
|
context "of valid colorname" do
|
8
|
-
it {
|
9
|
-
it {
|
10
|
-
it {
|
11
|
-
it {
|
8
|
+
it { Color.new("Alice Blue").name.to_s.should eql "Alice Blue" }
|
9
|
+
it { Color.new("Khaki").name.to_s.should eql "Khaki" }
|
10
|
+
it { Color.new("Mint Cream").name.to_s.should eql "Mint Cream" }
|
11
|
+
it { Color.new("Thistle").name.to_s.should eql "Thistle" }
|
12
12
|
end
|
13
13
|
|
14
14
|
context "of valid name variations" do
|
15
|
-
it {
|
16
|
-
it {
|
17
|
-
it {
|
18
|
-
it {
|
19
|
-
it {
|
20
|
-
it {
|
15
|
+
it { Color.new("AliceBlue").name.to_s.should eql "Alice Blue" }
|
16
|
+
it { Color.new("aliceblue").name.to_s.should eql "Alice Blue" }
|
17
|
+
it { Color.new("aliceblue").name.to_s.should eql "Alice Blue" }
|
18
|
+
it { Color.new(:AliceBlue).name.to_s.should eql "Alice Blue" }
|
19
|
+
it { Color.new(:aliceblue).name.to_s.should eql "Alice Blue" }
|
20
|
+
it { Color.new(:alice_blue).name.to_s.should eql "Alice Blue" }
|
21
21
|
end
|
22
22
|
|
23
23
|
context "of invalid name" do
|
24
24
|
it "raise an error" do
|
25
|
-
expect {
|
26
|
-
expect {
|
25
|
+
expect { Color.new("Alice-Blue") }.to raise_error ArgumentError
|
26
|
+
expect { Color.new("Alice") }.to raise_error ArgumentError
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "with a HEX string" do
|
32
|
+
context "of valid HEX" do
|
33
|
+
it { Color.new("#FFFFFF").hex.to_s.should eql "#FFFFFF" }
|
34
|
+
it { Color.new("#00F").hex.to_s.should eql "#0000FF" }
|
35
|
+
it { Color.new("000000").hex.to_s.should eql "#000000" }
|
36
|
+
end
|
37
|
+
|
38
|
+
context "of invalid HEX" do
|
39
|
+
it "raise an error" do
|
40
|
+
expect { Color.new("#FFFFGG") }.to raise_error ArgumentError
|
41
|
+
expect { Color.new("#FFFF") }.to raise_error ArgumentError
|
27
42
|
end
|
28
43
|
end
|
29
44
|
end
|
30
45
|
|
31
46
|
context "with an array" do
|
32
47
|
context "of valid RGB value" do
|
33
|
-
it {
|
34
|
-
it {
|
35
|
-
it {
|
36
|
-
it {
|
48
|
+
it { Color.new([240, 248, 255]).rgb.to_a.should eql [240, 248, 255] }
|
49
|
+
it { Color.new([240, 230, 140]).rgb.to_a.should eql [240, 230, 140] }
|
50
|
+
it { Color.new([245, 255, 250]).rgb.to_a.should eql [245, 255, 250] }
|
51
|
+
it { Color.new([216, 191, 216]).rgb.to_a.should eql [216, 191, 216] }
|
37
52
|
end
|
38
53
|
|
39
54
|
context "of invalid RGB value" do
|
40
55
|
it "raise an error" do
|
41
|
-
expect {
|
56
|
+
expect { Color.new([200, 100, 260]) }.to raise_error ArgumentError
|
42
57
|
end
|
43
58
|
end
|
44
59
|
end
|
60
|
+
|
61
|
+
context "with a RGB or HSB object" do
|
62
|
+
it { Color.new(RGB.new 240, 248, 255).name.to_s.should eql "Alice Blue" }
|
63
|
+
it { Color.new(HSB.new 208, 6, 100).name.to_s.should eql "Alice Blue" }
|
64
|
+
it { Color.new(HEX.new '#F0F8FF').name.to_s.should eql "Alice Blue" }
|
65
|
+
end
|
45
66
|
end
|
46
67
|
|
47
68
|
describe "#hex" do
|
48
|
-
it {
|
49
|
-
it {
|
50
|
-
it {
|
51
|
-
it {
|
69
|
+
it { Color.new("Alice Blue").hex.to_s.should eql "#F0F8FF" }
|
70
|
+
it { Color.new("Khaki").hex.to_s.should eql "#F0E68C" }
|
71
|
+
it { Color.new("Mint Cream").hex.to_s.should eql "#F5FFFA" }
|
72
|
+
it { Color.new("Thistle").hex.to_s.should eql "#D8BFD8" }
|
52
73
|
end
|
53
74
|
|
54
75
|
describe "#hsb" do
|
55
|
-
it {
|
56
|
-
it {
|
57
|
-
it {
|
58
|
-
it {
|
76
|
+
it { Color.new("Alice Blue").hsb.to_a.should eql [208, 6, 100] }
|
77
|
+
it { Color.new("Khaki").hsb.to_a.should eql [55, 42, 94] }
|
78
|
+
it { Color.new("Mint Cream").hsb.to_a.should eql [150, 4, 100] }
|
79
|
+
it { Color.new("Thistle").hsb.to_a.should eql [300, 12, 85] }
|
80
|
+
end
|
81
|
+
|
82
|
+
describe '#red, #green, #blue, #hue, #sat, #bright' do
|
83
|
+
subject { Color.new :alice_blue }
|
84
|
+
its(:red) { should eql 240 }
|
85
|
+
its(:green) { should eql 248 }
|
86
|
+
its(:blue) { should eql 255 }
|
87
|
+
its(:hue) { should eql 208 }
|
88
|
+
its(:sat) { should eql 6 }
|
89
|
+
its(:bright) { should eql 100 }
|
59
90
|
end
|
60
91
|
|
61
92
|
describe "#next" do
|
62
|
-
context "
|
63
|
-
|
64
|
-
|
65
|
-
|
93
|
+
context "with :NAME mode" do
|
94
|
+
subject { Color.new :khaki }
|
95
|
+
it { subject.next.to_s.should eql 'Lavender' }
|
96
|
+
it { subject.next(2).to_s.should eql 'Lavender Blush' }
|
97
|
+
end
|
98
|
+
|
99
|
+
context "with :RGB mode" do
|
100
|
+
subject { Color.new [240, 230, 140] }
|
101
|
+
it { subject.next.to_s.should eql "rgb(240,248,255)" }
|
102
|
+
it { subject.next(2).to_s.should eql "rgb(240,255,240)" }
|
103
|
+
end
|
104
|
+
|
105
|
+
context "with :HSB mode" do
|
106
|
+
before do
|
107
|
+
@c = Color.new :khaki
|
108
|
+
@c.mode = :HSB
|
66
109
|
end
|
110
|
+
it { @c.next.to_s.should eql "hsb(56,43,74)" } #Dark Khaki
|
111
|
+
it { @c.next(2).to_s.should eql "hsb(60,6,100)" } #Ivory
|
67
112
|
end
|
68
113
|
|
69
|
-
context "when
|
70
|
-
it
|
71
|
-
|
72
|
-
|
114
|
+
context "when color is not in X11 colorset" do
|
115
|
+
it { Color.new([100,10,10]).name.should be_nil }
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "#prev" do
|
120
|
+
context "with :NAME mode" do
|
121
|
+
subject { Color.new :khaki }
|
122
|
+
it { subject.prev.to_s.should eql 'Ivory' }
|
123
|
+
it { subject.prev(2).to_s.should eql 'Indigo' }
|
124
|
+
end
|
125
|
+
|
126
|
+
context "with :RGB mode" do
|
127
|
+
subject { Color.new [240, 230, 140] }
|
128
|
+
it { subject.prev.to_s.should eql "rgb(240,128,128)" } #Light Coral
|
129
|
+
it { subject.prev(2).to_s.should eql "rgb(238,232,170)" } #Pale Goldenrod
|
130
|
+
end
|
131
|
+
|
132
|
+
context "with :HSB mode" do
|
133
|
+
before do
|
134
|
+
@c = Color.new :khaki
|
135
|
+
@c.mode = :HSB
|
73
136
|
end
|
137
|
+
it { @c.prev.to_s.should eql "hsb(55,29,93)" } #Pale Goldenrod
|
138
|
+
it { @c.prev(2).to_s.should eql "hsb(55,20,100)" } #Lemon Chiffon
|
74
139
|
end
|
75
140
|
|
76
141
|
context "when color is not in X11 colorset" do
|
77
|
-
it
|
78
|
-
|
79
|
-
|
142
|
+
it { Color.new([100,10,10]).name.should be_nil }
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe "#mode=" do
|
147
|
+
context "init with colorname" do
|
148
|
+
subject { Color.new :black }
|
149
|
+
its(:mode) { should eql :NAME }
|
150
|
+
it { subject.instance_variable_get(:@mode).to_s.should eql "Black"}
|
151
|
+
end
|
152
|
+
|
153
|
+
context "init with rgb values" do
|
154
|
+
subject { Color.new([240, 248, 255]) }
|
155
|
+
its(:mode) { should eql :RGB }
|
156
|
+
it { subject.instance_variable_get(:@mode).to_s.should eql "rgb(240,248,255)"}
|
157
|
+
end
|
158
|
+
|
159
|
+
context "init with HSB class" do
|
160
|
+
subject { Color.new(HSB.new 208, 6, 100) }
|
161
|
+
its(:mode) { should eql :HSB }
|
162
|
+
it { subject.instance_variable_get(:@mode).to_s.should eql "hsb(208,6,100)"}
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
describe "#to_s" do
|
167
|
+
context "default" do
|
168
|
+
it { Color.new(:alice_blue).to_s.should eql "Alice Blue" }
|
169
|
+
end
|
170
|
+
|
171
|
+
context "with name mode" do
|
172
|
+
before do
|
173
|
+
@c = Color.new(:alice_blue)
|
174
|
+
@c.mode = :name
|
80
175
|
end
|
176
|
+
it { @c.mode.should eql :NAME }
|
177
|
+
it { @c.to_s.should eql "Alice Blue" }
|
178
|
+
end
|
179
|
+
|
180
|
+
context "with rgb mode" do
|
181
|
+
before do
|
182
|
+
@c = Color.new(:alice_blue)
|
183
|
+
@c.mode = :rgb
|
184
|
+
end
|
185
|
+
it { @c.mode.should eql :RGB }
|
186
|
+
it { @c.to_s.should eql "rgb(240,248,255)" }
|
187
|
+
end
|
188
|
+
|
189
|
+
context "with hsb mode" do
|
190
|
+
before do
|
191
|
+
@c = Color.new(:alice_blue)
|
192
|
+
@c.mode = :hsb
|
193
|
+
end
|
194
|
+
it { @c.mode.should eql :HSB }
|
195
|
+
it { @c.to_s.should eql "hsb(208,6,100)" }
|
81
196
|
end
|
82
197
|
end
|
83
198
|
|
84
|
-
describe "
|
85
|
-
context "
|
86
|
-
|
87
|
-
|
88
|
-
|
199
|
+
describe "#+" do
|
200
|
+
context "with :NAME mode" do
|
201
|
+
before { @c = Color.new :khaki }
|
202
|
+
it { (@c + 1).to_s.should eql 'Lavender' }
|
203
|
+
it { (@c + 2).to_s.should eql 'Lavender Blush' }
|
204
|
+
end
|
205
|
+
|
206
|
+
context "with rgb mode" do
|
207
|
+
before do
|
208
|
+
@c = Color.new([100, 100, 100])
|
209
|
+
@c.mode = :rgb
|
210
|
+
@c2 = @c + [0, 50, 100]
|
211
|
+
end
|
212
|
+
it { @c2.to_s.should eql "rgb(100,150,200)" }
|
213
|
+
it { @c.to_s.should eql "rgb(100,100,100)" }
|
214
|
+
end
|
215
|
+
|
216
|
+
context "with hsb mode" do
|
217
|
+
before do
|
218
|
+
@c = Color.new(HSB.new(250, 10, 80))
|
219
|
+
@c.mode = :hsb
|
220
|
+
@c2 = @c + [100, 10, 5]
|
221
|
+
end
|
222
|
+
it { @c2.to_s.should eql "hsb(350,20,85)" }
|
223
|
+
it { @c.to_s.should eql "hsb(250,10,80)" }
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
describe "#-" do
|
228
|
+
context "with :NAME mode" do
|
229
|
+
before { @c = Color.new :khaki }
|
230
|
+
it { (@c - 1).to_s.should eql 'Ivory' }
|
231
|
+
it { (@c - 2).to_s.should eql 'Indigo' }
|
232
|
+
end
|
233
|
+
|
234
|
+
context "with rgb mode" do
|
235
|
+
before do
|
236
|
+
@c = Color.new([100, 150, 200])
|
237
|
+
@c.mode = :rgb
|
238
|
+
@c2 = @c - [0, 50, 100]
|
89
239
|
end
|
240
|
+
it { @c.to_s.should eql "rgb(100,150,200)" }
|
241
|
+
it { @c2.to_s.should eql "rgb(100,100,100)" }
|
90
242
|
end
|
91
243
|
|
92
|
-
context "
|
93
|
-
|
94
|
-
@c =
|
95
|
-
@c.
|
244
|
+
context "with hsb mode" do
|
245
|
+
before do
|
246
|
+
@c = Color.new(HSB.new(350, 20, 85))
|
247
|
+
@c.mode = :hsb
|
248
|
+
@c2 = @c - [100, 10, 5]
|
96
249
|
end
|
250
|
+
it { @c.to_s.should eql "hsb(350,20,85)" }
|
251
|
+
it { @c2.to_s.should eql "hsb(250,10,80)" }
|
97
252
|
end
|
98
253
|
end
|
99
254
|
|
255
|
+
describe "#dark?" do
|
256
|
+
it { Color.new(:black).dark?.should eql true }
|
257
|
+
it { Color.new(:yellow).dark?.should eql false }
|
258
|
+
end
|
259
|
+
|
260
|
+
describe "#info" do
|
261
|
+
it 'returns data of the color' do
|
262
|
+
info = {
|
263
|
+
NAME:'Black',
|
264
|
+
RGB:[0, 0, 0],
|
265
|
+
HSB:[0, 0, 0],
|
266
|
+
HEX:'#000000',
|
267
|
+
MODE: :NAME,
|
268
|
+
DARK:true
|
269
|
+
}
|
270
|
+
Color.new(:black).info.should eql info
|
271
|
+
end
|
272
|
+
end
|
100
273
|
end
|
data/spec/colorset_spec.rb
CHANGED
@@ -1,83 +1,88 @@
|
|
1
1
|
require_relative 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
let(:color) { Colorable::Color }
|
3
|
+
include Colorable
|
4
|
+
describe Colorset do
|
6
5
|
describe ".new" do
|
7
|
-
|
8
|
-
|
6
|
+
context "default(NAME order)" do
|
7
|
+
subject { Colorset.new }
|
8
|
+
it { should be_a_instance_of Colorset }
|
9
|
+
it { subject.take(3).map(&:to_s).should eql ["Alice Blue", "Antique White", "Aqua"] }
|
10
|
+
end
|
11
|
+
|
12
|
+
context "with RGB order" do
|
13
|
+
subject { Colorset.new order:'RGB' }
|
14
|
+
it { subject.take(3).map(&:to_s).should eql ['rgb(0,0,0)', 'rgb(0,0,128)', 'rgb(0,0,139)'] }
|
15
|
+
it { subject.last.to_s.should eql 'rgb(255,255,255)' }
|
9
16
|
end
|
10
|
-
end
|
11
17
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
it {
|
16
|
-
it { @cs.last.to_s.should eql 'rgb(255,255,255)' }
|
18
|
+
context "with red order" do
|
19
|
+
subject { Colorset.new order: :red }
|
20
|
+
it { subject.take(3).map(&:to_s).should eql ['rgb(0,0,0)', 'rgb(0,0,128)', 'rgb(0,0,139)'] }
|
21
|
+
it { subject.last.to_s.should eql 'rgb(255,255,255)' }
|
17
22
|
end
|
18
23
|
|
19
|
-
context "
|
20
|
-
|
21
|
-
it {
|
22
|
-
it {
|
24
|
+
context "with green order, reverse direction" do
|
25
|
+
subject { Colorset.new order:'green', dir:'-' }
|
26
|
+
it { subject.take(3).map(&:to_s).should eql ['rgb(255,255,255)', 'rgb(255,255,240)', 'rgb(255,255,224)'] }
|
27
|
+
it { subject.last(3).map(&:to_s).should eql ['rgb(0,0,139)', 'rgb(0,0,128)', 'rgb(0,0,0)'] }
|
23
28
|
end
|
24
29
|
|
25
|
-
context "
|
26
|
-
|
27
|
-
it {
|
28
|
-
it {
|
30
|
+
context "with HSB order" do
|
31
|
+
subject { Colorset.new order: :hsb }
|
32
|
+
it { subject.take(3).map(&:to_s).should eql ['hsb(0,0,0)', 'hsb(0,0,41)', 'hsb(0,0,50)'] }
|
33
|
+
it { subject.last.to_s.should eql 'hsb(352,29,100)' }
|
29
34
|
end
|
30
35
|
end
|
31
36
|
|
32
37
|
describe "#at" do
|
33
|
-
context "
|
34
|
-
|
35
|
-
it {
|
36
|
-
it {
|
38
|
+
context "with NAME order" do
|
39
|
+
subject { Colorset.new }
|
40
|
+
it { subject.at.to_s.should eql 'Alice Blue' }
|
41
|
+
it { subject.at(1).to_s.should eql 'Antique White' }
|
37
42
|
end
|
38
43
|
|
39
|
-
context "
|
40
|
-
|
41
|
-
it {
|
42
|
-
it {
|
44
|
+
context "with RGB order" do
|
45
|
+
subject { Colorset.new order: :green }
|
46
|
+
it { subject.at.to_s.should eql 'rgb(0,0,0)' }
|
47
|
+
it { subject.at(1).to_s.should eql 'rgb(0,0,128)' }
|
43
48
|
end
|
44
49
|
|
45
50
|
context "when argument exceed colorset size" do
|
46
|
-
it "acts like linked list" do
|
47
|
-
|
51
|
+
it "acts like circularly linked list" do
|
52
|
+
Colorset.new.at(144).name.to_s.should eql "Alice Blue"
|
48
53
|
end
|
49
54
|
end
|
50
55
|
end
|
51
56
|
|
52
57
|
describe "#next" do
|
53
|
-
before(:all) { @cs =
|
54
|
-
it { @cs.next.name.should eql 'Antique White' }
|
55
|
-
it { @cs.next.name.should eql 'Aqua' }
|
56
|
-
it { @cs.next(2).name.should eql 'Azure' }
|
58
|
+
before(:all) { @cs = Colorset.new }
|
59
|
+
it { @cs.next.name.to_s.should eql 'Antique White' }
|
60
|
+
it { @cs.next.name.to_s.should eql 'Aqua' }
|
61
|
+
it { @cs.next(2).name.to_s.should eql 'Azure' }
|
57
62
|
end
|
58
63
|
|
59
64
|
describe "#prev" do
|
60
|
-
before(:all) { @cs =
|
61
|
-
it { @cs.prev.name.should eql 'Light Pink' }
|
62
|
-
it { @cs.prev.name.should eql 'Pink' }
|
63
|
-
it { @cs.prev.name.should eql 'Crimson' }
|
64
|
-
it { @cs.prev(2).name.should eql 'Lavender Blush' }
|
65
|
+
before(:all) { @cs = Colorset.new(order: :hsb) }
|
66
|
+
it { @cs.prev.name.to_s.should eql 'Light Pink' }
|
67
|
+
it { @cs.prev.name.to_s.should eql 'Pink' }
|
68
|
+
it { @cs.prev.name.to_s.should eql 'Crimson' }
|
69
|
+
it { @cs.prev(2).name.to_s.should eql 'Lavender Blush' }
|
65
70
|
end
|
66
71
|
|
67
72
|
describe "#rewind" do
|
68
|
-
before(:all) { @cs =
|
69
|
-
it { @cs.next(10); @cs.rewind.name.should eql 'Alice Blue' }
|
73
|
+
before(:all) { @cs = Colorset.new }
|
74
|
+
it { @cs.next(10); @cs.rewind.name.to_s.should eql 'Alice Blue' }
|
70
75
|
end
|
71
76
|
|
72
77
|
describe "#size" do
|
73
78
|
it "returns size of colorset" do
|
74
|
-
|
79
|
+
Colorset.new.size.should eql 144
|
75
80
|
end
|
76
81
|
end
|
77
82
|
|
78
83
|
describe "#to_s" do
|
79
|
-
before(:all) { @cs =
|
80
|
-
it { @cs.to_s.should eql "#<Colorset 0/144 pos='Alice Blue
|
81
|
-
it { @cs.next;@cs.to_s.should eql "#<Colorset 1/144 pos='Antique White
|
84
|
+
before(:all) { @cs = Colorset.new }
|
85
|
+
it { @cs.to_s.should eql "#<Colorset 0/144 pos='Alice Blue/rgb(240,248,255)/hsb(208,6,100)'>"}
|
86
|
+
it { @cs.next;@cs.to_s.should eql "#<Colorset 1/144 pos='Antique White/rgb(250,235,215)/hsb(35,14,98)'>"}
|
82
87
|
end
|
83
88
|
end
|