color 2.1.1 → 2.2.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +91 -19
- data/CODE_OF_CONDUCT.md +152 -114
- data/CONTRIBUTING.md +88 -49
- data/CONTRIBUTORS.md +7 -2
- data/README.md +20 -9
- data/Rakefile +7 -8
- data/SECURITY.md +12 -21
- data/lib/color/cielab.rb +5 -1
- data/lib/color/cmyk.rb +4 -5
- data/lib/color/grayscale.rb +4 -0
- data/lib/color/hsl.rb +2 -2
- data/lib/color/rgb.rb +25 -6
- data/lib/color/version.rb +1 -1
- data/lib/color/xyz.rb +3 -3
- data/lib/color/yiq.rb +9 -9
- data/test/minitest_helper.rb +8 -0
- data/test/test_cmyk.rb +66 -33
- data/test/test_grayscale.rb +39 -3
- data/test/test_hsl.rb +76 -36
- data/test/test_rgb.rb +142 -98
- data/test/test_yiq.rb +56 -8
- metadata +31 -64
data/test/test_hsl.rb
CHANGED
|
@@ -66,66 +66,106 @@ module TestColor
|
|
|
66
66
|
assert_equal "hsl(145.00deg 20.00% 30.00% / 1.00)", @hsl.css(alpha: 1)
|
|
67
67
|
end
|
|
68
68
|
|
|
69
|
+
def test_mix_with
|
|
70
|
+
red = Color::RGB::Red.to_hsl
|
|
71
|
+
yellow = Color::RGB::Yellow.to_hsl
|
|
72
|
+
assert_in_tolerance 0, red.hue
|
|
73
|
+
assert_in_tolerance 60, yellow.hue
|
|
74
|
+
ry25 = red.mix_with yellow, 0.25
|
|
75
|
+
assert_in_tolerance 15, ry25.hue
|
|
76
|
+
ry50 = red.mix_with yellow, 0.50
|
|
77
|
+
assert_in_tolerance 30, ry50.hue
|
|
78
|
+
ry75 = red.mix_with yellow, 0.75
|
|
79
|
+
assert_in_tolerance 45, ry75.hue
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_inspect
|
|
83
|
+
assert_equal "HSL [145.00deg 20.00% 30.00%]", @hsl.inspect
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def test_pretty_print
|
|
87
|
+
assert_pretty_inspect "HSL\n[145.00deg\n 20.00%\n 30.00%]\n", @hsl
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
class TestHSLConversions < Minitest::Test
|
|
92
|
+
def setup
|
|
93
|
+
@hsl = Color::HSL.from_values(145, 20, 30)
|
|
94
|
+
end
|
|
95
|
+
|
|
69
96
|
def test_to_cmyk
|
|
70
97
|
cmyk = @hsl.to_cmyk
|
|
71
|
-
assert_kind_of
|
|
72
|
-
assert_in_tolerance
|
|
73
|
-
assert_in_tolerance
|
|
74
|
-
assert_in_tolerance
|
|
75
|
-
assert_in_tolerance
|
|
98
|
+
assert_kind_of(Color::CMYK, cmyk)
|
|
99
|
+
assert_in_tolerance(0.3223, cmyk.c)
|
|
100
|
+
assert_in_tolerance(0.2023, cmyk.m)
|
|
101
|
+
assert_in_tolerance(0.2723, cmyk.y)
|
|
102
|
+
assert_in_tolerance(0.4377, cmyk.k)
|
|
103
|
+
assert_equal(Color::CMYK.from_percentage(0, 0, 0, 100), Color::HSL.from_values(0, 0, 0).to_cmyk)
|
|
76
104
|
end
|
|
77
105
|
|
|
78
106
|
def test_to_grayscale
|
|
79
107
|
gs = @hsl.to_grayscale
|
|
80
|
-
assert_kind_of
|
|
81
|
-
assert_in_tolerance
|
|
108
|
+
assert_kind_of(Color::Grayscale, gs)
|
|
109
|
+
assert_in_tolerance(30, gs.gray)
|
|
110
|
+
assert_equal(Color::Grayscale.from_fraction(0), Color::HSL.from_values(0, 0, 0).to_grayscale)
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def test_to_hsl
|
|
114
|
+
assert_equal(@hsl, @hsl.to_hsl)
|
|
115
|
+
assert_kind_of(Color::HSL, @hsl.to_hsl)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def test_to_lab
|
|
119
|
+
lab = @hsl.to_lab
|
|
120
|
+
assert_kind_of(Color::CIELAB, lab)
|
|
121
|
+
assert_in_tolerance(36.19817, lab.l)
|
|
122
|
+
assert_in_tolerance(-15.6, lab.a)
|
|
123
|
+
assert_in_tolerance(6.72317, lab.b)
|
|
124
|
+
assert_equal(Color::CIELAB.from_values(0, 0, 0), Color::HSL.from_values(0, 0, 0).to_lab)
|
|
82
125
|
end
|
|
83
126
|
|
|
84
127
|
def test_to_rgb
|
|
85
128
|
rgb = @hsl.to_rgb
|
|
86
|
-
assert_kind_of
|
|
87
|
-
assert_in_tolerance
|
|
88
|
-
assert_in_tolerance
|
|
89
|
-
assert_in_tolerance
|
|
129
|
+
assert_kind_of(Color::RGB, rgb)
|
|
130
|
+
assert_in_tolerance(0.24, rgb.r)
|
|
131
|
+
assert_in_tolerance(0.36, rgb.g)
|
|
132
|
+
assert_in_tolerance(0.29, rgb.b)
|
|
90
133
|
|
|
91
134
|
# The following tests address a bug reported by Jean Krohn on June 6,
|
|
92
135
|
# 2006 and exercise some previously unexercised code in to_rgb.
|
|
93
|
-
assert_equal
|
|
94
|
-
assert_equal
|
|
95
|
-
assert_equal
|
|
136
|
+
assert_equal(Color::RGB::Black, Color::HSL.from_values(75, 75, 0))
|
|
137
|
+
assert_equal(Color::RGB::White, Color::HSL.from_values(75, 75, 100))
|
|
138
|
+
assert_equal(Color::RGB::Gray80, Color::HSL.from_values(75, 0, 80))
|
|
96
139
|
|
|
97
140
|
# The following tests a bug reported by Adam Johnson on 29 October
|
|
98
141
|
# 2010.
|
|
99
142
|
rgb = Color::RGB.from_fraction(0.34496, 0.1386, 0.701399)
|
|
100
143
|
c = Color::HSL.from_values(262, 67, 42).to_rgb
|
|
101
|
-
assert_in_tolerance
|
|
102
|
-
assert_in_tolerance
|
|
103
|
-
assert_in_tolerance
|
|
144
|
+
assert_in_tolerance(rgb.r, c.r, "Red")
|
|
145
|
+
assert_in_tolerance(rgb.g, c.g, "Green")
|
|
146
|
+
assert_in_tolerance(rgb.b, c.b, "Blue")
|
|
104
147
|
end
|
|
105
148
|
|
|
106
|
-
def
|
|
107
|
-
|
|
108
|
-
assert_kind_of
|
|
109
|
-
assert_in_tolerance
|
|
110
|
-
assert_in_tolerance
|
|
111
|
-
assert_in_tolerance
|
|
149
|
+
def test_to_xyz
|
|
150
|
+
xyz = @hsl.to_xyz
|
|
151
|
+
assert_kind_of(Color::XYZ, xyz)
|
|
152
|
+
assert_in_tolerance(0.069805, xyz.x)
|
|
153
|
+
assert_in_tolerance(0.091115, xyz.y)
|
|
154
|
+
assert_in_tolerance(0.078593, xyz.z)
|
|
155
|
+
assert_equal(Color::XYZ.from_values(0, 0, 0), Color::HSL.from_values(0, 0, 0).to_xyz)
|
|
112
156
|
end
|
|
113
157
|
|
|
114
|
-
def
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
assert_in_tolerance
|
|
118
|
-
assert_in_tolerance
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
ry50 = red.mix_with yellow, 0.50
|
|
122
|
-
assert_in_tolerance 30, ry50.hue
|
|
123
|
-
ry75 = red.mix_with yellow, 0.75
|
|
124
|
-
assert_in_tolerance 45, ry75.hue
|
|
158
|
+
def test_to_yiq
|
|
159
|
+
yiq = @hsl.to_yiq
|
|
160
|
+
assert_kind_of(Color::YIQ, yiq)
|
|
161
|
+
assert_in_tolerance(0.3161, yiq.y)
|
|
162
|
+
assert_in_tolerance(0.0, yiq.i)
|
|
163
|
+
assert_in_tolerance(0.0, yiq.q)
|
|
164
|
+
assert_equal(Color::YIQ.from_values(0, 0, 0), Color::HSL.from_values(0, 0, 0).to_yiq)
|
|
125
165
|
end
|
|
126
166
|
|
|
127
|
-
def
|
|
128
|
-
assert_equal
|
|
167
|
+
def test_to_internal
|
|
168
|
+
assert_equal([0.0, 0.0, 0.0], Color::HSL.from_values(0, 0, 0).to_internal)
|
|
129
169
|
end
|
|
130
170
|
end
|
|
131
171
|
end
|
data/test/test_rgb.rb
CHANGED
|
@@ -91,97 +91,6 @@ module TestColor
|
|
|
91
91
|
assert_in_tolerance(0.5, Color::RGB::Blue.mix_with(Color::RGB::Red, 50).b)
|
|
92
92
|
end
|
|
93
93
|
|
|
94
|
-
def test_to_cmyk
|
|
95
|
-
assert_kind_of(Color::CMYK, Color::RGB::Black.to_cmyk)
|
|
96
|
-
assert_equal(Color::CMYK.from_percentage(0, 0, 0, 100), Color::RGB::Black.to_cmyk)
|
|
97
|
-
assert_equal(Color::CMYK.from_percentage(0, 0, 100, 0), Color::RGB::Yellow.to_cmyk)
|
|
98
|
-
assert_equal(Color::CMYK.from_percentage(100, 0, 0, 0), Color::RGB::Cyan.to_cmyk)
|
|
99
|
-
assert_equal(Color::CMYK.from_percentage(0, 100, 0, 0), Color::RGB::Magenta.to_cmyk)
|
|
100
|
-
assert_equal(Color::CMYK.from_percentage(0, 100, 100, 0), Color::RGB::Red.to_cmyk)
|
|
101
|
-
assert_equal(Color::CMYK.from_percentage(100, 0, 100, 0), Color::RGB::Lime.to_cmyk)
|
|
102
|
-
assert_equal(Color::CMYK.from_percentage(100, 100, 0, 0), Color::RGB::Blue.to_cmyk)
|
|
103
|
-
assert_equal(Color::CMYK.from_percentage(10.32, 60.52, 10.32, 39.47), Color::RGB::Purple.to_cmyk)
|
|
104
|
-
assert_equal(Color::CMYK.from_percentage(10.90, 59.13, 59.13, 24.39), Color::RGB::Brown.to_cmyk)
|
|
105
|
-
assert_equal(Color::CMYK.from_percentage(0, 63.14, 18.43, 0), Color::RGB::Carnation.to_cmyk)
|
|
106
|
-
assert_equal(Color::CMYK.from_percentage(7.39, 62.69, 62.69, 37.32), Color::RGB::Cayenne.to_cmyk)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def test_to_grayscale
|
|
110
|
-
assert_kind_of(Color::Grayscale, Color::RGB::Black.to_grayscale)
|
|
111
|
-
assert_equal(Color::Grayscale.from_fraction(0), Color::RGB::Black.to_grayscale)
|
|
112
|
-
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Yellow.to_grayscale)
|
|
113
|
-
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Cyan.to_grayscale)
|
|
114
|
-
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Magenta.to_grayscale)
|
|
115
|
-
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Red.to_grayscale)
|
|
116
|
-
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Lime.to_grayscale)
|
|
117
|
-
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Blue.to_grayscale)
|
|
118
|
-
assert_equal(Color::Grayscale.from_fraction(0.2510), Color::RGB::Purple.to_grayscale)
|
|
119
|
-
assert_equal(Color::Grayscale.from_percentage(40.58), Color::RGB::Brown.to_grayscale)
|
|
120
|
-
assert_equal(Color::Grayscale.from_percentage(68.43), Color::RGB::Carnation.to_grayscale)
|
|
121
|
-
assert_equal(Color::Grayscale.from_percentage(27.65), Color::RGB::Cayenne.to_grayscale)
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
def test_to_hsl
|
|
125
|
-
assert_kind_of(Color::HSL, Color::RGB::Black.to_hsl)
|
|
126
|
-
assert_equal(Color::HSL.from_values(0, 0, 0), Color::RGB::Black.to_hsl)
|
|
127
|
-
assert_equal(Color::HSL.from_values(60, 100, 50), Color::RGB::Yellow.to_hsl)
|
|
128
|
-
assert_equal(Color::HSL.from_values(180, 100, 50), Color::RGB::Cyan.to_hsl)
|
|
129
|
-
assert_equal(Color::HSL.from_values(300, 100, 50), Color::RGB::Magenta.to_hsl)
|
|
130
|
-
assert_equal(Color::HSL.from_values(0, 100, 50), Color::RGB::Red.to_hsl)
|
|
131
|
-
assert_equal(Color::HSL.from_values(120, 100, 50), Color::RGB::Lime.to_hsl)
|
|
132
|
-
assert_equal(Color::HSL.from_values(240, 100, 50), Color::RGB::Blue.to_hsl)
|
|
133
|
-
assert_equal(Color::HSL.from_values(300, 100, 25.10), Color::RGB::Purple.to_hsl)
|
|
134
|
-
assert_equal(Color::HSL.from_values(0, 59.42, 40.59), Color::RGB::Brown.to_hsl)
|
|
135
|
-
assert_equal(Color::HSL.from_values(317.5, 100, 68.43), Color::RGB::Carnation.to_hsl)
|
|
136
|
-
assert_equal(Color::HSL.from_values(0, 100, 27.64), Color::RGB::Cayenne.to_hsl)
|
|
137
|
-
|
|
138
|
-
# The following tests a bug reported by Jean Krohn on 10 June 2006 where HSL
|
|
139
|
-
# conversion was not quite correct, resulting in a bad round-trip.
|
|
140
|
-
assert_equal("RGB [#008800]", Color::RGB.from_html("#008800").to_hsl.to_rgb.inspect)
|
|
141
|
-
refute_equal("RGB [#002288]", Color::RGB.from_html("#008800").to_hsl.to_rgb.inspect)
|
|
142
|
-
|
|
143
|
-
# The following tests a bug reported by Adam Johnson on 29 October
|
|
144
|
-
# 2010.
|
|
145
|
-
hsl = Color::HSL.from_values(262, 67, 42)
|
|
146
|
-
c = Color::RGB.from_fraction(0.34496, 0.1386, 0.701399).to_hsl
|
|
147
|
-
assert_in_tolerance hsl.h, c.h, "Hue"
|
|
148
|
-
assert_in_tolerance hsl.s, c.s, "Saturation"
|
|
149
|
-
assert_in_tolerance hsl.l, c.l, "Luminance"
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
def test_to_rgb
|
|
153
|
-
assert_same(Color::RGB::Black, Color::RGB::Black.to_rgb)
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
def test_to_yiq
|
|
157
|
-
assert_kind_of(Color::YIQ, Color::RGB::Black.to_yiq)
|
|
158
|
-
assert_equal(Color::YIQ.from_values(0, 0, 0), Color::RGB::Black.to_yiq)
|
|
159
|
-
assert_equal(Color::YIQ.from_values(88.6, 32.1, 0), Color::RGB::Yellow.to_yiq)
|
|
160
|
-
assert_equal(Color::YIQ.from_values(70.1, 0, 0), Color::RGB::Cyan.to_yiq)
|
|
161
|
-
assert_equal(Color::YIQ.from_values(41.3, 27.5, 52.3), Color::RGB::Magenta.to_yiq)
|
|
162
|
-
assert_equal(Color::YIQ.from_values(29.9, 59.6, 21.2), Color::RGB::Red.to_yiq)
|
|
163
|
-
assert_equal(Color::YIQ.from_values(58.7, 0, 0), Color::RGB::Lime.to_yiq)
|
|
164
|
-
assert_equal(Color::YIQ.from_values(11.4, 0, 31.1), Color::RGB::Blue.to_yiq)
|
|
165
|
-
assert_equal(Color::YIQ.from_values(20.73, 13.80, 26.25), Color::RGB::Purple.to_yiq)
|
|
166
|
-
assert_equal(Color::YIQ.from_values(30.89, 28.75, 10.23), Color::RGB::Brown.to_yiq)
|
|
167
|
-
assert_equal(Color::YIQ.from_values(60.84, 23.28, 27.29), Color::RGB::Carnation.to_yiq)
|
|
168
|
-
assert_equal(Color::YIQ.from_values(16.53, 32.96, 11.72), Color::RGB::Cayenne.to_yiq)
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
def test_to_lab
|
|
172
|
-
# Luminosity can be an absolute.
|
|
173
|
-
assert_in_tolerance(0.0, Color::RGB::Black.to_lab.l)
|
|
174
|
-
assert_in_tolerance(100.0, Color::RGB::White.to_lab.l)
|
|
175
|
-
|
|
176
|
-
# It's not really possible to have absolute
|
|
177
|
-
# numbers here because of how L*a*b* works, but
|
|
178
|
-
# negative/positive comparisons work
|
|
179
|
-
assert(Color::RGB::Green.to_lab.a < 0)
|
|
180
|
-
assert(Color::RGB::Magenta.to_lab.a > 0)
|
|
181
|
-
assert(Color::RGB::Blue.to_lab.b < 0)
|
|
182
|
-
assert(Color::RGB::Yellow.to_lab.b > 0)
|
|
183
|
-
end
|
|
184
|
-
|
|
185
94
|
def test_closest_match
|
|
186
95
|
# It should match Blue to Indigo (very simple match)
|
|
187
96
|
match_from = [Color::RGB::Red, Color::RGB::Green, Color::RGB::Blue]
|
|
@@ -230,8 +139,8 @@ module TestColor
|
|
|
230
139
|
def test_by_hex
|
|
231
140
|
assert_same(Color::RGB::Cyan, Color::RGB.by_hex("#0ff"))
|
|
232
141
|
assert_same(Color::RGB::Cyan, Color::RGB.by_hex("#00ffff"))
|
|
233
|
-
assert_equal("RGB [#333333]", Color::RGB.by_hex("#333").inspect)
|
|
234
|
-
assert_equal("RGB [#333333]", Color::RGB.by_hex("333").inspect)
|
|
142
|
+
assert_equal("RGB [#333333] {tungsten}", Color::RGB.by_hex("#333").inspect)
|
|
143
|
+
assert_equal("RGB [#333333] {tungsten}", Color::RGB.by_hex("333").inspect)
|
|
235
144
|
assert_raises(ArgumentError) { Color::RGB.by_hex("5555555") }
|
|
236
145
|
assert_raises(ArgumentError) { Color::RGB.by_hex("#55555") }
|
|
237
146
|
end
|
|
@@ -261,11 +170,41 @@ module TestColor
|
|
|
261
170
|
end
|
|
262
171
|
|
|
263
172
|
def test_inspect
|
|
264
|
-
assert_equal("RGB [#000000]", Color::RGB::Black.inspect)
|
|
265
|
-
assert_equal("RGB [#0000ff]", Color::RGB::Blue.inspect)
|
|
266
|
-
assert_equal("RGB [#00ff00]", Color::RGB::Lime.inspect)
|
|
267
|
-
assert_equal("RGB [#ff0000]", Color::RGB::Red.inspect)
|
|
268
|
-
assert_equal("RGB [#ffffff]", Color::RGB::White.inspect)
|
|
173
|
+
assert_equal("RGB [#000000] {black}", Color::RGB::Black.inspect)
|
|
174
|
+
assert_equal("RGB [#0000ff] {blue}", Color::RGB::Blue.inspect)
|
|
175
|
+
assert_equal("RGB [#00ff00] {lime}", Color::RGB::Lime.inspect)
|
|
176
|
+
assert_equal("RGB [#ff0000] {red}", Color::RGB::Red.inspect)
|
|
177
|
+
assert_equal("RGB [#ffffff] {white}", Color::RGB::White.inspect)
|
|
178
|
+
assert_equal("RGB [#708090] {slategray slategrey}", Color::RGB::SlateGrey.inspect)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def test_pretty_print
|
|
182
|
+
assert_pretty_inspect("RGB\n[#000000]\n{black}\n", Color::RGB::Black)
|
|
183
|
+
assert_pretty_inspect("RGB\n[#0000ff]\n{blue}\n", Color::RGB::Blue)
|
|
184
|
+
assert_pretty_inspect("RGB\n[#00ff00]\n{lime}\n", Color::RGB::Lime)
|
|
185
|
+
assert_pretty_inspect("RGB\n[#ff0000]\n{red}\n", Color::RGB::Red)
|
|
186
|
+
assert_pretty_inspect("RGB\n[#ffffff]\n{white}\n", Color::RGB::White)
|
|
187
|
+
assert_pretty_inspect("RGB\n[#708090]\n{slategray\n slategrey}\n", Color::RGB::SlateGrey)
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def test_name
|
|
191
|
+
assert_equal("black", Color::RGB::Black.name)
|
|
192
|
+
assert_equal("blue", Color::RGB::Blue.name)
|
|
193
|
+
assert_equal("lime", Color::RGB::Lime.name)
|
|
194
|
+
assert_equal("red", Color::RGB::Red.name)
|
|
195
|
+
assert_equal("white", Color::RGB::White.name)
|
|
196
|
+
|
|
197
|
+
assert_equal("black", Color::RGB.from_values(0, 0, 0).name)
|
|
198
|
+
assert_equal("blue", Color::RGB.from_values(0, 0, 0xff).name)
|
|
199
|
+
assert_equal("lime", Color::RGB.from_values(0, 0xff, 0).name)
|
|
200
|
+
assert_equal("red", Color::RGB.from_values(0xff, 0, 0).name)
|
|
201
|
+
assert_equal("white", Color::RGB.from_values(0xff, 0xff, 0xff).name)
|
|
202
|
+
|
|
203
|
+
assert_equal("000", Color::RGB.from_values(0, 0, 0, ["000"]).name)
|
|
204
|
+
assert_equal("00f", Color::RGB.from_values(0, 0, 0xff, ["00f"]).name)
|
|
205
|
+
assert_equal("0f0", Color::RGB.from_values(0, 0xff, 0, ["0f0"]).name)
|
|
206
|
+
assert_equal("f00", Color::RGB.from_values(0xff, 0, 0, ["f00"]).name)
|
|
207
|
+
assert_equal("fff", Color::RGB.from_values(0xff, 0xff, 0xff, ["fff"]).name)
|
|
269
208
|
end
|
|
270
209
|
|
|
271
210
|
def test_delta_e2000
|
|
@@ -325,6 +264,80 @@ module TestColor
|
|
|
325
264
|
|
|
326
265
|
assert_equal("#ff00aa", Color::RGB.by_hex("#ff00aa").to_lab.to_rgb.html)
|
|
327
266
|
end
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
class TestRGBConversions < Minitest::Test
|
|
270
|
+
def test_to_cmyk
|
|
271
|
+
assert_kind_of(Color::CMYK, Color::RGB::Black.to_cmyk)
|
|
272
|
+
assert_equal(Color::CMYK.from_percentage(0, 0, 0, 100), Color::RGB::Black.to_cmyk)
|
|
273
|
+
assert_equal(Color::CMYK.from_percentage(0, 0, 100, 0), Color::RGB::Yellow.to_cmyk)
|
|
274
|
+
assert_equal(Color::CMYK.from_percentage(100, 0, 0, 0), Color::RGB::Cyan.to_cmyk)
|
|
275
|
+
assert_equal(Color::CMYK.from_percentage(0, 100, 0, 0), Color::RGB::Magenta.to_cmyk)
|
|
276
|
+
assert_equal(Color::CMYK.from_percentage(0, 100, 100, 0), Color::RGB::Red.to_cmyk)
|
|
277
|
+
assert_equal(Color::CMYK.from_percentage(100, 0, 100, 0), Color::RGB::Lime.to_cmyk)
|
|
278
|
+
assert_equal(Color::CMYK.from_percentage(100, 100, 0, 0), Color::RGB::Blue.to_cmyk)
|
|
279
|
+
assert_equal(Color::CMYK.from_percentage(10.32, 60.52, 10.32, 39.47), Color::RGB::Purple.to_cmyk)
|
|
280
|
+
assert_equal(Color::CMYK.from_percentage(10.90, 59.13, 59.13, 24.39), Color::RGB::Brown.to_cmyk)
|
|
281
|
+
assert_equal(Color::CMYK.from_percentage(0, 63.14, 18.43, 0), Color::RGB::Carnation.to_cmyk)
|
|
282
|
+
assert_equal(Color::CMYK.from_percentage(7.39, 62.69, 62.69, 37.32), Color::RGB::Cayenne.to_cmyk)
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
def test_to_grayscale
|
|
286
|
+
assert_kind_of(Color::Grayscale, Color::RGB::Black.to_grayscale)
|
|
287
|
+
assert_equal(Color::Grayscale.from_fraction(0), Color::RGB::Black.to_grayscale)
|
|
288
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Yellow.to_grayscale)
|
|
289
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Cyan.to_grayscale)
|
|
290
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Magenta.to_grayscale)
|
|
291
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Red.to_grayscale)
|
|
292
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Lime.to_grayscale)
|
|
293
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Blue.to_grayscale)
|
|
294
|
+
assert_equal(Color::Grayscale.from_fraction(0.2510), Color::RGB::Purple.to_grayscale)
|
|
295
|
+
assert_equal(Color::Grayscale.from_percentage(40.58), Color::RGB::Brown.to_grayscale)
|
|
296
|
+
assert_equal(Color::Grayscale.from_percentage(68.43), Color::RGB::Carnation.to_grayscale)
|
|
297
|
+
assert_equal(Color::Grayscale.from_percentage(27.65), Color::RGB::Cayenne.to_grayscale)
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def test_to_hsl
|
|
301
|
+
assert_kind_of(Color::HSL, Color::RGB::Black.to_hsl)
|
|
302
|
+
assert_equal(Color::HSL.from_values(0, 0, 0), Color::RGB::Black.to_hsl)
|
|
303
|
+
assert_equal(Color::HSL.from_values(60, 100, 50), Color::RGB::Yellow.to_hsl)
|
|
304
|
+
assert_equal(Color::HSL.from_values(180, 100, 50), Color::RGB::Cyan.to_hsl)
|
|
305
|
+
assert_equal(Color::HSL.from_values(300, 100, 50), Color::RGB::Magenta.to_hsl)
|
|
306
|
+
assert_equal(Color::HSL.from_values(0, 100, 50), Color::RGB::Red.to_hsl)
|
|
307
|
+
assert_equal(Color::HSL.from_values(120, 100, 50), Color::RGB::Lime.to_hsl)
|
|
308
|
+
assert_equal(Color::HSL.from_values(240, 100, 50), Color::RGB::Blue.to_hsl)
|
|
309
|
+
assert_equal(Color::HSL.from_values(300, 100, 25.10), Color::RGB::Purple.to_hsl)
|
|
310
|
+
assert_equal(Color::HSL.from_values(0, 59.42, 40.59), Color::RGB::Brown.to_hsl)
|
|
311
|
+
assert_equal(Color::HSL.from_values(317.5, 100, 68.43), Color::RGB::Carnation.to_hsl)
|
|
312
|
+
assert_equal(Color::HSL.from_values(0, 100, 27.64), Color::RGB::Cayenne.to_hsl)
|
|
313
|
+
|
|
314
|
+
# The following tests a bug reported by Jean Krohn on 10 June 2006 where HSL
|
|
315
|
+
# conversion was not quite correct, resulting in a bad round-trip.
|
|
316
|
+
assert_equal("RGB [#008800]", Color::RGB.from_html("#008800").to_hsl.to_rgb.inspect)
|
|
317
|
+
refute_equal("RGB [#002288]", Color::RGB.from_html("#008800").to_hsl.to_rgb.inspect)
|
|
318
|
+
|
|
319
|
+
# The following tests a bug reported by Adam Johnson on 29 October
|
|
320
|
+
# 2010.
|
|
321
|
+
hsl = Color::HSL.from_values(262, 67, 42)
|
|
322
|
+
c = Color::RGB.from_fraction(0.34496, 0.1386, 0.701399).to_hsl
|
|
323
|
+
assert_in_tolerance hsl.h, c.h, "Hue"
|
|
324
|
+
assert_in_tolerance hsl.s, c.s, "Saturation"
|
|
325
|
+
assert_in_tolerance hsl.l, c.l, "Luminance"
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
def test_to_lab
|
|
329
|
+
# Luminosity can be an absolute.
|
|
330
|
+
assert_in_tolerance(0.0, Color::RGB::Black.to_lab.l)
|
|
331
|
+
assert_in_tolerance(100.0, Color::RGB::White.to_lab.l)
|
|
332
|
+
|
|
333
|
+
# It's not really possible to have absolute
|
|
334
|
+
# numbers here because of how L*a*b* works, but
|
|
335
|
+
# negative/positive comparisons work
|
|
336
|
+
assert(Color::RGB::Green.to_lab.a < 0)
|
|
337
|
+
assert(Color::RGB::Magenta.to_lab.a > 0)
|
|
338
|
+
assert(Color::RGB::Blue.to_lab.b < 0)
|
|
339
|
+
assert(Color::RGB::Yellow.to_lab.b > 0)
|
|
340
|
+
end
|
|
328
341
|
|
|
329
342
|
# # An RGB color round-tripped through CIELAB should still have more or less the same
|
|
330
343
|
# # RGB values, but this doesn't really work because the color math here is slightly
|
|
@@ -340,5 +353,36 @@ module TestColor
|
|
|
340
353
|
# assert_in_tolerance(c1.b, c2.b)
|
|
341
354
|
# end
|
|
342
355
|
# end
|
|
356
|
+
|
|
357
|
+
def test_to_rgb
|
|
358
|
+
assert_same(Color::RGB::Black, Color::RGB::Black.to_rgb)
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
def test_to_xyz
|
|
362
|
+
assert_kind_of(Color::XYZ, Color::RGB::Black.to_xyz)
|
|
363
|
+
assert_equal(Color::XYZ.from_values(0, 0, 0), Color::RGB::Black.to_xyz)
|
|
364
|
+
assert_equal(Color::XYZ.from_fraction(0.4124564, 0.2126729, 0.0193339), Color::RGB::Red.to_xyz)
|
|
365
|
+
assert_equal(Color::XYZ.from_fraction(0.0771865, 0.1543731, 0.0257288), Color::RGB::Green.to_xyz)
|
|
366
|
+
assert_equal(Color::XYZ.from_fraction(0.1804375, 0.072175, 0.9503041), Color::RGB::Blue.to_xyz)
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
def test_to_yiq
|
|
370
|
+
assert_kind_of(Color::YIQ, Color::RGB::Black.to_yiq)
|
|
371
|
+
assert_equal(Color::YIQ.from_values(0, 0, 0), Color::RGB::Black.to_yiq)
|
|
372
|
+
assert_equal(Color::YIQ.from_values(88.6, 32.1, 0), Color::RGB::Yellow.to_yiq)
|
|
373
|
+
assert_equal(Color::YIQ.from_values(70.1, 0, 0), Color::RGB::Cyan.to_yiq)
|
|
374
|
+
assert_equal(Color::YIQ.from_values(41.3, 27.5, 52.3), Color::RGB::Magenta.to_yiq)
|
|
375
|
+
assert_equal(Color::YIQ.from_values(29.9, 59.6, 21.2), Color::RGB::Red.to_yiq)
|
|
376
|
+
assert_equal(Color::YIQ.from_values(58.7, 0, 0), Color::RGB::Lime.to_yiq)
|
|
377
|
+
assert_equal(Color::YIQ.from_values(11.4, 0, 31.1), Color::RGB::Blue.to_yiq)
|
|
378
|
+
assert_equal(Color::YIQ.from_values(20.73, 13.80, 26.25), Color::RGB::Purple.to_yiq)
|
|
379
|
+
assert_equal(Color::YIQ.from_values(30.89, 28.75, 10.23), Color::RGB::Brown.to_yiq)
|
|
380
|
+
assert_equal(Color::YIQ.from_values(60.84, 23.28, 27.29), Color::RGB::Carnation.to_yiq)
|
|
381
|
+
assert_equal(Color::YIQ.from_values(16.53, 32.96, 11.72), Color::RGB::Cayenne.to_yiq)
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
def test_to_internal
|
|
385
|
+
assert_equal([0.0, 0.0, 0.0], Color::RGB::Black.to_internal)
|
|
386
|
+
end
|
|
343
387
|
end
|
|
344
388
|
end
|
data/test/test_yiq.rb
CHANGED
|
@@ -23,14 +23,6 @@ module TestColor
|
|
|
23
23
|
assert_in_tolerance(0.3, @yiq.q)
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
def test_to_grayscale
|
|
27
|
-
assert_equal(Color::Grayscale.from_fraction(0.1), @yiq.to_grayscale)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def test_to_yiq
|
|
31
|
-
assert_equal(@yiq, @yiq.to_yiq)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
26
|
def test_y
|
|
35
27
|
assert_in_tolerance(0.1, @yiq.y)
|
|
36
28
|
assert_in_tolerance(0.1, @yiq.y)
|
|
@@ -39,5 +31,61 @@ module TestColor
|
|
|
39
31
|
def test_inspect
|
|
40
32
|
assert_equal("YIQ [10.00% 20.00% 30.00%]", @yiq.inspect)
|
|
41
33
|
end
|
|
34
|
+
|
|
35
|
+
def test_pretty_print
|
|
36
|
+
assert_pretty_inspect "YIQ\n[10.00%\n 20.00%\n 30.00%]\n", @yiq
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class TestYIQConversions < Minitest::Test
|
|
41
|
+
def setup
|
|
42
|
+
@yiq = Color::YIQ.from_fraction(0.1, 0.2, 0.3)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def test_to_cmyk
|
|
46
|
+
skip("to_cmyk conversion not yet implemented")
|
|
47
|
+
cmyk = @yiq.to_cmyk
|
|
48
|
+
assert_kind_of(Color::CMYK, cmyk)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_to_grayscale
|
|
52
|
+
gs = @yiq.to_grayscale
|
|
53
|
+
assert_kind_of(Color::Grayscale, gs)
|
|
54
|
+
assert_in_tolerance(0.1, gs.g)
|
|
55
|
+
assert_equal(Color::Grayscale.from_fraction(0), Color::YIQ.from_values(0, 0, 0).to_grayscale)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def test_to_hsl
|
|
59
|
+
skip("to_hsl conversion not yet implemented")
|
|
60
|
+
hsl = @yiq.to_hsl
|
|
61
|
+
assert_kind_of(Color::HSL, hsl)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def test_to_lab
|
|
65
|
+
skip("to_lab conversion not yet implemented")
|
|
66
|
+
lab = @yiq.to_lab
|
|
67
|
+
assert_kind_of(Color::CIELAB, lab)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def test_to_rgb
|
|
71
|
+
skip("to_rgb conversion not yet implemented")
|
|
72
|
+
rgb = @yiq.to_rgb
|
|
73
|
+
assert_kind_of(Color::RGB, rgb)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def test_to_xyz
|
|
77
|
+
skip("to_xyz conversion not yet implemented")
|
|
78
|
+
xyz = @yiq.to_xyz
|
|
79
|
+
assert_kind_of(Color::XYZ, xyz)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def test_to_yiq
|
|
83
|
+
assert_equal(@yiq, @yiq.to_yiq)
|
|
84
|
+
assert_kind_of(Color::YIQ, @yiq.to_yiq)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def test_to_internal
|
|
88
|
+
assert_equal([0.0, 0.0, 0.0], Color::YIQ.from_values(0, 0, 0).to_internal)
|
|
89
|
+
end
|
|
42
90
|
end
|
|
43
91
|
end
|