color_lib 1.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,131 @@
1
+ require 'minitest/autorun'
2
+ require 'test/unit/assertions'
3
+ require 'color_lib'
4
+ require 'color_lib/palette/monocontrast'
5
+
6
+ module TestColorLib
7
+ module TestPalette
8
+ class TestMonoContrast < Minitest::Test
9
+ include Test::Unit::Assertions
10
+ include ColorLib::Palette
11
+
12
+ def setup
13
+ @high = ColorLib::RGB.from_html("#c9e3a6")
14
+ @low = ColorLib::RGB.from_html("#746b8e")
15
+ @mcp1 = MonoContrast.new(@high)
16
+ @mcp2 = MonoContrast.new(@low)
17
+ end
18
+
19
+ def test_background
20
+ assert_equal("#141711", @mcp1.background[-5].html)
21
+ assert_equal("#32392a", @mcp1.background[-4].html)
22
+ assert_equal("#657253", @mcp1.background[-3].html)
23
+ assert_equal("#97aa7d", @mcp1.background[-2].html)
24
+ assert_equal("#abc18d", @mcp1.background[-1].html)
25
+ assert_equal("#c9e3a6", @mcp1.background[0].html)
26
+ assert_equal("#d1e7b3", @mcp1.background[+1].html)
27
+ assert_equal("#d7eabc", @mcp1.background[+2].html) # d7eabd
28
+ assert_equal("#e4f1d3", @mcp1.background[+3].html) # e5f2d3
29
+ assert_equal("#f2f8e9", @mcp1.background[+4].html) # f1f8e9
30
+ assert_equal("#fafcf6", @mcp1.background[+5].html) # fafdf7
31
+
32
+ assert_equal("#0c0b0e", @mcp2.background[-5].html)
33
+ assert_equal("#1d1b24", @mcp2.background[-4].html)
34
+ assert_equal("#3a3647", @mcp2.background[-3].html)
35
+ assert_equal("#57506b", @mcp2.background[-2].html)
36
+ assert_equal("#635b79", @mcp2.background[-1].html)
37
+ assert_equal("#746b8e", @mcp2.background[0].html)
38
+ assert_equal("#89819f", @mcp2.background[+1].html)
39
+ assert_equal("#9790aa", @mcp2.background[+2].html) # 9790ab
40
+ assert_equal("#bab5c7", @mcp2.background[+3].html) # bab6c7
41
+ assert_equal("#dcdae3", @mcp2.background[+4].html)
42
+ assert_equal("#f1f0f4", @mcp2.background[+5].html) # f2f1f4
43
+ end
44
+
45
+ def test_brightness_diff
46
+ bd1 = @mcp1.brightness_diff(@high, @low)
47
+ bd2 = @mcp1.brightness_diff(@low, @high)
48
+ assert_in_delta(bd1, bd2, ColorLib::COLOR_TOLERANCE)
49
+ end
50
+
51
+ def test_calculate_foreground
52
+ assert_equal("#ffffff", @mcp1.calculate_foreground(@low, @high).html)
53
+ assert_equal("#1d1b24", @mcp1.calculate_foreground(@high, @low).html)
54
+ end
55
+
56
+ def test_color_diff
57
+ assert_in_delta(@mcp1.color_diff(@low, @high),
58
+ @mcp1.color_diff(@high, @low),
59
+ ColorLib::COLOR_TOLERANCE)
60
+ end
61
+
62
+ def test_foreground
63
+ assert_equal("#c9e3a6", @mcp1.foreground[-5].html)
64
+ assert_equal("#e4f1d3", @mcp1.foreground[-4].html) # e5f2d3
65
+ assert_equal("#ffffff", @mcp1.foreground[-3].html)
66
+ assert_equal("#000000", @mcp1.foreground[-2].html)
67
+ assert_equal("#000000", @mcp1.foreground[-1].html)
68
+ assert_equal("#000000", @mcp1.foreground[0].html)
69
+ assert_equal("#000000", @mcp1.foreground[+1].html)
70
+ assert_equal("#000000", @mcp1.foreground[+2].html)
71
+ assert_equal("#32392a", @mcp1.foreground[+3].html)
72
+ assert_equal("#32392a", @mcp1.foreground[+4].html)
73
+ assert_equal("#32392a", @mcp1.foreground[+5].html)
74
+
75
+ assert_equal("#bab5c7", @mcp2.foreground[-5].html) # bab6c7
76
+ assert_equal("#dcdae3", @mcp2.foreground[-4].html)
77
+ assert_equal("#ffffff", @mcp2.foreground[-3].html)
78
+ assert_equal("#ffffff", @mcp2.foreground[-2].html)
79
+ assert_equal("#ffffff", @mcp2.foreground[-1].html)
80
+ assert_equal("#ffffff", @mcp2.foreground[0].html)
81
+ assert_equal("#000000", @mcp2.foreground[+1].html)
82
+ assert_equal("#000000", @mcp2.foreground[+2].html)
83
+ assert_equal("#000000", @mcp2.foreground[+3].html)
84
+ assert_equal("#1d1b24", @mcp2.foreground[+4].html)
85
+ assert_equal("#3a3647", @mcp2.foreground[+5].html)
86
+ end
87
+
88
+ def test_minimum_brightness_diff
89
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_BRIGHTNESS_DIFF,
90
+ @mcp1.minimum_brightness_diff, ColorLib::COLOR_TOLERANCE)
91
+ end
92
+
93
+ def test_minimum_brightness_diff_equals
94
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_BRIGHTNESS_DIFF,
95
+ @mcp1.minimum_brightness_diff, ColorLib::COLOR_TOLERANCE)
96
+ mcps = @mcp1.dup
97
+ @mcp1.minimum_brightness_diff = 0.75
98
+ assert_in_delta(0.75, @mcp1.minimum_brightness_diff, ColorLib::COLOR_TOLERANCE)
99
+ assert_not_equal(@mcp1.foreground[-5], mcps.foreground[-5])
100
+ @mcp1.minimum_brightness_diff = 4.0
101
+ assert_in_delta(1, @mcp1.minimum_brightness_diff, ColorLib::COLOR_TOLERANCE)
102
+ @mcp1.minimum_brightness_diff = -4.0
103
+ assert_in_delta(0, @mcp1.minimum_brightness_diff, ColorLib::COLOR_TOLERANCE)
104
+ @mcp1.minimum_brightness_diff = nil
105
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_BRIGHTNESS_DIFF,
106
+ @mcp1.minimum_brightness_diff, ColorLib::COLOR_TOLERANCE)
107
+ end
108
+
109
+ def test_minimum_color_diff
110
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_COLOR_DIFF,
111
+ @mcp1.minimum_color_diff, ColorLib::COLOR_TOLERANCE)
112
+ end
113
+
114
+ def test_minimum_color_diff_equals
115
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_COLOR_DIFF,
116
+ @mcp1.minimum_color_diff, ColorLib::COLOR_TOLERANCE)
117
+ mcps = @mcp1.dup
118
+ @mcp1.minimum_color_diff = 0.75
119
+ assert_in_delta(0.75, @mcp1.minimum_color_diff, ColorLib::COLOR_TOLERANCE)
120
+ assert_not_equal(@mcp1.foreground[-5], mcps.foreground[-5])
121
+ @mcp1.minimum_color_diff = 4.0
122
+ assert_in_delta(3, @mcp1.minimum_color_diff, ColorLib::COLOR_TOLERANCE)
123
+ @mcp1.minimum_color_diff = -4.0
124
+ assert_in_delta(0, @mcp1.minimum_color_diff, ColorLib::COLOR_TOLERANCE)
125
+ @mcp1.minimum_color_diff = nil
126
+ assert_in_delta(MonoContrast::DEFAULT_MINIMUM_COLOR_DIFF,
127
+ @mcp1.minimum_color_diff, ColorLib::COLOR_TOLERANCE)
128
+ end
129
+ end
130
+ end
131
+ end
data/test/test_rgb.rb ADDED
@@ -0,0 +1,333 @@
1
+ require 'minitest/autorun'
2
+ require 'test/unit/assertions'
3
+ require 'color_lib'
4
+
5
+ module TestColorLib
6
+ class TestRGB < Minitest::Test
7
+ include Test::Unit::Assertions
8
+
9
+ def test_adjust_brightness
10
+ assert_equal("#1a1aff", ColorLib::RGB::Blue.adjust_brightness(10).html)
11
+ assert_equal("#0000e6", ColorLib::RGB::Blue.adjust_brightness(-10).html)
12
+ end
13
+
14
+ def test_adjust_hue
15
+ assert_equal("#6600ff", ColorLib::RGB::Blue.adjust_hue(10).html)
16
+ assert_equal("#0066ff", ColorLib::RGB::Blue.adjust_hue(-10).html)
17
+ end
18
+
19
+ def test_adjust_saturation
20
+ assert_equal("#ef9374",
21
+ ColorLib::RGB::DarkSalmon.adjust_saturation(10).html)
22
+ assert_equal("#e39980",
23
+ ColorLib::RGB::DarkSalmon.adjust_saturation(-10).html)
24
+ end
25
+
26
+ def test_red
27
+ red = ColorLib::RGB::Red.dup
28
+ assert_in_delta(1.0, red.r, ColorLib::COLOR_TOLERANCE)
29
+ assert_in_delta(100, red.red_p, ColorLib::COLOR_TOLERANCE)
30
+ assert_in_delta(255, red.red, ColorLib::COLOR_TOLERANCE)
31
+ assert_in_delta(1.0, red.r, ColorLib::COLOR_TOLERANCE)
32
+ red.red_p = 33
33
+ assert_in_delta(0.33, red.r, ColorLib::COLOR_TOLERANCE)
34
+ red.red = 330
35
+ assert_in_delta(1.0, red.r, ColorLib::COLOR_TOLERANCE)
36
+ red.r = -3.3
37
+ assert_in_delta(0.0, red.r, ColorLib::COLOR_TOLERANCE)
38
+ end
39
+
40
+ def test_green
41
+ lime = ColorLib::RGB::Lime.dup
42
+ assert_in_delta(1.0, lime.g, ColorLib::COLOR_TOLERANCE)
43
+ assert_in_delta(100, lime.green_p, ColorLib::COLOR_TOLERANCE)
44
+ assert_in_delta(255, lime.green, ColorLib::COLOR_TOLERANCE)
45
+ lime.green_p = 33
46
+ assert_in_delta(0.33, lime.g, ColorLib::COLOR_TOLERANCE)
47
+ lime.green = 330
48
+ assert_in_delta(1.0, lime.g, ColorLib::COLOR_TOLERANCE)
49
+ lime.g = -3.3
50
+ assert_in_delta(0.0, lime.g, ColorLib::COLOR_TOLERANCE)
51
+ end
52
+
53
+ def test_blue
54
+ blue = ColorLib::RGB::Blue.dup
55
+ assert_in_delta(1.0, blue.b, ColorLib::COLOR_TOLERANCE)
56
+ assert_in_delta(255, blue.blue, ColorLib::COLOR_TOLERANCE)
57
+ assert_in_delta(100, blue.blue_p, ColorLib::COLOR_TOLERANCE)
58
+ blue.blue_p = 33
59
+ assert_in_delta(0.33, blue.b, ColorLib::COLOR_TOLERANCE)
60
+ blue.blue = 330
61
+ assert_in_delta(1.0, blue.b, ColorLib::COLOR_TOLERANCE)
62
+ blue.b = -3.3
63
+ assert_in_delta(0.0, blue.b, ColorLib::COLOR_TOLERANCE)
64
+ end
65
+
66
+ def test_brightness
67
+ assert_in_delta(0.0, ColorLib::RGB::Black.brightness, ColorLib::COLOR_TOLERANCE)
68
+ assert_in_delta(0.5, ColorLib::RGB::Grey50.brightness, ColorLib::COLOR_TOLERANCE)
69
+ assert_in_delta(1.0, ColorLib::RGB::White.brightness, ColorLib::COLOR_TOLERANCE)
70
+ end
71
+
72
+ def test_darken_by
73
+ assert_in_delta(0.5, ColorLib::RGB::Blue.darken_by(50).b,
74
+ ColorLib::COLOR_TOLERANCE)
75
+ end
76
+
77
+ def test_html
78
+ assert_equal("#000000", ColorLib::RGB::Black.html)
79
+ assert_equal(ColorLib::RGB::Black, ColorLib::RGB.from_html("#000000"))
80
+ assert_equal("#0000ff", ColorLib::RGB::Blue.html)
81
+ assert_equal("#00ff00", ColorLib::RGB::Lime.html)
82
+ assert_equal("#ff0000", ColorLib::RGB::Red.html)
83
+ assert_equal("#ffffff", ColorLib::RGB::White.html)
84
+
85
+ assert_equal("rgb(0.00%, 0.00%, 0.00%)", ColorLib::RGB::Black.css_rgb)
86
+ assert_equal("rgb(0.00%, 0.00%, 100.00%)", ColorLib::RGB::Blue.css_rgb)
87
+ assert_equal("rgb(0.00%, 100.00%, 0.00%)", ColorLib::RGB::Lime.css_rgb)
88
+ assert_equal("rgb(100.00%, 0.00%, 0.00%)", ColorLib::RGB::Red.css_rgb)
89
+ assert_equal("rgb(100.00%, 100.00%, 100.00%)", ColorLib::RGB::White.css_rgb)
90
+
91
+ assert_equal("rgba(0.00%, 0.00%, 0.00%, 1.00)", ColorLib::RGB::Black.css_rgba)
92
+ assert_equal("rgba(0.00%, 0.00%, 100.00%, 1.00)", ColorLib::RGB::Blue.css_rgba)
93
+ assert_equal("rgba(0.00%, 100.00%, 0.00%, 1.00)", ColorLib::RGB::Lime.css_rgba)
94
+ assert_equal("rgba(100.00%, 0.00%, 0.00%, 1.00)", ColorLib::RGB::Red.css_rgba)
95
+ assert_equal("rgba(100.00%, 100.00%, 100.00%, 1.00)",
96
+ ColorLib::RGB::White.css_rgba)
97
+ end
98
+
99
+ def test_lighten_by
100
+ assert_in_delta(1.0, ColorLib::RGB::Blue.lighten_by(50).b,
101
+ ColorLib::COLOR_TOLERANCE)
102
+ assert_in_delta(0.5, ColorLib::RGB::Blue.lighten_by(50).r,
103
+ ColorLib::COLOR_TOLERANCE)
104
+ assert_in_delta(0.5, ColorLib::RGB::Blue.lighten_by(50).g,
105
+ ColorLib::COLOR_TOLERANCE)
106
+ end
107
+
108
+ def test_mix_with
109
+ assert_in_delta(0.5, ColorLib::RGB::Red.mix_with(ColorLib::RGB::Blue, 50).r,
110
+ ColorLib::COLOR_TOLERANCE)
111
+ assert_in_delta(0.0, ColorLib::RGB::Red.mix_with(ColorLib::RGB::Blue, 50).g,
112
+ ColorLib::COLOR_TOLERANCE)
113
+ assert_in_delta(0.5, ColorLib::RGB::Red.mix_with(ColorLib::RGB::Blue, 50).b,
114
+ ColorLib::COLOR_TOLERANCE)
115
+ assert_in_delta(0.5, ColorLib::RGB::Blue.mix_with(ColorLib::RGB::Red, 50).r,
116
+ ColorLib::COLOR_TOLERANCE)
117
+ assert_in_delta(0.0, ColorLib::RGB::Blue.mix_with(ColorLib::RGB::Red, 50).g,
118
+ ColorLib::COLOR_TOLERANCE)
119
+ assert_in_delta(0.5, ColorLib::RGB::Blue.mix_with(ColorLib::RGB::Red, 50).b,
120
+ ColorLib::COLOR_TOLERANCE)
121
+ end
122
+
123
+ def test_pdf_fill
124
+ assert_equal("0.000 0.000 0.000 rg", ColorLib::RGB::Black.pdf_fill)
125
+ assert_equal("0.000 0.000 1.000 rg", ColorLib::RGB::Blue.pdf_fill)
126
+ assert_equal("0.000 1.000 0.000 rg", ColorLib::RGB::Lime.pdf_fill)
127
+ assert_equal("1.000 0.000 0.000 rg", ColorLib::RGB::Red.pdf_fill)
128
+ assert_equal("1.000 1.000 1.000 rg", ColorLib::RGB::White.pdf_fill)
129
+ assert_equal("0.000 0.000 0.000 RG", ColorLib::RGB::Black.pdf_stroke)
130
+ assert_equal("0.000 0.000 1.000 RG", ColorLib::RGB::Blue.pdf_stroke)
131
+ assert_equal("0.000 1.000 0.000 RG", ColorLib::RGB::Lime.pdf_stroke)
132
+ assert_equal("1.000 0.000 0.000 RG", ColorLib::RGB::Red.pdf_stroke)
133
+ assert_equal("1.000 1.000 1.000 RG", ColorLib::RGB::White.pdf_stroke)
134
+ end
135
+
136
+ def test_to_cmyk
137
+ assert_kind_of(ColorLib::CMYK, ColorLib::RGB::Black.to_cmyk)
138
+ assert_equal(ColorLib::CMYK.new(0, 0, 0, 100), ColorLib::RGB::Black.to_cmyk)
139
+ assert_equal(ColorLib::CMYK.new(0, 0, 100, 0),
140
+ ColorLib::RGB::Yellow.to_cmyk)
141
+ assert_equal(ColorLib::CMYK.new(100, 0, 0, 0), ColorLib::RGB::Cyan.to_cmyk)
142
+ assert_equal(ColorLib::CMYK.new(0, 100, 0, 0),
143
+ ColorLib::RGB::Magenta.to_cmyk)
144
+ assert_equal(ColorLib::CMYK.new(0, 100, 100, 0), ColorLib::RGB::Red.to_cmyk)
145
+ assert_equal(ColorLib::CMYK.new(100, 0, 100, 0),
146
+ ColorLib::RGB::Lime.to_cmyk)
147
+ assert_equal(ColorLib::CMYK.new(100, 100, 0, 0),
148
+ ColorLib::RGB::Blue.to_cmyk)
149
+ assert_equal(ColorLib::CMYK.new(10.32, 60.52, 10.32, 39.47),
150
+ ColorLib::RGB::Purple.to_cmyk)
151
+ assert_equal(ColorLib::CMYK.new(10.90, 59.13, 59.13, 24.39),
152
+ ColorLib::RGB::Brown.to_cmyk)
153
+ assert_equal(ColorLib::CMYK.new(0, 63.14, 18.43, 0),
154
+ ColorLib::RGB::Carnation.to_cmyk)
155
+ assert_equal(ColorLib::CMYK.new(7.39, 62.69, 62.69, 37.32),
156
+ ColorLib::RGB::Cayenne.to_cmyk)
157
+ end
158
+
159
+ def test_to_grayscale
160
+ assert_kind_of(ColorLib::GrayScale, ColorLib::RGB::Black.to_grayscale)
161
+ assert_equal(ColorLib::GrayScale.from_fraction(0),
162
+ ColorLib::RGB::Black.to_grayscale)
163
+ assert_equal(ColorLib::GrayScale.from_fraction(0.5),
164
+ ColorLib::RGB::Yellow.to_grayscale)
165
+ assert_equal(ColorLib::GrayScale.from_fraction(0.5),
166
+ ColorLib::RGB::Cyan.to_grayscale)
167
+ assert_equal(ColorLib::GrayScale.from_fraction(0.5),
168
+ ColorLib::RGB::Magenta.to_grayscale)
169
+ assert_equal(ColorLib::GrayScale.from_fraction(0.5),
170
+ ColorLib::RGB::Red.to_grayscale)
171
+ assert_equal(ColorLib::GrayScale.from_fraction(0.5),
172
+ ColorLib::RGB::Lime.to_grayscale)
173
+ assert_equal(ColorLib::GrayScale.from_fraction(0.5),
174
+ ColorLib::RGB::Blue.to_grayscale)
175
+ assert_equal(ColorLib::GrayScale.from_fraction(0.2510),
176
+ ColorLib::RGB::Purple.to_grayscale)
177
+ assert_equal(ColorLib::GrayScale.new(40.58),
178
+ ColorLib::RGB::Brown.to_grayscale)
179
+ assert_equal(ColorLib::GrayScale.new(68.43),
180
+ ColorLib::RGB::Carnation.to_grayscale)
181
+ assert_equal(ColorLib::GrayScale.new(27.65),
182
+ ColorLib::RGB::Cayenne.to_grayscale)
183
+ end
184
+
185
+ def test_to_hsl
186
+ assert_kind_of(ColorLib::HSL, ColorLib::RGB::Black.to_hsl)
187
+ assert_equal(ColorLib::HSL.new, ColorLib::RGB::Black.to_hsl)
188
+ assert_equal(ColorLib::HSL.new(60, 100, 50), ColorLib::RGB::Yellow.to_hsl)
189
+ assert_equal(ColorLib::HSL.new(180, 100, 50), ColorLib::RGB::Cyan.to_hsl)
190
+ assert_equal(ColorLib::HSL.new(300, 100, 50), ColorLib::RGB::Magenta.to_hsl)
191
+ assert_equal(ColorLib::HSL.new(0, 100, 50), ColorLib::RGB::Red.to_hsl)
192
+ assert_equal(ColorLib::HSL.new(120, 100, 50), ColorLib::RGB::Lime.to_hsl)
193
+ assert_equal(ColorLib::HSL.new(240, 100, 50), ColorLib::RGB::Blue.to_hsl)
194
+ assert_equal(ColorLib::HSL.new(300, 100, 25.10),
195
+ ColorLib::RGB::Purple.to_hsl)
196
+ assert_equal(ColorLib::HSL.new(0, 59.42, 40.59),
197
+ ColorLib::RGB::Brown.to_hsl)
198
+ assert_equal(ColorLib::HSL.new(317.5, 100, 68.43),
199
+ ColorLib::RGB::Carnation.to_hsl)
200
+ assert_equal(ColorLib::HSL.new(0, 100, 27.64),
201
+ ColorLib::RGB::Cayenne.to_hsl)
202
+
203
+ assert_equal("hsl(0.00, 0.00%, 0.00%)", ColorLib::RGB::Black.css_hsl)
204
+ assert_equal("hsl(60.00, 100.00%, 50.00%)",
205
+ ColorLib::RGB::Yellow.css_hsl)
206
+ assert_equal("hsl(180.00, 100.00%, 50.00%)", ColorLib::RGB::Cyan.css_hsl)
207
+ assert_equal("hsl(300.00, 100.00%, 50.00%)",
208
+ ColorLib::RGB::Magenta.css_hsl)
209
+ assert_equal("hsl(0.00, 100.00%, 50.00%)", ColorLib::RGB::Red.css_hsl)
210
+ assert_equal("hsl(120.00, 100.00%, 50.00%)", ColorLib::RGB::Lime.css_hsl)
211
+ assert_equal("hsl(240.00, 100.00%, 50.00%)", ColorLib::RGB::Blue.css_hsl)
212
+ assert_equal("hsl(300.00, 100.00%, 25.10%)",
213
+ ColorLib::RGB::Purple.css_hsl)
214
+ assert_equal("hsl(0.00, 59.42%, 40.59%)", ColorLib::RGB::Brown.css_hsl)
215
+ assert_equal("hsl(317.52, 100.00%, 68.43%)",
216
+ ColorLib::RGB::Carnation.css_hsl)
217
+ assert_equal("hsl(0.00, 100.00%, 27.65%)", ColorLib::RGB::Cayenne.css_hsl)
218
+
219
+ assert_equal("hsla(0.00, 0.00%, 0.00%, 1.00)",
220
+ ColorLib::RGB::Black.css_hsla)
221
+ assert_equal("hsla(60.00, 100.00%, 50.00%, 1.00)",
222
+ ColorLib::RGB::Yellow.css_hsla)
223
+ assert_equal("hsla(180.00, 100.00%, 50.00%, 1.00)",
224
+ ColorLib::RGB::Cyan.css_hsla)
225
+ assert_equal("hsla(300.00, 100.00%, 50.00%, 1.00)",
226
+ ColorLib::RGB::Magenta.css_hsla)
227
+ assert_equal("hsla(0.00, 100.00%, 50.00%, 1.00)",
228
+ ColorLib::RGB::Red.css_hsla)
229
+ assert_equal("hsla(120.00, 100.00%, 50.00%, 1.00)",
230
+ ColorLib::RGB::Lime.css_hsla)
231
+ assert_equal("hsla(240.00, 100.00%, 50.00%, 1.00)",
232
+ ColorLib::RGB::Blue.css_hsla)
233
+ assert_equal("hsla(300.00, 100.00%, 25.10%, 1.00)",
234
+ ColorLib::RGB::Purple.css_hsla)
235
+ assert_equal("hsla(0.00, 59.42%, 40.59%, 1.00)",
236
+ ColorLib::RGB::Brown.css_hsla)
237
+ assert_equal("hsla(317.52, 100.00%, 68.43%, 1.00)",
238
+ ColorLib::RGB::Carnation.css_hsla)
239
+ assert_equal("hsla(0.00, 100.00%, 27.65%, 1.00)",
240
+ ColorLib::RGB::Cayenne.css_hsla)
241
+
242
+ # The following tests a bug reported by Jean Krohn on 10 June 2006
243
+ # where HSL conversion was not quite correct, resulting in a bad
244
+ # round-trip.
245
+ assert_equal("#008800", ColorLib::RGB.from_html("#008800").to_hsl.html)
246
+ assert_not_equal("#002288", ColorLib::RGB.from_html("#008800").to_hsl.html)
247
+
248
+ # The following tests a bug reported by Adam Johnson on 29 October
249
+ # 2010.
250
+ hsl = ColorLib::HSL.new(262, 67, 42)
251
+ c = ColorLib::RGB.from_fraction(0.34496, 0.1386, 0.701399).to_hsl
252
+ assert_in_delta hsl.h, c.h, ColorLib::COLOR_TOLERANCE, "Hue"
253
+ assert_in_delta hsl.s, c.s, ColorLib::COLOR_TOLERANCE, "Saturation"
254
+ assert_in_delta hsl.l, c.l, ColorLib::COLOR_TOLERANCE, "Luminance"
255
+ end
256
+
257
+ def test_to_rgb
258
+ assert_equal(ColorLib::RGB::Black, ColorLib::RGB::Black.to_rgb)
259
+ end
260
+
261
+ def test_to_yiq
262
+ assert_kind_of(ColorLib::YIQ, ColorLib::RGB::Black.to_yiq)
263
+ assert_equal(ColorLib::YIQ.new, ColorLib::RGB::Black.to_yiq)
264
+ assert_equal(ColorLib::YIQ.new(88.6, 32.1, 0), ColorLib::RGB::Yellow.to_yiq)
265
+ assert_equal(ColorLib::YIQ.new(70.1, 0, 0), ColorLib::RGB::Cyan.to_yiq)
266
+ assert_equal(ColorLib::YIQ.new(41.3, 27.5, 52.3),
267
+ ColorLib::RGB::Magenta.to_yiq)
268
+ assert_equal(ColorLib::YIQ.new(29.9, 59.6, 21.2), ColorLib::RGB::Red.to_yiq)
269
+ assert_equal(ColorLib::YIQ.new(58.7, 0, 0), ColorLib::RGB::Lime.to_yiq)
270
+ assert_equal(ColorLib::YIQ.new(11.4, 0, 31.1), ColorLib::RGB::Blue.to_yiq)
271
+ assert_equal(ColorLib::YIQ.new(20.73, 13.80, 26.25),
272
+ ColorLib::RGB::Purple.to_yiq)
273
+ assert_equal(ColorLib::YIQ.new(30.89, 28.75, 10.23),
274
+ ColorLib::RGB::Brown.to_yiq)
275
+ assert_equal(ColorLib::YIQ.new(60.84, 23.28, 27.29),
276
+ ColorLib::RGB::Carnation.to_yiq)
277
+ assert_equal(ColorLib::YIQ.new(16.53, 32.96, 11.72),
278
+ ColorLib::RGB::Cayenne.to_yiq)
279
+ end
280
+
281
+ def test_add
282
+ ColorLib::RGB::Cyan + ColorLib::RGB::Yellow
283
+ white = ColorLib::RGB::Cyan + ColorLib::RGB::Yellow
284
+ assert_not_nil(white)
285
+ assert_equal(ColorLib::RGB::White, white)
286
+
287
+ c1 = ColorLib::RGB.new(0x80, 0x80, 0x00)
288
+ c2 = ColorLib::RGB.new(0x45, 0x20, 0xf0)
289
+ cr = ColorLib::RGB.new(0xc5, 0xa0, 0xf0)
290
+
291
+ assert_equal(cr, c1 + c2)
292
+ end
293
+
294
+ def test_subtract
295
+ black = ColorLib::RGB::LightCoral - ColorLib::RGB::Honeydew
296
+ assert_equal(ColorLib::RGB::Black, black)
297
+
298
+ c1 = ColorLib::RGB.new(0x85, 0x80, 0x00)
299
+ c2 = ColorLib::RGB.new(0x40, 0x20, 0xf0)
300
+ cr = ColorLib::RGB.new(0x45, 0x60, 0x00)
301
+
302
+ assert_equal(cr, c1 - c2)
303
+ end
304
+
305
+ def test_mean_grayscale
306
+ c1 = ColorLib::RGB.new(0x85, 0x80, 0x00)
307
+ c1_max = c1.max_rgb_as_greyscale
308
+ c1_max = c1.max_rgb_as_greyscale
309
+ c1_result = ColorLib::GrayScale.from_fraction(0x85 / 255.0)
310
+
311
+ assert_equal(c1_result, c1_max)
312
+ end
313
+
314
+ def test_from_html
315
+ assert_equal("RGB [#333333]", ColorLib::RGB.from_html("#333").inspect)
316
+ assert_equal("RGB [#333333]", ColorLib::RGB.from_html("333").inspect)
317
+ assert_equal("RGB [#555555]", ColorLib::RGB.from_html("#555555").inspect)
318
+ assert_equal("RGB [#555555]", ColorLib::RGB.from_html("555555").inspect)
319
+ assert_raises(ArgumentError) { ColorLib::RGB.from_html("#5555555") }
320
+ assert_raises(ArgumentError) { ColorLib::RGB.from_html("5555555") }
321
+ assert_raises(ArgumentError) { ColorLib::RGB.from_html("#55555") }
322
+ assert_raises(ArgumentError) { ColorLib::RGB.from_html("55555") }
323
+ end
324
+
325
+ def test_inspect
326
+ assert_equal("RGB [#000000]", ColorLib::RGB::Black.inspect)
327
+ assert_equal("RGB [#0000ff]", ColorLib::RGB::Blue.inspect)
328
+ assert_equal("RGB [#00ff00]", ColorLib::RGB::Lime.inspect)
329
+ assert_equal("RGB [#ff0000]", ColorLib::RGB::Red.inspect)
330
+ assert_equal("RGB [#ffffff]", ColorLib::RGB::White.inspect)
331
+ end
332
+ end
333
+ end