colorable 0.0.8 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/color_spec.rb CHANGED
@@ -1,100 +1,273 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
- describe Colorable::Color do
4
- let(:color) { Colorable::Color }
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 { color.new("Alice Blue").name.should eql "Alice Blue" }
9
- it { color.new("Khaki").name.should eql "Khaki" }
10
- it { color.new("Mint Cream").name.should eql "Mint Cream" }
11
- it { color.new("Thistle").name.should eql "Thistle" }
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 { color.new("AliceBlue").name.should eql "Alice Blue" }
16
- it { color.new("aliceblue").name.should eql "Alice Blue" }
17
- it { color.new("aliceblue").name.should eql "Alice Blue" }
18
- it { color.new(:AliceBlue).name.should eql "Alice Blue" }
19
- it { color.new(:aliceblue).name.should eql "Alice Blue" }
20
- it { color.new(:alice_blue).name.should eql "Alice Blue" }
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 { color.new("Alice-Blue") }.to raise_error color::ColorNameError
26
- expect { color.new("Alice") }.to raise_error color::ColorNameError
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 { color.new([240, 248, 255]).rgb.should eql [240, 248, 255] }
34
- it { color.new([240, 230, 140]).rgb.should eql [240, 230, 140] }
35
- it { color.new([245, 255, 250]).rgb.should eql [245, 255, 250] }
36
- it { color.new([216, 191, 216]).rgb.should eql [216, 191, 216] }
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 { color.new([200, 100, 260]) }.to raise_error ArgumentError
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 { color.new("Alice Blue").hex.should eql "#F0F8FF" }
49
- it { color.new("Khaki").hex.should eql "#F0E68C" }
50
- it { color.new("Mint Cream").hex.should eql "#F5FFFA" }
51
- it { color.new("Thistle").hex.should eql "#D8BFD8" }
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 { color.new("Alice Blue").hsb.should eql [208, 6, 100] }
56
- it { color.new("Khaki").hsb.should eql [55, 42, 94] }
57
- it { color.new("Mint Cream").hsb.should eql [150, 4, 100] }
58
- it { color.new("Thistle").hsb.should eql [300, 12, 85] }
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 "when no argument" do
63
- it "returns next color in name order" do
64
- @c = color.new("khaki")
65
- @c.next.name.should eql "Lavender"
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 :hsb passed" do
70
- it "returns next color in hsb order" do
71
- @c = color.new("khaki")
72
- @c.next(:hsb).name.should eql "Dark Khaki"
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 "returns nil" do
78
- @c = color.new([100,10,10])
79
- @c.next.should be_nil
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 "#prev" do
85
- context "when no argument" do
86
- it "returns prev color in name order" do
87
- @c = color.new("Alice Blue")
88
- @c.prev.name.should eql "Yellow Green"
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 "when :hsb passed" do
93
- it "returns prev color in hsb order" do
94
- @c = color.new("Yellow")
95
- @c.prev(:hsb).name.should eql "Olive"
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
@@ -1,83 +1,88 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
- describe Colorable::Colorset do
4
- let(:colorset) { Colorable::Colorset }
5
- let(:color) { Colorable::Color }
3
+ include Colorable
4
+ describe Colorset do
6
5
  describe ".new" do
7
- it "returns a colorset object" do
8
- colorset.new.should be_a_instance_of colorset
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
- describe ".[](order)" do
13
- context "when :rgb passed" do
14
- before(:all) { @cs = colorset[:rgb] }
15
- it { @cs.take(3).map(&:to_s).should eql ['rgb(0,0,0)', 'rgb(0,0,128)', 'rgb(0,0,139)'] }
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 "when :red" do
20
- before(:all) { @cs = colorset[:red] }
21
- it { @cs.take(3).map(&:to_s).should eql ['rgb(0,0,0)', 'rgb(0,0,128)', 'rgb(0,0,139)'] }
22
- it { @cs.last.to_s.should eql 'rgb(255,255,255)' }
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 "when :green in descent order" do
26
- before(:all) { @cs = colorset[:green, :-] }
27
- it { @cs.take(3).map(&:to_s).should eql ['rgb(255,255,255)', 'rgb(255,255,240)', 'rgb(255,255,224)'] }
28
- it { @cs.last(3).map(&:to_s).should eql ['rgb(0,0,139)', 'rgb(0,0,128)', 'rgb(0,0,0)'] }
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 "when colorset is ordered RGB" do
34
- before(:all) { @cs = colorset.new }
35
- it { @cs.at.to_s.should eql 'rgb(240,248,255)' }
36
- it { @cs.at(1).to_s.should eql 'rgb(250,235,215)' }
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 "when colorset is ordered RGB" do
40
- before(:all) { @cs = colorset[:green] }
41
- it { @cs.at.to_s.should eql 'rgb(0,0,0)' }
42
- it { @cs.at(1).to_s.should eql 'rgb(0,0,128)' }
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
- colorset.new.at(144).name.should eql "Alice Blue"
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 = colorset.new }
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 = colorset[:hsb] }
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 = colorset.new }
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
- colorset.new.size.should eql 144
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 = colorset.new }
80
- it { @cs.to_s.should eql "#<Colorset 0/144 pos='Alice Blue:rgb(240,248,255)'>"}
81
- it { @cs.next;@cs.to_s.should eql "#<Colorset 1/144 pos='Antique White:rgb(250,235,215)'>"}
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