style_train 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,389 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- ColorType = StyleTrain::ColorType unless defined?( ColorType )
4
- HexColor = StyleTrain::HexColor unless defined?( HexColor )
5
- KeywordColor = StyleTrain::KeywordColor unless defined?( KeywordColor )
6
- RGBcolor = StyleTrain::RGBcolor unless defined?( RGBcolor )
7
-
8
- describe ColorType do
9
- describe 'class methods' do
10
- describe 'percentages' do
11
- it 'should return false if string does not contain a percentage' do
12
- ColorType.percentage( '255' ).should == false
13
- end
14
-
15
- it 'should return the number' do
16
- ColorType.percentage( '33%' ).should == 33
17
- end
18
-
19
- it 'should return decimal values of percentages' do
20
- ColorType.percentage( '33.5%' ).should == 33.5
21
- end
22
-
23
- it 'should raise an argument error if the percentage is out of range' do
24
- lambda { ColorType.percentage( '133%' ) }.should raise_error( ArgumentError )
25
- end
26
- end
27
-
28
- describe 'byte numbers' do
29
- it 'should return the number' do
30
- ColorType.byte( '33' ).should == 33
31
- ColorType.byte( '0' ).should == 0
32
- end
33
-
34
- it 'should raise an argument error if the number is out of range' do
35
- lambda { ColorType.byte( '256' ) }.should raise_error( ArgumentError )
36
- end
37
- end
38
-
39
- describe 'conversion' do
40
- it 'should convert from byte numbers to percentages' do
41
- ColorType.byte_to_percentage( 255 ).should == 100
42
- ColorType.byte_to_percentage( 127 ).should == 50
43
- ColorType.byte_to_percentage( 64 ).should == 25
44
- ColorType.byte_to_percentage( 0 ).should == 0
45
- lambda{ ColorType.byte_to_percentage(256) }.should raise_error( ColorType::ByteNumberError )
46
- lambda{ ColorType.byte_to_percentage(-5) }.should raise_error( ColorType::ByteNumberError )
47
- lambda{ ColorType.byte_to_percentage(122.6) }.should_not raise_error
48
- end
49
-
50
- it 'should convert from percentage to byte' do
51
- ColorType.percent_to_byte( 100 ).should == 255
52
- ColorType.percent_to_byte( 50 ).should == 127
53
- ColorType.percent_to_byte( 25 ).should == 64
54
- ColorType.percent_to_byte( 0 ).should == 0
55
- lambda{ ColorType.percent_to_byte(101) }.should raise_error( ColorType::PercentageError )
56
- lambda{ ColorType.percent_to_byte(-5) }.should raise_error( ColorType::PercentageError )
57
- lambda{ ColorType.percent_to_byte(22.6) }.should_not raise_error
58
- end
59
-
60
- it 'custom argument errors should raise custom messages' do
61
- begin
62
- ColorType.percent_to_byte(101)
63
- rescue Exception => e
64
- e.message.match(/must be between 0 and/)
65
- end
66
-
67
- begin
68
- ColorType.byte_to_percentage(256)
69
- rescue Exception => e
70
- e.message.match(/must be between 0 and/)
71
- end
72
- end
73
- end
74
-
75
- describe '#is_color?' do
76
- it 'should recognize any of the color types' do
77
- ColorType.is_color?(KeywordColor.new(:color => :linen)).should be_true
78
- ColorType.is_color?(RGBcolor.new(:color => [0,0,0])).should be_true
79
- end
80
-
81
- it 'should be false if not a color type' do
82
- ColorType.is_color?( nil ).should be_false
83
- ColorType.is_color?( {} ).should be_false
84
- ColorType.is_color?( [] ).should be_false
85
- end
86
- end
87
-
88
- describe '#make' do
89
- it 'returns nil if not the right type' do
90
- HexColor.make(:color => :lightyellow).should be_nil
91
- end
92
-
93
- it 'returns an instance on the color type if arguments are correct' do
94
- KeywordColor.make(:color => :lightyellow).is_a?(KeywordColor).should be_true
95
- end
96
- end
97
- end
98
-
99
- describe 'instance methods' do
100
- describe 'conversion' do
101
- describe 'from rgb' do
102
- before do
103
- @color = RGBcolor.new(:color => [0,0,0])
104
- end
105
-
106
- it 'to keyword works' do
107
- new_color = @color.to(:keyword)
108
- new_color.class.should == KeywordColor
109
- new_color.keyword.should == :black
110
- end
111
-
112
- it 'to hex works' do
113
- new_color = @color.to(:hex)
114
- new_color.class.should == HexColor
115
- new_color.hex_6.should == 0x000000
116
- end
117
- end
118
-
119
- describe 'from hex' do
120
- before do
121
- @color = HexColor.new(:color => '#000')
122
- end
123
-
124
- it 'to keyword works' do
125
- new_color = @color.to(:keyword)
126
- new_color.class.should == KeywordColor
127
- new_color.keyword.should == :black
128
- end
129
-
130
- it 'to rgb works' do
131
- new_color = @color.to(:rgb)
132
- new_color.class.should == RGBcolor
133
- new_color.red.should == 0
134
- new_color.green.should == 0
135
- new_color.blue.should == 0
136
- end
137
- end
138
-
139
- describe 'from keyword' do
140
- before do
141
- @color = KeywordColor.new(:color => :black)
142
- end
143
-
144
- it 'to rgb works' do
145
- new_color = @color.to(:rgb)
146
- new_color.class.should == RGBcolor
147
- new_color.red.should == 0
148
- new_color.green.should == 0
149
- new_color.blue.should == 0
150
- end
151
-
152
- it 'to hex works' do
153
- new_color = @color.to(:hex)
154
- new_color.class.should == HexColor
155
- new_color.hex_6.should == 0x000000
156
- end
157
- end
158
- end
159
-
160
- describe 'alpha' do
161
- before do
162
- @color = KeywordColor.new(:color => :black)
163
- end
164
-
165
- it 'should be 1.0 by default' do
166
- @color.alpha.should == 1.0
167
- end
168
-
169
- it 'should be 1.0 if alpha is entered as 1' do
170
- @color.alpha = 1
171
- @color.alpha.should == 1.0
172
- end
173
-
174
- it 'should be be 0.0 if set to 0' do
175
- @color.alpha = 0
176
- @color.alpha.should == 0.0
177
- end
178
-
179
- it 'should be a decimal value between zero and one if set that way' do
180
- @color.alpha = 0.5
181
- @color.alpha.should == 0.5
182
- end
183
-
184
- it 'should be set via the initializer' do
185
- color = KeywordColor.new(:color => :black, :alpha => 0.5)
186
- color.alpha.should == 0.5
187
- end
188
-
189
- it 'should raise an error if out of range' do
190
- lambda{ @color.alpha = 1.5 }.should raise_error
191
- end
192
-
193
- describe '#alpha?' do
194
- it '#should be false if alpha value is 1.0' do
195
- @color.alpha?.should == false
196
- end
197
-
198
- it 'should be true if alpha is greater than 0 and less than 1' do
199
- @color.alpha = 0.5
200
- @color.alpha?.should be_true
201
- end
202
- end
203
- end
204
-
205
- describe 'comparisons' do
206
- before do
207
- @hex = HexColor.new(:color => '#000')
208
- @keyword = KeywordColor.new(:color => :black)
209
- end
210
-
211
- describe '=~' do
212
- it 'should be true if the r, g, b values are the same' do
213
- (@hex =~ @keyword).should == true
214
- end
215
-
216
- it 'should be true even if the alpha values are different' do
217
- @hex.alpha = 0.5
218
- (@hex =~ @keyword).should == true
219
- end
220
-
221
- it 'should compare with the delagate of a color' do
222
- (@hex =~ StyleTrain::Color.new('#000')).should == true
223
- (@hex =~ StyleTrain::Color.new('#fff')).should == false
224
- end
225
- end
226
-
227
- describe '==' do
228
- it 'should be true if the colors are =~ and also have the same alpha' do
229
- (@hex == @keyword).should == true
230
- end
231
-
232
- it 'should be false if the alphas are different' do
233
- @hex.alpha = 0.5
234
- (@hex == @keyword).should == false
235
- end
236
-
237
- it 'should be false if the r, g, b values are different' do
238
- color = KeywordColor.new(:color => :maroon)
239
- (color == @keyword).should == false
240
- end
241
- end
242
-
243
- describe '===' do
244
- before do
245
- @key2 = KeywordColor.new(:color => :black)
246
- end
247
-
248
- it 'should be true if the colors are == and also have the same class' do
249
- (@key2 === @keyword).should == true
250
- end
251
-
252
- it 'should be false if the classes are different' do
253
- (@keyword === @hex).should == false
254
- end
255
-
256
- it 'should be false if the alphas are different' do
257
- @key2.alpha = 0.5
258
- (@key2 == @keyword).should == false
259
- end
260
-
261
- it 'should be false if the r, g, b values are different' do
262
- color = KeywordColor.new(:color => :maroon)
263
- (color == @keyword).should == false
264
- end
265
- end
266
- end
267
-
268
- describe 'mixing' do
269
- before do
270
- @rgb = RGBcolor.new(:color => [20,40,60], :alpha => 0.5)
271
- @hex = HexColor.new(:color => '#666')
272
- end
273
-
274
- describe 'averaging (getting a midway point)' do
275
- it 'should average the r, g, and b values' do
276
- color = @rgb.mix(@hex)
277
- color.r.should == ((102+20)/2.0).round
278
- color.g.should == ((102+40)/2.0).round
279
- color.b.should == ((102+60)/2.0).round
280
- end
281
-
282
- it 'should average the alpha' do
283
- color = @rgb.mix(@hex)
284
- color.alpha.should == 0.75
285
- end
286
-
287
- it 'should return the original color type' do
288
- color = @rgb.mix(@hex)
289
- color.class.should == RGBcolor
290
-
291
- color = @hex.mix(@rgb)
292
- color.class.should == HexColor
293
- end
294
- end
295
-
296
- describe 'layering' do
297
- before do
298
- @white = HexColor.new(:color => '#FFF')
299
- @shadow = HexColor.new(:color => '#000', :alpha => 0.5)
300
- end
301
-
302
- it 'should blend on the class method' do
303
- ColorType.blend(255, 0, 0.5).should == 128
304
- ColorType.blend(128, 0, 0.5).should == 64
305
- ColorType.blend(255, 0, 0.25).should == 64
306
- end
307
-
308
- describe 'r, g, b' do
309
- describe 'opaque top layer' do
310
- it 'should have the r, g, b values of the top layer' do
311
- color = @rgb.layer(@hex)
312
- color.r.should == 102
313
- color.g.should == 102
314
- color.b.should == 102
315
- end
316
- end
317
-
318
- describe 'opaque bottom layer' do
319
- before do
320
- @color = @white.layer(@shadow)
321
- end
322
-
323
- it 'should mix the r, g, and b in proportion to the top layer\'s alpha' do
324
- @color.r.should == 128
325
- @color.g.should == 128
326
- @color.b.should == 128
327
-
328
- red = RGBcolor.new(:color => [153,0,0], :alpha => 0.25)
329
- color = @white.layer(red)
330
- color.r.should == (153*0.25 + 255*0.75).round
331
- color.g.should == (0*0.25 + 255*0.75).round
332
- color.b.should == (0*0.25 + 255*0.75).round
333
- end
334
- end
335
- end
336
-
337
- describe 'alpha blending' do
338
- it 'should be 1.0 if bottom layer is opaque' do
339
- color = @white.layer(@shadow)
340
- color.alpha.should == 1.0
341
- end
342
-
343
- it 'should be 1.0 if the top layer is opaque' do
344
- color = @shadow.layer(@white)
345
- color.alpha.should == 1.0
346
- end
347
-
348
- it 'should have an alpha greater than or equal to the composites' do
349
- color = @rgb.layer(@shadow)
350
- (color.alpha >= @rgb.alpha).should be_true
351
- (color.alpha >= @shadow.alpha).should be_true
352
- end
353
-
354
- it 'should calculate the blending of two alphas properly' do
355
- color = @rgb.layer(@shadow)
356
- color.alpha.should == 0.75 # 0.5 for the base color, plus 0.5 of the remaining transparency
357
- end
358
- end
359
- end
360
-
361
- describe 'blending a ratio of one color to the other' do
362
- it 'should mix in the correct percentage r, g, and b values' do
363
- color = @rgb.mix(@hex, 0.25) # only 25% of the @hex
364
- color.r.should == (102*0.25 + 20*0.75).round
365
- color.g.should == (102*0.25 + 40*0.75).round
366
- color.b.should == (102*0.25 + 60*0.75).round
367
- end
368
-
369
- it 'should average the alpha' do
370
- color = @rgb.mix(@hex, 0.25)
371
- color.alpha.should == 0.5*0.75 + 1*0.25
372
- end
373
-
374
- it 'should return the original color type' do
375
- color = @rgb.mix(@hex, 0.25)
376
- color.class.should == RGBcolor
377
-
378
- color = @hex.mix(@rgb, 0.25)
379
- color.class.should == HexColor
380
- end
381
- end
382
- end
383
-
384
- describe 'rendering' do
385
- # todo: do color specs first, to work out where the blending of background should happen
386
- end
387
- end
388
-
389
- end
@@ -1,160 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- HexColor = StyleTrain::HexColor unless defined?( HexColor )
4
-
5
- describe HexColor do
6
- describe 'class methods' do
7
- it 'should expand a 3-digit hex' do
8
- HexColor.expand('333').should == 0x333333
9
- end
10
- end
11
-
12
- describe 'initialization' do
13
- describe 'from another color' do
14
- it 'should convert correctly from a RGB color' do
15
- color = StyleTrain::RGBcolor.new(:color => [20, 40, 60])
16
- hex = HexColor.new(color)
17
- hex.r.should == color.r
18
- hex.g.should == color.g
19
- hex.b.should == color.b
20
- hex.hex.upcase.should == '14283C'
21
- hex.hex_6.should == 0x14283C
22
- end
23
- end
24
-
25
- describe 'from a hex without a hash and possibly a number' do
26
- before do
27
- @gray = HexColor.new(:color => 666)
28
- @red = HexColor.new(:color => '993300')
29
- @gold = HexColor.new(:color => 'FC0')
30
- end
31
-
32
- it 'should set the #hex attribute' do
33
- @gray.hex.should == '666'
34
- @red.hex.should == '993300'
35
- @gold.hex.should == 'FC0'
36
- end
37
-
38
- it 'should set the #hex_6 attribute' do
39
- @gray.hex_6.should == 0x666666
40
- @red.hex_6.should == 0x993300
41
- @gold.hex_6.should == 0xFFCC00
42
- end
43
-
44
- it 'should set the #r attribute' do
45
- @gray.r.should == 102
46
- @red.r.should == 153
47
- @gold.r.should == 255
48
- end
49
-
50
- it 'should set the #g attribute' do
51
- @gray.g.should == 102
52
- @red.g.should == 51
53
- @gold.g.should == 204
54
- end
55
-
56
- it 'should set the #b attribute' do
57
- @gray.b.should == 102
58
- @red.b.should == 0
59
- @gold.b.should == 0
60
- end
61
- end
62
-
63
- describe 'from a three digit hex' do
64
- before do
65
- @gray = HexColor.new(:color =>'#666')
66
- @red = HexColor.new(:color => '#930')
67
- @gold = HexColor.new(:color => '#FC0')
68
- end
69
-
70
- it 'should set the #hex attribute' do
71
- @gray.hex.should == '666'
72
- @red.hex.should == '930'
73
- @gold.hex.should == 'FC0'
74
- end
75
-
76
- it 'should set the #hex_6 attribute' do
77
- @gray.hex_6.should == 0x666666
78
- @red.hex_6.should == 0x993300
79
- @gold.hex_6.should == 0xFFCC00
80
- end
81
-
82
- it 'should set the #r attribute' do
83
- @gray.r.should == 102
84
- @red.r.should == 153
85
- @gold.r.should == 255
86
- end
87
-
88
- it 'should set the #g attribute' do
89
- @gray.g.should == 102
90
- @red.g.should == 51
91
- @gold.g.should == 204
92
- end
93
-
94
- it 'should set the #b attribute' do
95
- @gray.b.should == 102
96
- @red.b.should == 0
97
- @gold.b.should == 0
98
- end
99
- end
100
-
101
- describe 'from a 6 digit hex' do
102
- before do
103
- @gray = HexColor.new(:color => '#666666')
104
- @red = HexColor.new(:color =>'#993300')
105
- @gold = HexColor.new(:color => '#FFCC00')
106
- @mix = HexColor.new(:color => '#6b602f')
107
- end
108
-
109
- it 'should set the #hex attribute' do
110
- @gray.hex.should == '666666'
111
- @red.hex.should == '993300'
112
- @gold.hex.should == 'FFCC00'
113
- @mix.hex.should == '6b602f'
114
- end
115
-
116
- it 'should set the #hex_6 attribute' do
117
- @gray.hex_6.should == 0x666666
118
- @red.hex_6.should == 0x993300
119
- @gold.hex_6.should == 0xFFCC00
120
- @mix.hex_6.should == 0x6b602f
121
- end
122
-
123
- it 'should set the #r attribute' do
124
- @gray.r.should == 102
125
- @red.r.should == 153
126
- @gold.r.should == 255
127
- @mix.r.should == 107
128
- end
129
-
130
- it 'should set the #g attribute' do
131
- @gray.g.should == 102
132
- @red.g.should == 51
133
- @gold.g.should == 204
134
- @mix.g.should == 96
135
- end
136
-
137
- it 'should set the #b attribute' do
138
- @gray.b.should == 102
139
- @red.b.should == 0
140
- @gold.b.should == 0
141
- @mix.b.should == 47
142
- end
143
- end
144
- end
145
-
146
- describe '#to_s' do
147
- before do
148
- @color = HexColor.new(:color => '666')
149
- end
150
-
151
- it 'should render with the original passed in value if no alpha is present' do
152
- @color.to_s.should == "#666"
153
- end
154
-
155
- it 'should render as rgba if alpha is present' do
156
- @color.alpha = 0.5
157
- @color.to_s.should == "rgba( 102, 102, 102, 0.5 )"
158
- end
159
- end
160
- end