color 2.1.0 → 2.1.2
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 +43 -3
- data/CODE_OF_CONDUCT.md +152 -114
- data/CONTRIBUTING.md +87 -49
- data/CONTRIBUTORS.md +2 -0
- data/README.md +3 -5
- data/Rakefile +5 -5
- data/SECURITY.md +17 -16
- data/lib/color/cielab.rb +5 -1
- data/lib/color/grayscale.rb +4 -0
- data/lib/color/hsl.rb +4 -4
- data/lib/color/rgb.rb +7 -4
- data/lib/color/version.rb +1 -1
- data/lib/color/xyz.rb +1 -1
- data/test/test_cmyk.rb +62 -33
- data/test/test_grayscale.rb +35 -3
- data/test/test_hsl.rb +72 -36
- data/test/test_rgb.rb +105 -91
- data/test/test_yiq.rb +52 -8
- metadata +20 -50
data/lib/color/cielab.rb
CHANGED
|
@@ -140,7 +140,7 @@ class Color::CIELAB
|
|
|
140
140
|
fx = a / 500.0 + fy
|
|
141
141
|
|
|
142
142
|
xr = ((fx3 = fx**3) > Color::XYZ::E) ? fx3 : (116.0 * fx - 16) / Color::XYZ::K
|
|
143
|
-
yr = (l > Color::XYZ::EK) ? ((l + 16.0) / 116)**3 : l
|
|
143
|
+
yr = (l > Color::XYZ::EK) ? ((l + 16.0) / 116)**3 : l / Color::XYZ::K
|
|
144
144
|
zr = ((fz3 = fz**3) > Color::XYZ::E) ? fz3 : (116.0 * fz - 16) / Color::XYZ::K
|
|
145
145
|
|
|
146
146
|
ref = kwargs[:white] || args.first
|
|
@@ -149,6 +149,10 @@ class Color::CIELAB
|
|
|
149
149
|
ref.scale(xr, yr, zr)
|
|
150
150
|
end
|
|
151
151
|
|
|
152
|
+
##
|
|
153
|
+
# Converts \CIELAB to Color::YIQ via Color::XYZ.
|
|
154
|
+
def to_yiq(...) = to_xyz(...).to_yiq(...)
|
|
155
|
+
|
|
152
156
|
##
|
|
153
157
|
# Render the CSS `lab()` function for this \CIELAB object, adding an `alpha` if
|
|
154
158
|
# provided.
|
data/lib/color/grayscale.rb
CHANGED
data/lib/color/hsl.rb
CHANGED
|
@@ -93,8 +93,8 @@ class Color::HSL
|
|
|
93
93
|
# fvd and van Dam, originally found at [1] (implemented similarly at [2]). This
|
|
94
94
|
# simplifies the calculations with the following assumptions:
|
|
95
95
|
#
|
|
96
|
-
# - Luminance values <= 0 always translate to Color::RGB
|
|
97
|
-
# - Luminance values >= 1 always translate to Color::RGB
|
|
96
|
+
# - Luminance values <= 0 always translate to a black Color::RGB value.
|
|
97
|
+
# - Luminance values >= 1 always translate to a white Color::RGB value.
|
|
98
98
|
# - Saturation values <= 0 always translate to a shade of gray using luminance as
|
|
99
99
|
# a percentage of gray.
|
|
100
100
|
#
|
|
@@ -102,9 +102,9 @@ class Color::HSL
|
|
|
102
102
|
# [2] http://support.microsoft.com/kb/29240
|
|
103
103
|
def to_rgb(...)
|
|
104
104
|
if near_zero_or_less?(l)
|
|
105
|
-
Color::RGB::
|
|
105
|
+
Color::RGB::Black000
|
|
106
106
|
elsif near_one_or_more?(l)
|
|
107
|
-
Color::RGB::
|
|
107
|
+
Color::RGB::WhiteFFF
|
|
108
108
|
elsif near_zero?(s)
|
|
109
109
|
Color::RGB.from_fraction(l, l, l)
|
|
110
110
|
else
|
data/lib/color/rgb.rb
CHANGED
|
@@ -63,6 +63,9 @@ class Color::RGB
|
|
|
63
63
|
super(r: normalize(r), g: normalize(g), b: normalize(b), names: names)
|
|
64
64
|
end
|
|
65
65
|
|
|
66
|
+
Black000 = new(r: 0x00, g: 0x00, b: 0x00) # :nodoc:
|
|
67
|
+
WhiteFFF = new(r: 0xff, g: 0xff, b: 0xff) # :nodoc:
|
|
68
|
+
|
|
66
69
|
##
|
|
67
70
|
# :attr_reader: name
|
|
68
71
|
# The primary name for this \RGB color.
|
|
@@ -263,18 +266,18 @@ class Color::RGB
|
|
|
263
266
|
def delta_e2000(other) = to_lab.delta_e2000(coerce(other).to_lab)
|
|
264
267
|
|
|
265
268
|
##
|
|
266
|
-
# Mix the \RGB hue with
|
|
269
|
+
# Mix the \RGB hue with white so that the \RGB hue is the specified percentage of the
|
|
267
270
|
# resulting color.
|
|
268
271
|
#
|
|
269
272
|
# Strictly speaking, this isn't a `lighten_by` operation, but it mostly works.
|
|
270
|
-
def lighten_by(percent) = mix_with(Color::RGB::
|
|
273
|
+
def lighten_by(percent) = mix_with(Color::RGB::WhiteFFF, percent)
|
|
271
274
|
|
|
272
275
|
##
|
|
273
|
-
# Mix the \RGB hue with
|
|
276
|
+
# Mix the \RGB hue with black so that the \RGB hue is the specified percentage of the
|
|
274
277
|
# resulting color.
|
|
275
278
|
#
|
|
276
279
|
# Strictly speaking, this isn't a `darken_by` operation, but it mostly works.
|
|
277
|
-
def darken_by(percent) = mix_with(Color::RGB::
|
|
280
|
+
def darken_by(percent) = mix_with(Color::RGB::Black000, percent)
|
|
278
281
|
|
|
279
282
|
##
|
|
280
283
|
# Mix the mask color with the current color at the stated opacity percentage (0..100).
|
data/lib/color/version.rb
CHANGED
data/lib/color/xyz.rb
CHANGED
data/test/test_cmyk.rb
CHANGED
|
@@ -30,31 +30,62 @@ module TestColor
|
|
|
30
30
|
assert_in_tolerance(40, @cmyk.black)
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
+
def test_inspect
|
|
34
|
+
assert_equal("CMYK [10.00% 20.00% 30.00% 40.00%]", @cmyk.inspect)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def test_css
|
|
38
|
+
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00%, rgb(54.00% 48.00% 42.00%))", @cmyk.css)
|
|
39
|
+
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00% / 0.50, rgb(54.00% 48.00% 42.00% / 0.50))", @cmyk.css(alpha: 0.5))
|
|
40
|
+
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00%)", @cmyk.css(fallback: false))
|
|
41
|
+
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00% / 0.50)", @cmyk.css(alpha: 0.5, fallback: false))
|
|
42
|
+
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00%, rgb(0 0 100.00%))", @cmyk.css(fallback: Color::RGB::Blue))
|
|
43
|
+
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00% / 0.50, rgb(0 0 100.00% / 0.50))", @cmyk.css(alpha: 0.5, fallback: Color::RGB::Blue))
|
|
44
|
+
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00% / 0.50, rgb(0 0 100.00%))", @cmyk.css(alpha: 0.5, fallback: {color: Color::RGB::Blue}))
|
|
45
|
+
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00% / 0.50, rgb(0 0 100.00% / 0.30))", @cmyk.css(alpha: 0.5, fallback: {color: Color::RGB::Blue, alpha: 0.3}))
|
|
46
|
+
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00% / 0.50, rgb(54.00% 48.00% 42.00% / 0.30))", @cmyk.css(alpha: 0.5, fallback: {alpha: 0.3}))
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
class TestCMYKConversions < Minitest::Test
|
|
51
|
+
def setup
|
|
52
|
+
@cmyk = Color::CMYK.from_percentage(10, 20, 30, 40)
|
|
53
|
+
end
|
|
54
|
+
|
|
33
55
|
def test_to_cmyk
|
|
34
|
-
|
|
56
|
+
assert_equal(@cmyk, @cmyk.to_cmyk)
|
|
57
|
+
assert_kind_of(Color::CMYK, @cmyk.to_cmyk)
|
|
35
58
|
end
|
|
36
59
|
|
|
37
60
|
def test_to_grayscale
|
|
38
61
|
gs = @cmyk.to_grayscale
|
|
39
62
|
assert_kind_of(Color::Grayscale, gs)
|
|
40
63
|
assert_in_tolerance(0.4185, gs.g)
|
|
41
|
-
assert_kind_of(Color::Grayscale, @cmyk.to_grayscale)
|
|
42
64
|
end
|
|
43
65
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
66
|
+
def test_to_hsl
|
|
67
|
+
hsl = @cmyk.to_hsl
|
|
68
|
+
assert_kind_of(Color::HSL, hsl)
|
|
69
|
+
assert_in_tolerance(0.48, hsl.l)
|
|
70
|
+
assert_in_tolerance(0.125, hsl.s)
|
|
71
|
+
assert_in_tolerance(0.08333, hsl.h)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def test_to_lab
|
|
75
|
+
lab = @cmyk.to_lab
|
|
76
|
+
assert_kind_of(Color::CIELAB, lab)
|
|
77
|
+
assert_in_tolerance(52.35269, lab.l)
|
|
78
|
+
assert_in_tolerance(3.268274, lab.a)
|
|
79
|
+
assert_in_tolerance(10.52531, lab.b)
|
|
80
|
+
assert_equal(Color::CIELAB.from_values(0, 0, 0), Color::CMYK.from_percentage(0, 0, 0, 100).to_lab)
|
|
81
|
+
end
|
|
51
82
|
|
|
52
83
|
def test_to_rgb
|
|
53
|
-
|
|
54
|
-
assert_kind_of(Color::RGB,
|
|
55
|
-
assert_in_tolerance(0.5,
|
|
56
|
-
assert_in_tolerance(0.4,
|
|
57
|
-
assert_in_tolerance(0.3,
|
|
84
|
+
rgb_adobe = @cmyk.to_rgb(rgb_method: :adobe)
|
|
85
|
+
assert_kind_of(Color::RGB, rgb_adobe)
|
|
86
|
+
assert_in_tolerance(0.5, rgb_adobe.r)
|
|
87
|
+
assert_in_tolerance(0.4, rgb_adobe.g)
|
|
88
|
+
assert_in_tolerance(0.3, rgb_adobe.b)
|
|
58
89
|
|
|
59
90
|
rgb = @cmyk.to_rgb
|
|
60
91
|
assert_kind_of(Color::RGB, rgb)
|
|
@@ -63,28 +94,26 @@ module TestColor
|
|
|
63
94
|
assert_in_tolerance(0.42, rgb.b)
|
|
64
95
|
end
|
|
65
96
|
|
|
66
|
-
def
|
|
67
|
-
|
|
97
|
+
def test_to_xyz
|
|
98
|
+
xyz = @cmyk.to_xyz
|
|
99
|
+
assert_kind_of(Color::XYZ, xyz)
|
|
100
|
+
assert_in_tolerance(0.200995, xyz.x)
|
|
101
|
+
assert_in_tolerance(0.204594, xyz.y)
|
|
102
|
+
assert_in_tolerance(0.168249, xyz.z)
|
|
103
|
+
assert_equal(Color::XYZ.from_values(0, 0, 0), Color::CMYK.from_percentage(0, 0, 0, 100).to_xyz)
|
|
68
104
|
end
|
|
69
105
|
|
|
70
|
-
def
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
assert_equal(
|
|
77
|
-
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00% / 0.50, rgb(0 0 100.00%))", @cmyk.css(alpha: 0.5, fallback: {color: Color::RGB::Blue}))
|
|
78
|
-
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00% / 0.50, rgb(0 0 100.00% / 0.30))", @cmyk.css(alpha: 0.5, fallback: {color: Color::RGB::Blue, alpha: 0.3}))
|
|
79
|
-
assert_equal("device-cmyk(10.00% 20.00% 30.00% 40.00% / 0.50, rgb(54.00% 48.00% 42.00% / 0.30))", @cmyk.css(alpha: 0.5, fallback: {alpha: 0.3}))
|
|
106
|
+
def test_to_yiq
|
|
107
|
+
yiq = @cmyk.to_yiq
|
|
108
|
+
assert_kind_of(Color::YIQ, yiq)
|
|
109
|
+
assert_in_tolerance(0.4911, yiq.y)
|
|
110
|
+
assert_in_tolerance(0.05502, yiq.i)
|
|
111
|
+
assert_in_tolerance(0.0, yiq.q)
|
|
112
|
+
assert_equal(Color::YIQ.from_values(0, 0, 0), Color::CMYK.from_percentage(0, 0, 0, 100).to_yiq)
|
|
80
113
|
end
|
|
81
114
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
# assert_in_tolerance(0.4911, yiq.y)
|
|
86
|
-
# assert_in_tolerance(0.05502, yiq.i)
|
|
87
|
-
# assert_in_tolerance(0.0, yiq.q)
|
|
88
|
-
# end
|
|
115
|
+
def test_to_internal
|
|
116
|
+
assert_equal([0.0, 0.0, 0.0, 1.0], Color::CMYK.from_percentage(0, 0, 0, 100).to_internal)
|
|
117
|
+
end
|
|
89
118
|
end
|
|
90
119
|
end
|
data/test/test_grayscale.rb
CHANGED
|
@@ -39,6 +39,16 @@ module TestColor
|
|
|
39
39
|
assert_in_tolerance(0.363, @gs.lighten_by(10).g)
|
|
40
40
|
end
|
|
41
41
|
|
|
42
|
+
def test_inspect
|
|
43
|
+
assert_equal("Grayscale [33.00%]", @gs.inspect)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
class TestGrayscaleConversions < Minitest::Test
|
|
48
|
+
def setup
|
|
49
|
+
@gs = Color::Grayscale.from_percentage(33)
|
|
50
|
+
end
|
|
51
|
+
|
|
42
52
|
def test_to_cmyk
|
|
43
53
|
cmyk = @gs.to_cmyk
|
|
44
54
|
assert_kind_of(Color::CMYK, cmyk)
|
|
@@ -46,11 +56,12 @@ module TestColor
|
|
|
46
56
|
assert_in_tolerance(0.0, cmyk.m)
|
|
47
57
|
assert_in_tolerance(0.0, cmyk.y)
|
|
48
58
|
assert_in_tolerance(0.67, cmyk.k)
|
|
59
|
+
assert_equal(Color::CMYK.from_percentage(0, 0, 0, 100), Color::Grayscale.from_percentage(0).to_cmyk)
|
|
49
60
|
end
|
|
50
61
|
|
|
51
62
|
def test_to_grayscale
|
|
52
63
|
assert_equal(@gs, @gs.to_grayscale)
|
|
53
|
-
|
|
64
|
+
assert_kind_of(Color::Grayscale, @gs.to_grayscale)
|
|
54
65
|
end
|
|
55
66
|
|
|
56
67
|
def test_to_hsl
|
|
@@ -59,6 +70,16 @@ module TestColor
|
|
|
59
70
|
assert_in_tolerance(0.0, hsl.h)
|
|
60
71
|
assert_in_tolerance(0.0, hsl.s)
|
|
61
72
|
assert_in_tolerance(0.33, hsl.l)
|
|
73
|
+
assert_equal(Color::HSL.from_values(0, 0, 0), Color::Grayscale.from_percentage(0).to_hsl)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def test_to_lab
|
|
77
|
+
lab = @gs.to_lab
|
|
78
|
+
assert_kind_of(Color::CIELAB, lab)
|
|
79
|
+
assert_in_tolerance(35.78746, lab.l)
|
|
80
|
+
assert_in_tolerance(0.0031199, lab.a)
|
|
81
|
+
assert_in_tolerance(-0.000639, lab.b)
|
|
82
|
+
assert_equal(Color::CIELAB.from_values(0, 0, 0), Color::Grayscale.from_percentage(0).to_lab)
|
|
62
83
|
end
|
|
63
84
|
|
|
64
85
|
def test_to_rgb
|
|
@@ -67,6 +88,16 @@ module TestColor
|
|
|
67
88
|
assert_in_tolerance(0.33, rgb.r)
|
|
68
89
|
assert_in_tolerance(0.33, rgb.g)
|
|
69
90
|
assert_in_tolerance(0.33, rgb.b)
|
|
91
|
+
assert_equal(Color::RGB.from_values(0, 0, 0), Color::Grayscale.from_percentage(0).to_rgb)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def test_to_xyz
|
|
95
|
+
xyz = @gs.to_xyz
|
|
96
|
+
assert_kind_of(Color::XYZ, xyz)
|
|
97
|
+
assert_in_tolerance(0.08457, xyz.x)
|
|
98
|
+
assert_in_tolerance(0.08898, xyz.y)
|
|
99
|
+
assert_in_tolerance(0.09688, xyz.z)
|
|
100
|
+
assert_equal(Color::XYZ.from_values(0, 0, 0), Color::Grayscale.from_percentage(0).to_xyz)
|
|
70
101
|
end
|
|
71
102
|
|
|
72
103
|
def test_to_yiq
|
|
@@ -75,10 +106,11 @@ module TestColor
|
|
|
75
106
|
assert_in_tolerance(0.33, yiq.y)
|
|
76
107
|
assert_in_tolerance(0.0, yiq.i)
|
|
77
108
|
assert_in_tolerance(0.0, yiq.q)
|
|
109
|
+
assert_equal(Color::YIQ.from_values(0, 0, 0), Color::Grayscale.from_percentage(0).to_yiq)
|
|
78
110
|
end
|
|
79
111
|
|
|
80
|
-
def
|
|
81
|
-
assert_equal(
|
|
112
|
+
def test_to_internal
|
|
113
|
+
assert_equal([0.0], Color::Grayscale.from_percentage(0).to_internal)
|
|
82
114
|
end
|
|
83
115
|
end
|
|
84
116
|
end
|
data/test/test_hsl.rb
CHANGED
|
@@ -66,66 +66,102 @@ 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
|
+
end
|
|
86
|
+
|
|
87
|
+
class TestHSLConversions < Minitest::Test
|
|
88
|
+
def setup
|
|
89
|
+
@hsl = Color::HSL.from_values(145, 20, 30)
|
|
90
|
+
end
|
|
91
|
+
|
|
69
92
|
def test_to_cmyk
|
|
70
93
|
cmyk = @hsl.to_cmyk
|
|
71
|
-
assert_kind_of
|
|
72
|
-
assert_in_tolerance
|
|
73
|
-
assert_in_tolerance
|
|
74
|
-
assert_in_tolerance
|
|
75
|
-
assert_in_tolerance
|
|
94
|
+
assert_kind_of(Color::CMYK, cmyk)
|
|
95
|
+
assert_in_tolerance(0.3223, cmyk.c)
|
|
96
|
+
assert_in_tolerance(0.2023, cmyk.m)
|
|
97
|
+
assert_in_tolerance(0.2723, cmyk.y)
|
|
98
|
+
assert_in_tolerance(0.4377, cmyk.k)
|
|
99
|
+
assert_equal(Color::CMYK.from_percentage(0, 0, 0, 100), Color::HSL.from_values(0, 0, 0).to_cmyk)
|
|
76
100
|
end
|
|
77
101
|
|
|
78
102
|
def test_to_grayscale
|
|
79
103
|
gs = @hsl.to_grayscale
|
|
80
|
-
assert_kind_of
|
|
81
|
-
assert_in_tolerance
|
|
104
|
+
assert_kind_of(Color::Grayscale, gs)
|
|
105
|
+
assert_in_tolerance(30, gs.gray)
|
|
106
|
+
assert_equal(Color::Grayscale.from_fraction(0), Color::HSL.from_values(0, 0, 0).to_grayscale)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def test_to_hsl
|
|
110
|
+
assert_equal(@hsl, @hsl.to_hsl)
|
|
111
|
+
assert_kind_of(Color::HSL, @hsl.to_hsl)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def test_to_lab
|
|
115
|
+
lab = @hsl.to_lab
|
|
116
|
+
assert_kind_of(Color::CIELAB, lab)
|
|
117
|
+
assert_in_tolerance(36.19817, lab.l)
|
|
118
|
+
assert_in_tolerance(-15.6, lab.a)
|
|
119
|
+
assert_in_tolerance(6.72317, lab.b)
|
|
120
|
+
assert_equal(Color::CIELAB.from_values(0, 0, 0), Color::HSL.from_values(0, 0, 0).to_lab)
|
|
82
121
|
end
|
|
83
122
|
|
|
84
123
|
def test_to_rgb
|
|
85
124
|
rgb = @hsl.to_rgb
|
|
86
|
-
assert_kind_of
|
|
87
|
-
assert_in_tolerance
|
|
88
|
-
assert_in_tolerance
|
|
89
|
-
assert_in_tolerance
|
|
125
|
+
assert_kind_of(Color::RGB, rgb)
|
|
126
|
+
assert_in_tolerance(0.24, rgb.r)
|
|
127
|
+
assert_in_tolerance(0.36, rgb.g)
|
|
128
|
+
assert_in_tolerance(0.29, rgb.b)
|
|
90
129
|
|
|
91
130
|
# The following tests address a bug reported by Jean Krohn on June 6,
|
|
92
131
|
# 2006 and exercise some previously unexercised code in to_rgb.
|
|
93
|
-
assert_equal
|
|
94
|
-
assert_equal
|
|
95
|
-
assert_equal
|
|
132
|
+
assert_equal(Color::RGB::Black, Color::HSL.from_values(75, 75, 0))
|
|
133
|
+
assert_equal(Color::RGB::White, Color::HSL.from_values(75, 75, 100))
|
|
134
|
+
assert_equal(Color::RGB::Gray80, Color::HSL.from_values(75, 0, 80))
|
|
96
135
|
|
|
97
136
|
# The following tests a bug reported by Adam Johnson on 29 October
|
|
98
137
|
# 2010.
|
|
99
138
|
rgb = Color::RGB.from_fraction(0.34496, 0.1386, 0.701399)
|
|
100
139
|
c = Color::HSL.from_values(262, 67, 42).to_rgb
|
|
101
|
-
assert_in_tolerance
|
|
102
|
-
assert_in_tolerance
|
|
103
|
-
assert_in_tolerance
|
|
140
|
+
assert_in_tolerance(rgb.r, c.r, "Red")
|
|
141
|
+
assert_in_tolerance(rgb.g, c.g, "Green")
|
|
142
|
+
assert_in_tolerance(rgb.b, c.b, "Blue")
|
|
104
143
|
end
|
|
105
144
|
|
|
106
|
-
def
|
|
107
|
-
|
|
108
|
-
assert_kind_of
|
|
109
|
-
assert_in_tolerance
|
|
110
|
-
assert_in_tolerance
|
|
111
|
-
assert_in_tolerance
|
|
145
|
+
def test_to_xyz
|
|
146
|
+
xyz = @hsl.to_xyz
|
|
147
|
+
assert_kind_of(Color::XYZ, xyz)
|
|
148
|
+
assert_in_tolerance(0.069805, xyz.x)
|
|
149
|
+
assert_in_tolerance(0.091115, xyz.y)
|
|
150
|
+
assert_in_tolerance(0.078593, xyz.z)
|
|
151
|
+
assert_equal(Color::XYZ.from_values(0, 0, 0), Color::HSL.from_values(0, 0, 0).to_xyz)
|
|
112
152
|
end
|
|
113
153
|
|
|
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
|
|
154
|
+
def test_to_yiq
|
|
155
|
+
yiq = @hsl.to_yiq
|
|
156
|
+
assert_kind_of(Color::YIQ, yiq)
|
|
157
|
+
assert_in_tolerance(0.3161, yiq.y)
|
|
158
|
+
assert_in_tolerance(0.0, yiq.i)
|
|
159
|
+
assert_in_tolerance(0.0, yiq.q)
|
|
160
|
+
assert_equal(Color::YIQ.from_values(0, 0, 0), Color::HSL.from_values(0, 0, 0).to_yiq)
|
|
125
161
|
end
|
|
126
162
|
|
|
127
|
-
def
|
|
128
|
-
assert_equal
|
|
163
|
+
def test_to_internal
|
|
164
|
+
assert_equal([0.0, 0.0, 0.0], Color::HSL.from_values(0, 0, 0).to_internal)
|
|
129
165
|
end
|
|
130
166
|
end
|
|
131
167
|
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]
|
|
@@ -325,6 +234,80 @@ module TestColor
|
|
|
325
234
|
|
|
326
235
|
assert_equal("#ff00aa", Color::RGB.by_hex("#ff00aa").to_lab.to_rgb.html)
|
|
327
236
|
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
class TestRGBConversions < Minitest::Test
|
|
240
|
+
def test_to_cmyk
|
|
241
|
+
assert_kind_of(Color::CMYK, Color::RGB::Black.to_cmyk)
|
|
242
|
+
assert_equal(Color::CMYK.from_percentage(0, 0, 0, 100), Color::RGB::Black.to_cmyk)
|
|
243
|
+
assert_equal(Color::CMYK.from_percentage(0, 0, 100, 0), Color::RGB::Yellow.to_cmyk)
|
|
244
|
+
assert_equal(Color::CMYK.from_percentage(100, 0, 0, 0), Color::RGB::Cyan.to_cmyk)
|
|
245
|
+
assert_equal(Color::CMYK.from_percentage(0, 100, 0, 0), Color::RGB::Magenta.to_cmyk)
|
|
246
|
+
assert_equal(Color::CMYK.from_percentage(0, 100, 100, 0), Color::RGB::Red.to_cmyk)
|
|
247
|
+
assert_equal(Color::CMYK.from_percentage(100, 0, 100, 0), Color::RGB::Lime.to_cmyk)
|
|
248
|
+
assert_equal(Color::CMYK.from_percentage(100, 100, 0, 0), Color::RGB::Blue.to_cmyk)
|
|
249
|
+
assert_equal(Color::CMYK.from_percentage(10.32, 60.52, 10.32, 39.47), Color::RGB::Purple.to_cmyk)
|
|
250
|
+
assert_equal(Color::CMYK.from_percentage(10.90, 59.13, 59.13, 24.39), Color::RGB::Brown.to_cmyk)
|
|
251
|
+
assert_equal(Color::CMYK.from_percentage(0, 63.14, 18.43, 0), Color::RGB::Carnation.to_cmyk)
|
|
252
|
+
assert_equal(Color::CMYK.from_percentage(7.39, 62.69, 62.69, 37.32), Color::RGB::Cayenne.to_cmyk)
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def test_to_grayscale
|
|
256
|
+
assert_kind_of(Color::Grayscale, Color::RGB::Black.to_grayscale)
|
|
257
|
+
assert_equal(Color::Grayscale.from_fraction(0), Color::RGB::Black.to_grayscale)
|
|
258
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Yellow.to_grayscale)
|
|
259
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Cyan.to_grayscale)
|
|
260
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Magenta.to_grayscale)
|
|
261
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Red.to_grayscale)
|
|
262
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Lime.to_grayscale)
|
|
263
|
+
assert_equal(Color::Grayscale.from_fraction(0.5), Color::RGB::Blue.to_grayscale)
|
|
264
|
+
assert_equal(Color::Grayscale.from_fraction(0.2510), Color::RGB::Purple.to_grayscale)
|
|
265
|
+
assert_equal(Color::Grayscale.from_percentage(40.58), Color::RGB::Brown.to_grayscale)
|
|
266
|
+
assert_equal(Color::Grayscale.from_percentage(68.43), Color::RGB::Carnation.to_grayscale)
|
|
267
|
+
assert_equal(Color::Grayscale.from_percentage(27.65), Color::RGB::Cayenne.to_grayscale)
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def test_to_hsl
|
|
271
|
+
assert_kind_of(Color::HSL, Color::RGB::Black.to_hsl)
|
|
272
|
+
assert_equal(Color::HSL.from_values(0, 0, 0), Color::RGB::Black.to_hsl)
|
|
273
|
+
assert_equal(Color::HSL.from_values(60, 100, 50), Color::RGB::Yellow.to_hsl)
|
|
274
|
+
assert_equal(Color::HSL.from_values(180, 100, 50), Color::RGB::Cyan.to_hsl)
|
|
275
|
+
assert_equal(Color::HSL.from_values(300, 100, 50), Color::RGB::Magenta.to_hsl)
|
|
276
|
+
assert_equal(Color::HSL.from_values(0, 100, 50), Color::RGB::Red.to_hsl)
|
|
277
|
+
assert_equal(Color::HSL.from_values(120, 100, 50), Color::RGB::Lime.to_hsl)
|
|
278
|
+
assert_equal(Color::HSL.from_values(240, 100, 50), Color::RGB::Blue.to_hsl)
|
|
279
|
+
assert_equal(Color::HSL.from_values(300, 100, 25.10), Color::RGB::Purple.to_hsl)
|
|
280
|
+
assert_equal(Color::HSL.from_values(0, 59.42, 40.59), Color::RGB::Brown.to_hsl)
|
|
281
|
+
assert_equal(Color::HSL.from_values(317.5, 100, 68.43), Color::RGB::Carnation.to_hsl)
|
|
282
|
+
assert_equal(Color::HSL.from_values(0, 100, 27.64), Color::RGB::Cayenne.to_hsl)
|
|
283
|
+
|
|
284
|
+
# The following tests a bug reported by Jean Krohn on 10 June 2006 where HSL
|
|
285
|
+
# conversion was not quite correct, resulting in a bad round-trip.
|
|
286
|
+
assert_equal("RGB [#008800]", Color::RGB.from_html("#008800").to_hsl.to_rgb.inspect)
|
|
287
|
+
refute_equal("RGB [#002288]", Color::RGB.from_html("#008800").to_hsl.to_rgb.inspect)
|
|
288
|
+
|
|
289
|
+
# The following tests a bug reported by Adam Johnson on 29 October
|
|
290
|
+
# 2010.
|
|
291
|
+
hsl = Color::HSL.from_values(262, 67, 42)
|
|
292
|
+
c = Color::RGB.from_fraction(0.34496, 0.1386, 0.701399).to_hsl
|
|
293
|
+
assert_in_tolerance hsl.h, c.h, "Hue"
|
|
294
|
+
assert_in_tolerance hsl.s, c.s, "Saturation"
|
|
295
|
+
assert_in_tolerance hsl.l, c.l, "Luminance"
|
|
296
|
+
end
|
|
297
|
+
|
|
298
|
+
def test_to_lab
|
|
299
|
+
# Luminosity can be an absolute.
|
|
300
|
+
assert_in_tolerance(0.0, Color::RGB::Black.to_lab.l)
|
|
301
|
+
assert_in_tolerance(100.0, Color::RGB::White.to_lab.l)
|
|
302
|
+
|
|
303
|
+
# It's not really possible to have absolute
|
|
304
|
+
# numbers here because of how L*a*b* works, but
|
|
305
|
+
# negative/positive comparisons work
|
|
306
|
+
assert(Color::RGB::Green.to_lab.a < 0)
|
|
307
|
+
assert(Color::RGB::Magenta.to_lab.a > 0)
|
|
308
|
+
assert(Color::RGB::Blue.to_lab.b < 0)
|
|
309
|
+
assert(Color::RGB::Yellow.to_lab.b > 0)
|
|
310
|
+
end
|
|
328
311
|
|
|
329
312
|
# # An RGB color round-tripped through CIELAB should still have more or less the same
|
|
330
313
|
# # RGB values, but this doesn't really work because the color math here is slightly
|
|
@@ -340,5 +323,36 @@ module TestColor
|
|
|
340
323
|
# assert_in_tolerance(c1.b, c2.b)
|
|
341
324
|
# end
|
|
342
325
|
# end
|
|
326
|
+
|
|
327
|
+
def test_to_rgb
|
|
328
|
+
assert_same(Color::RGB::Black, Color::RGB::Black.to_rgb)
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
def test_to_xyz
|
|
332
|
+
assert_kind_of(Color::XYZ, Color::RGB::Black.to_xyz)
|
|
333
|
+
assert_equal(Color::XYZ.from_values(0, 0, 0), Color::RGB::Black.to_xyz)
|
|
334
|
+
assert_equal(Color::XYZ.from_fraction(0.4124564, 0.2126729, 0.0193339), Color::RGB::Red.to_xyz)
|
|
335
|
+
assert_equal(Color::XYZ.from_fraction(0.0771865, 0.1543731, 0.0257288), Color::RGB::Green.to_xyz)
|
|
336
|
+
assert_equal(Color::XYZ.from_fraction(0.1804375, 0.072175, 0.9503041), Color::RGB::Blue.to_xyz)
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def test_to_yiq
|
|
340
|
+
assert_kind_of(Color::YIQ, Color::RGB::Black.to_yiq)
|
|
341
|
+
assert_equal(Color::YIQ.from_values(0, 0, 0), Color::RGB::Black.to_yiq)
|
|
342
|
+
assert_equal(Color::YIQ.from_values(88.6, 32.1, 0), Color::RGB::Yellow.to_yiq)
|
|
343
|
+
assert_equal(Color::YIQ.from_values(70.1, 0, 0), Color::RGB::Cyan.to_yiq)
|
|
344
|
+
assert_equal(Color::YIQ.from_values(41.3, 27.5, 52.3), Color::RGB::Magenta.to_yiq)
|
|
345
|
+
assert_equal(Color::YIQ.from_values(29.9, 59.6, 21.2), Color::RGB::Red.to_yiq)
|
|
346
|
+
assert_equal(Color::YIQ.from_values(58.7, 0, 0), Color::RGB::Lime.to_yiq)
|
|
347
|
+
assert_equal(Color::YIQ.from_values(11.4, 0, 31.1), Color::RGB::Blue.to_yiq)
|
|
348
|
+
assert_equal(Color::YIQ.from_values(20.73, 13.80, 26.25), Color::RGB::Purple.to_yiq)
|
|
349
|
+
assert_equal(Color::YIQ.from_values(30.89, 28.75, 10.23), Color::RGB::Brown.to_yiq)
|
|
350
|
+
assert_equal(Color::YIQ.from_values(60.84, 23.28, 27.29), Color::RGB::Carnation.to_yiq)
|
|
351
|
+
assert_equal(Color::YIQ.from_values(16.53, 32.96, 11.72), Color::RGB::Cayenne.to_yiq)
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def test_to_internal
|
|
355
|
+
assert_equal([0.0, 0.0, 0.0], Color::RGB::Black.to_internal)
|
|
356
|
+
end
|
|
343
357
|
end
|
|
344
358
|
end
|