color 1.7.1 → 2.0.0.pre.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.
Files changed (53) hide show
  1. checksums.yaml +5 -13
  2. data/CHANGELOG.md +298 -0
  3. data/CODE_OF_CONDUCT.md +128 -0
  4. data/CONTRIBUTING.md +70 -0
  5. data/CONTRIBUTORS.md +10 -0
  6. data/LICENCE.md +27 -0
  7. data/Manifest.txt +11 -21
  8. data/README.md +54 -0
  9. data/Rakefile +74 -53
  10. data/SECURITY.md +34 -0
  11. data/lib/color/cielab.rb +348 -0
  12. data/lib/color/cmyk.rb +279 -213
  13. data/lib/color/grayscale.rb +128 -160
  14. data/lib/color/hsl.rb +205 -173
  15. data/lib/color/rgb/colors.rb +177 -163
  16. data/lib/color/rgb.rb +534 -537
  17. data/lib/color/version.rb +5 -0
  18. data/lib/color/xyz.rb +214 -0
  19. data/lib/color/yiq.rb +91 -46
  20. data/lib/color.rb +208 -141
  21. data/test/fixtures/cielab.json +444 -0
  22. data/test/minitest_helper.rb +20 -4
  23. data/test/test_cmyk.rb +49 -71
  24. data/test/test_color.rb +58 -106
  25. data/test/test_grayscale.rb +35 -56
  26. data/test/test_hsl.rb +72 -76
  27. data/test/test_rgb.rb +195 -267
  28. data/test/test_yiq.rb +12 -30
  29. metadata +165 -150
  30. checksums.yaml.gz.sig +0 -0
  31. data/.autotest +0 -5
  32. data/.gemtest +0 -0
  33. data/.hoerc +0 -2
  34. data/.minitest.rb +0 -2
  35. data/.travis.yml +0 -35
  36. data/Contributing.rdoc +0 -60
  37. data/Gemfile +0 -9
  38. data/History.rdoc +0 -172
  39. data/Licence.rdoc +0 -27
  40. data/README.rdoc +0 -50
  41. data/lib/color/css.rb +0 -7
  42. data/lib/color/palette/adobecolor.rb +0 -260
  43. data/lib/color/palette/gimp.rb +0 -104
  44. data/lib/color/palette/monocontrast.rb +0 -164
  45. data/lib/color/palette.rb +0 -4
  46. data/lib/color/rgb/contrast.rb +0 -57
  47. data/lib/color/rgb/metallic.rb +0 -28
  48. data/test/test_adobecolor.rb +0 -405
  49. data/test/test_css.rb +0 -19
  50. data/test/test_gimp.rb +0 -87
  51. data/test/test_monocontrast.rb +0 -130
  52. data.tar.gz.sig +0 -0
  53. metadata.gz.sig +0 -0
data/test/test_rgb.rb CHANGED
@@ -1,7 +1,8 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- require 'color'
4
- require 'minitest_helper'
3
+ require "color"
4
+ require "json"
5
+ require "minitest_helper"
5
6
 
6
7
  module TestColor
7
8
  class TestRGB < Minitest::Test
@@ -16,61 +17,40 @@ module TestColor
16
17
  end
17
18
 
18
19
  def test_adjust_saturation
19
- assert_equal("#ef9374",
20
- Color::RGB::DarkSalmon.adjust_saturation(10).html)
21
- assert_equal("#e39980",
22
- Color::RGB::DarkSalmon.adjust_saturation(-10).html)
20
+ assert_equal("#ef9374", Color::RGB::DarkSalmon.adjust_saturation(10).html)
21
+ assert_equal("#e39980", Color::RGB::DarkSalmon.adjust_saturation(-10).html)
23
22
  end
24
23
 
25
24
  def test_red
26
- red = Color::RGB::Red.dup
27
- assert_in_delta(1.0, red.r, Color::COLOR_TOLERANCE)
28
- assert_in_delta(100, red.red_p, Color::COLOR_TOLERANCE)
29
- assert_in_delta(255, red.red, Color::COLOR_TOLERANCE)
30
- assert_in_delta(1.0, red.r, Color::COLOR_TOLERANCE)
31
- red.red_p = 33
32
- assert_in_delta(0.33, red.r, Color::COLOR_TOLERANCE)
33
- red.red = 330
34
- assert_in_delta(1.0, red.r, Color::COLOR_TOLERANCE)
35
- red.r = -3.3
36
- assert_in_delta(0.0, red.r, Color::COLOR_TOLERANCE)
25
+ red = Color::RGB::Red
26
+ assert_in_tolerance(1.0, red.r)
27
+ assert_in_tolerance(100, red.red_p)
28
+ assert_in_tolerance(255, red.red)
29
+ assert_in_tolerance(1.0, red.r)
37
30
  end
38
31
 
39
32
  def test_green
40
- lime = Color::RGB::Lime.dup
41
- assert_in_delta(1.0, lime.g, Color::COLOR_TOLERANCE)
42
- assert_in_delta(100, lime.green_p, Color::COLOR_TOLERANCE)
43
- assert_in_delta(255, lime.green, Color::COLOR_TOLERANCE)
44
- lime.green_p = 33
45
- assert_in_delta(0.33, lime.g, Color::COLOR_TOLERANCE)
46
- lime.green = 330
47
- assert_in_delta(1.0, lime.g, Color::COLOR_TOLERANCE)
48
- lime.g = -3.3
49
- assert_in_delta(0.0, lime.g, Color::COLOR_TOLERANCE)
33
+ lime = Color::RGB::Lime
34
+ assert_in_tolerance(1.0, lime.g)
35
+ assert_in_tolerance(100, lime.green_p)
36
+ assert_in_tolerance(255, lime.green)
50
37
  end
51
38
 
52
39
  def test_blue
53
- blue = Color::RGB::Blue.dup
54
- assert_in_delta(1.0, blue.b, Color::COLOR_TOLERANCE)
55
- assert_in_delta(255, blue.blue, Color::COLOR_TOLERANCE)
56
- assert_in_delta(100, blue.blue_p, Color::COLOR_TOLERANCE)
57
- blue.blue_p = 33
58
- assert_in_delta(0.33, blue.b, Color::COLOR_TOLERANCE)
59
- blue.blue = 330
60
- assert_in_delta(1.0, blue.b, Color::COLOR_TOLERANCE)
61
- blue.b = -3.3
62
- assert_in_delta(0.0, blue.b, Color::COLOR_TOLERANCE)
40
+ blue = Color::RGB::Blue
41
+ assert_in_tolerance(1.0, blue.b)
42
+ assert_in_tolerance(255, blue.blue)
43
+ assert_in_tolerance(100, blue.blue_p)
63
44
  end
64
45
 
65
46
  def test_brightness
66
- assert_in_delta(0.0, Color::RGB::Black.brightness, Color::COLOR_TOLERANCE)
67
- assert_in_delta(0.5, Color::RGB::Grey50.brightness, Color::COLOR_TOLERANCE)
68
- assert_in_delta(1.0, Color::RGB::White.brightness, Color::COLOR_TOLERANCE)
47
+ assert_in_tolerance(0.0, Color::RGB::Black.brightness)
48
+ assert_in_tolerance(0.5, Color::RGB::Grey50.brightness)
49
+ assert_in_tolerance(1.0, Color::RGB::White.brightness)
69
50
  end
70
51
 
71
52
  def test_darken_by
72
- assert_in_delta(0.5, Color::RGB::Blue.darken_by(50).b,
73
- Color::COLOR_TOLERANCE)
53
+ assert_in_tolerance(0.5, Color::RGB::Blue.darken_by(50).b)
74
54
  end
75
55
 
76
56
  def test_html
@@ -80,177 +60,93 @@ module TestColor
80
60
  assert_equal("#00ff00", Color::RGB::Lime.html)
81
61
  assert_equal("#ff0000", Color::RGB::Red.html)
82
62
  assert_equal("#ffffff", Color::RGB::White.html)
63
+ end
83
64
 
84
- assert_equal("rgb(0.00%, 0.00%, 0.00%)", Color::RGB::Black.css_rgb)
85
- assert_equal("rgb(0.00%, 0.00%, 100.00%)", Color::RGB::Blue.css_rgb)
86
- assert_equal("rgb(0.00%, 100.00%, 0.00%)", Color::RGB::Lime.css_rgb)
87
- assert_equal("rgb(100.00%, 0.00%, 0.00%)", Color::RGB::Red.css_rgb)
88
- assert_equal("rgb(100.00%, 100.00%, 100.00%)", Color::RGB::White.css_rgb)
89
-
90
- assert_equal("rgba(0.00%, 0.00%, 0.00%, 1.00)", Color::RGB::Black.css_rgba)
91
- assert_equal("rgba(0.00%, 0.00%, 100.00%, 1.00)", Color::RGB::Blue.css_rgba)
92
- assert_equal("rgba(0.00%, 100.00%, 0.00%, 1.00)", Color::RGB::Lime.css_rgba)
93
- assert_equal("rgba(100.00%, 0.00%, 0.00%, 1.00)", Color::RGB::Red.css_rgba)
94
- assert_equal("rgba(100.00%, 100.00%, 100.00%, 1.00)",
95
- Color::RGB::White.css_rgba)
65
+ def test_css
66
+ assert_equal("rgb(0 0 0)", Color::RGB::Black.css)
67
+ assert_equal("rgb(0 0 100.00%)", Color::RGB::Blue.css)
68
+ assert_equal("rgb(0 100.00% 0)", Color::RGB::Lime.css)
69
+ assert_equal("rgb(100.00% 0 0)", Color::RGB::Red.css)
70
+ assert_equal("rgb(100.00% 100.00% 100.00%)", Color::RGB::White.css)
71
+ assert_equal("rgb(0 0 0 / 1.00)", Color::RGB::Black.css(alpha: 1))
72
+ assert_equal("rgb(0 0 100.00% / 1.00)", Color::RGB::Blue.css(alpha: 1))
73
+ assert_equal("rgb(0 100.00% 0 / 1.00)", Color::RGB::Lime.css(alpha: 1))
74
+ assert_equal("rgb(100.00% 0 0 / 1.00)", Color::RGB::Red.css(alpha: 1))
75
+ assert_equal("rgb(100.00% 100.00% 100.00% / 1.00)", Color::RGB::White.css(alpha: 1))
76
+ assert_equal("rgb(100.00% 0 0 / 0.50)", Color::RGB::Red.css(alpha: 0.5))
96
77
  end
97
78
 
98
79
  def test_lighten_by
99
- assert_in_delta(1.0, Color::RGB::Blue.lighten_by(50).b,
100
- Color::COLOR_TOLERANCE)
101
- assert_in_delta(0.5, Color::RGB::Blue.lighten_by(50).r,
102
- Color::COLOR_TOLERANCE)
103
- assert_in_delta(0.5, Color::RGB::Blue.lighten_by(50).g,
104
- Color::COLOR_TOLERANCE)
80
+ assert_in_tolerance(1.0, Color::RGB::Blue.lighten_by(50).b)
81
+ assert_in_tolerance(0.5, Color::RGB::Blue.lighten_by(50).r)
82
+ assert_in_tolerance(0.5, Color::RGB::Blue.lighten_by(50).g)
105
83
  end
106
84
 
107
85
  def test_mix_with
108
- assert_in_delta(0.5, Color::RGB::Red.mix_with(Color::RGB::Blue, 50).r,
109
- Color::COLOR_TOLERANCE)
110
- assert_in_delta(0.0, Color::RGB::Red.mix_with(Color::RGB::Blue, 50).g,
111
- Color::COLOR_TOLERANCE)
112
- assert_in_delta(0.5, Color::RGB::Red.mix_with(Color::RGB::Blue, 50).b,
113
- Color::COLOR_TOLERANCE)
114
- assert_in_delta(0.5, Color::RGB::Blue.mix_with(Color::RGB::Red, 50).r,
115
- Color::COLOR_TOLERANCE)
116
- assert_in_delta(0.0, Color::RGB::Blue.mix_with(Color::RGB::Red, 50).g,
117
- Color::COLOR_TOLERANCE)
118
- assert_in_delta(0.5, Color::RGB::Blue.mix_with(Color::RGB::Red, 50).b,
119
- Color::COLOR_TOLERANCE)
120
- end
121
-
122
- def test_pdf_fill
123
- assert_equal("0.000 0.000 0.000 rg", Color::RGB::Black.pdf_fill)
124
- assert_equal("0.000 0.000 1.000 rg", Color::RGB::Blue.pdf_fill)
125
- assert_equal("0.000 1.000 0.000 rg", Color::RGB::Lime.pdf_fill)
126
- assert_equal("1.000 0.000 0.000 rg", Color::RGB::Red.pdf_fill)
127
- assert_equal("1.000 1.000 1.000 rg", Color::RGB::White.pdf_fill)
128
- assert_equal("0.000 0.000 0.000 RG", Color::RGB::Black.pdf_stroke)
129
- assert_equal("0.000 0.000 1.000 RG", Color::RGB::Blue.pdf_stroke)
130
- assert_equal("0.000 1.000 0.000 RG", Color::RGB::Lime.pdf_stroke)
131
- assert_equal("1.000 0.000 0.000 RG", Color::RGB::Red.pdf_stroke)
132
- assert_equal("1.000 1.000 1.000 RG", Color::RGB::White.pdf_stroke)
86
+ assert_in_tolerance(0.5, Color::RGB::Red.mix_with(Color::RGB::Blue, 50).r)
87
+ assert_in_tolerance(0.0, Color::RGB::Red.mix_with(Color::RGB::Blue, 50).g)
88
+ assert_in_tolerance(0.5, Color::RGB::Red.mix_with(Color::RGB::Blue, 50).b)
89
+ assert_in_tolerance(0.5, Color::RGB::Blue.mix_with(Color::RGB::Red, 50).r)
90
+ assert_in_tolerance(0.0, Color::RGB::Blue.mix_with(Color::RGB::Red, 50).g)
91
+ assert_in_tolerance(0.5, Color::RGB::Blue.mix_with(Color::RGB::Red, 50).b)
133
92
  end
134
93
 
135
94
  def test_to_cmyk
136
95
  assert_kind_of(Color::CMYK, Color::RGB::Black.to_cmyk)
137
- assert_equal(Color::CMYK.new(0, 0, 0, 100), Color::RGB::Black.to_cmyk)
138
- assert_equal(Color::CMYK.new(0, 0, 100, 0),
139
- Color::RGB::Yellow.to_cmyk)
140
- assert_equal(Color::CMYK.new(100, 0, 0, 0), Color::RGB::Cyan.to_cmyk)
141
- assert_equal(Color::CMYK.new(0, 100, 0, 0),
142
- Color::RGB::Magenta.to_cmyk)
143
- assert_equal(Color::CMYK.new(0, 100, 100, 0), Color::RGB::Red.to_cmyk)
144
- assert_equal(Color::CMYK.new(100, 0, 100, 0),
145
- Color::RGB::Lime.to_cmyk)
146
- assert_equal(Color::CMYK.new(100, 100, 0, 0),
147
- Color::RGB::Blue.to_cmyk)
148
- assert_equal(Color::CMYK.new(10.32, 60.52, 10.32, 39.47),
149
- Color::RGB::Purple.to_cmyk)
150
- assert_equal(Color::CMYK.new(10.90, 59.13, 59.13, 24.39),
151
- Color::RGB::Brown.to_cmyk)
152
- assert_equal(Color::CMYK.new(0, 63.14, 18.43, 0),
153
- Color::RGB::Carnation.to_cmyk)
154
- assert_equal(Color::CMYK.new(7.39, 62.69, 62.69, 37.32),
155
- Color::RGB::Cayenne.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)
156
107
  end
157
108
 
158
109
  def test_to_grayscale
159
- assert_kind_of(Color::GrayScale, Color::RGB::Black.to_grayscale)
160
- assert_equal(Color::GrayScale.from_fraction(0),
161
- Color::RGB::Black.to_grayscale)
162
- assert_equal(Color::GrayScale.from_fraction(0.5),
163
- Color::RGB::Yellow.to_grayscale)
164
- assert_equal(Color::GrayScale.from_fraction(0.5),
165
- Color::RGB::Cyan.to_grayscale)
166
- assert_equal(Color::GrayScale.from_fraction(0.5),
167
- Color::RGB::Magenta.to_grayscale)
168
- assert_equal(Color::GrayScale.from_fraction(0.5),
169
- Color::RGB::Red.to_grayscale)
170
- assert_equal(Color::GrayScale.from_fraction(0.5),
171
- Color::RGB::Lime.to_grayscale)
172
- assert_equal(Color::GrayScale.from_fraction(0.5),
173
- Color::RGB::Blue.to_grayscale)
174
- assert_equal(Color::GrayScale.from_fraction(0.2510),
175
- Color::RGB::Purple.to_grayscale)
176
- assert_equal(Color::GrayScale.new(40.58),
177
- Color::RGB::Brown.to_grayscale)
178
- assert_equal(Color::GrayScale.new(68.43),
179
- Color::RGB::Carnation.to_grayscale)
180
- assert_equal(Color::GrayScale.new(27.65),
181
- Color::RGB::Cayenne.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)
182
122
  end
183
123
 
184
124
  def test_to_hsl
185
125
  assert_kind_of(Color::HSL, Color::RGB::Black.to_hsl)
186
- assert_equal(Color::HSL.new, Color::RGB::Black.to_hsl)
187
- assert_equal(Color::HSL.new(60, 100, 50), Color::RGB::Yellow.to_hsl)
188
- assert_equal(Color::HSL.new(180, 100, 50), Color::RGB::Cyan.to_hsl)
189
- assert_equal(Color::HSL.new(300, 100, 50), Color::RGB::Magenta.to_hsl)
190
- assert_equal(Color::HSL.new(0, 100, 50), Color::RGB::Red.to_hsl)
191
- assert_equal(Color::HSL.new(120, 100, 50), Color::RGB::Lime.to_hsl)
192
- assert_equal(Color::HSL.new(240, 100, 50), Color::RGB::Blue.to_hsl)
193
- assert_equal(Color::HSL.new(300, 100, 25.10),
194
- Color::RGB::Purple.to_hsl)
195
- assert_equal(Color::HSL.new(0, 59.42, 40.59),
196
- Color::RGB::Brown.to_hsl)
197
- assert_equal(Color::HSL.new(317.5, 100, 68.43),
198
- Color::RGB::Carnation.to_hsl)
199
- assert_equal(Color::HSL.new(0, 100, 27.64),
200
- Color::RGB::Cayenne.to_hsl)
201
-
202
- assert_equal("hsl(0.00, 0.00%, 0.00%)", Color::RGB::Black.css_hsl)
203
- assert_equal("hsl(60.00, 100.00%, 50.00%)",
204
- Color::RGB::Yellow.css_hsl)
205
- assert_equal("hsl(180.00, 100.00%, 50.00%)", Color::RGB::Cyan.css_hsl)
206
- assert_equal("hsl(300.00, 100.00%, 50.00%)",
207
- Color::RGB::Magenta.css_hsl)
208
- assert_equal("hsl(0.00, 100.00%, 50.00%)", Color::RGB::Red.css_hsl)
209
- assert_equal("hsl(120.00, 100.00%, 50.00%)", Color::RGB::Lime.css_hsl)
210
- assert_equal("hsl(240.00, 100.00%, 50.00%)", Color::RGB::Blue.css_hsl)
211
- assert_equal("hsl(300.00, 100.00%, 25.10%)",
212
- Color::RGB::Purple.css_hsl)
213
- assert_equal("hsl(0.00, 59.42%, 40.59%)", Color::RGB::Brown.css_hsl)
214
- assert_equal("hsl(317.52, 100.00%, 68.43%)",
215
- Color::RGB::Carnation.css_hsl)
216
- assert_equal("hsl(0.00, 100.00%, 27.65%)", Color::RGB::Cayenne.css_hsl)
217
-
218
- assert_equal("hsla(0.00, 0.00%, 0.00%, 1.00)",
219
- Color::RGB::Black.css_hsla)
220
- assert_equal("hsla(60.00, 100.00%, 50.00%, 1.00)",
221
- Color::RGB::Yellow.css_hsla)
222
- assert_equal("hsla(180.00, 100.00%, 50.00%, 1.00)",
223
- Color::RGB::Cyan.css_hsla)
224
- assert_equal("hsla(300.00, 100.00%, 50.00%, 1.00)",
225
- Color::RGB::Magenta.css_hsla)
226
- assert_equal("hsla(0.00, 100.00%, 50.00%, 1.00)",
227
- Color::RGB::Red.css_hsla)
228
- assert_equal("hsla(120.00, 100.00%, 50.00%, 1.00)",
229
- Color::RGB::Lime.css_hsla)
230
- assert_equal("hsla(240.00, 100.00%, 50.00%, 1.00)",
231
- Color::RGB::Blue.css_hsla)
232
- assert_equal("hsla(300.00, 100.00%, 25.10%, 1.00)",
233
- Color::RGB::Purple.css_hsla)
234
- assert_equal("hsla(0.00, 59.42%, 40.59%, 1.00)",
235
- Color::RGB::Brown.css_hsla)
236
- assert_equal("hsla(317.52, 100.00%, 68.43%, 1.00)",
237
- Color::RGB::Carnation.css_hsla)
238
- assert_equal("hsla(0.00, 100.00%, 27.65%, 1.00)",
239
- Color::RGB::Cayenne.css_hsla)
240
-
241
- # The following tests a bug reported by Jean Krohn on 10 June 2006
242
- # where HSL conversion was not quite correct, resulting in a bad
243
- # round-trip.
244
- assert_equal("#008800", Color::RGB.from_html("#008800").to_hsl.html)
245
- refute_equal("#002288", Color::RGB.from_html("#008800").to_hsl.html)
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)
246
142
 
247
143
  # The following tests a bug reported by Adam Johnson on 29 October
248
144
  # 2010.
249
- hsl = Color::HSL.new(262, 67, 42)
145
+ hsl = Color::HSL.from_values(262, 67, 42)
250
146
  c = Color::RGB.from_fraction(0.34496, 0.1386, 0.701399).to_hsl
251
- assert_in_delta hsl.h, c.h, Color::COLOR_TOLERANCE, "Hue"
252
- assert_in_delta hsl.s, c.s, Color::COLOR_TOLERANCE, "Saturation"
253
- assert_in_delta hsl.l, c.l, Color::COLOR_TOLERANCE, "Luminance"
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"
254
150
  end
255
151
 
256
152
  def test_to_rgb
@@ -259,36 +155,31 @@ module TestColor
259
155
 
260
156
  def test_to_yiq
261
157
  assert_kind_of(Color::YIQ, Color::RGB::Black.to_yiq)
262
- assert_equal(Color::YIQ.new, Color::RGB::Black.to_yiq)
263
- assert_equal(Color::YIQ.new(88.6, 32.1, 0), Color::RGB::Yellow.to_yiq)
264
- assert_equal(Color::YIQ.new(70.1, 0, 0), Color::RGB::Cyan.to_yiq)
265
- assert_equal(Color::YIQ.new(41.3, 27.5, 52.3),
266
- Color::RGB::Magenta.to_yiq)
267
- assert_equal(Color::YIQ.new(29.9, 59.6, 21.2), Color::RGB::Red.to_yiq)
268
- assert_equal(Color::YIQ.new(58.7, 0, 0), Color::RGB::Lime.to_yiq)
269
- assert_equal(Color::YIQ.new(11.4, 0, 31.1), Color::RGB::Blue.to_yiq)
270
- assert_equal(Color::YIQ.new(20.73, 13.80, 26.25),
271
- Color::RGB::Purple.to_yiq)
272
- assert_equal(Color::YIQ.new(30.89, 28.75, 10.23),
273
- Color::RGB::Brown.to_yiq)
274
- assert_equal(Color::YIQ.new(60.84, 23.28, 27.29),
275
- Color::RGB::Carnation.to_yiq)
276
- assert_equal(Color::YIQ.new(16.53, 32.96, 11.72),
277
- Color::RGB::Cayenne.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)
278
169
  end
279
170
 
280
171
  def test_to_lab
281
172
  # Luminosity can be an absolute.
282
- assert_in_delta(0.0, Color::RGB::Black.to_lab[:L], Color::COLOR_TOLERANCE)
283
- assert_in_delta(100.0, Color::RGB::White.to_lab[:L], Color::COLOR_TOLERANCE)
173
+ assert_in_tolerance(0.0, Color::RGB::Black.to_lab.l)
174
+ assert_in_tolerance(100.0, Color::RGB::White.to_lab.l)
284
175
 
285
176
  # It's not really possible to have absolute
286
177
  # numbers here because of how L*a*b* works, but
287
178
  # negative/positive comparisons work
288
- assert(Color::RGB::Green.to_lab[:a] < 0)
289
- assert(Color::RGB::Magenta.to_lab[:a] > 0)
290
- assert(Color::RGB::Blue.to_lab[:b] < 0)
291
- assert(Color::RGB::Yellow.to_lab[:b] > 0)
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)
292
183
  end
293
184
 
294
185
  def test_closest_match
@@ -296,56 +187,31 @@ module TestColor
296
187
  match_from = [Color::RGB::Red, Color::RGB::Green, Color::RGB::Blue]
297
188
  assert_equal(Color::RGB::Blue, Color::RGB::Indigo.closest_match(match_from))
298
189
  # But fails if using the :just_noticeable difference.
299
- assert_nil(Color::RGB::Indigo.closest_match(match_from, :just_noticeable))
190
+ assert_nil(Color::RGB::Indigo.closest_match(match_from, threshold_distance: :just_noticeable))
300
191
 
301
192
  # Crimson & Firebrick are visually closer than DarkRed and Firebrick
302
193
  # (more precise match)
303
194
  match_from += [Color::RGB::DarkRed, Color::RGB::Crimson]
304
195
  assert_equal(Color::RGB::Crimson,
305
- Color::RGB::Firebrick.closest_match(match_from))
306
- # Specifying a threshold low enough will cause even that match to
307
- # fail, though.
308
- assert_nil(Color::RGB::Firebrick.closest_match(match_from, 8.0))
196
+ Color::RGB::Firebrick.closest_match(match_from))
197
+ # Specifying a threshold low enough will cause even that match to fail, though.
198
+ assert_nil(Color::RGB::Firebrick.closest_match(match_from, threshold_distance: 8.0))
309
199
  # If the match_from list is an empty array, it also returns nil
310
200
  assert_nil(Color::RGB::Red.closest_match([]))
311
201
 
312
202
  # RGB::Green is 0,128,0, so we'll pick something VERY close to it, visually
313
- jnd_green = Color::RGB.new(3, 132, 3)
314
- assert_equal(Color::RGB::Green,
315
- jnd_green.closest_match(match_from, :jnd))
203
+ jnd_green = Color::RGB.from_values(3, 132, 3)
204
+ assert_equal(Color::RGB::Green, jnd_green.closest_match(match_from, threshold_distance: :jnd))
316
205
  # And then something that's just barely out of the tolerance range
317
- diff_green = Color::RGB.new(9, 142, 9)
318
- assert_nil(diff_green.closest_match(match_from, :jnd))
319
- end
320
-
321
- def test_add
322
- white = Color::RGB::Cyan + Color::RGB::Yellow
323
- refute_nil(white)
324
- assert_equal(Color::RGB::White, white)
325
-
326
- c1 = Color::RGB.new(0x80, 0x80, 0x00)
327
- c2 = Color::RGB.new(0x45, 0x20, 0xf0)
328
- cr = Color::RGB.new(0xc5, 0xa0, 0xf0)
329
-
330
- assert_equal(cr, c1 + c2)
331
- end
332
-
333
- def test_subtract
334
- black = Color::RGB::LightCoral - Color::RGB::Honeydew
335
- assert_equal(Color::RGB::Black, black)
336
-
337
- c1 = Color::RGB.new(0x85, 0x80, 0x00)
338
- c2 = Color::RGB.new(0x40, 0x20, 0xf0)
339
- cr = Color::RGB.new(0x45, 0x60, 0x00)
340
-
341
- assert_equal(cr, c1 - c2)
206
+ diff_green = Color::RGB.from_values(9, 142, 9)
207
+ assert_nil(diff_green.closest_match(match_from, threshold_distance: :jnd))
342
208
  end
343
209
 
344
210
  def test_mean_grayscale
345
- c1 = Color::RGB.new(0x85, 0x80, 0x00)
346
- c1_max = c1.max_rgb_as_greyscale
347
- c1_max = c1.max_rgb_as_greyscale
348
- c1_result = Color::GrayScale.from_fraction(0x85 / 255.0)
211
+ c1 = Color::RGB.from_values(0x85, 0x80, 0x00)
212
+ c1.max_rgb_as_grayscale
213
+ c1_max = c1.max_rgb_as_grayscale
214
+ c1_result = Color::Grayscale.from_fraction(0x85 / 255.0)
349
215
 
350
216
  assert_equal(c1_result, c1_max)
351
217
  end
@@ -362,38 +228,36 @@ module TestColor
362
228
  end
363
229
 
364
230
  def test_by_hex
365
- assert_same(Color::RGB::Cyan, Color::RGB.by_hex('#0ff'))
366
- assert_same(Color::RGB::Cyan, Color::RGB.by_hex('#00ffff'))
231
+ assert_same(Color::RGB::Cyan, Color::RGB.by_hex("#0ff"))
232
+ assert_same(Color::RGB::Cyan, Color::RGB.by_hex("#00ffff"))
367
233
  assert_equal("RGB [#333333]", Color::RGB.by_hex("#333").inspect)
368
234
  assert_equal("RGB [#333333]", Color::RGB.by_hex("333").inspect)
369
235
  assert_raises(ArgumentError) { Color::RGB.by_hex("5555555") }
370
236
  assert_raises(ArgumentError) { Color::RGB.by_hex("#55555") }
371
- assert_equal(:boom, Color::RGB.by_hex('#55555') { :boom })
372
237
  end
373
238
 
374
239
  def test_by_name
375
- assert_same(Color::RGB::Cyan, Color::RGB.by_name('cyan'))
240
+ assert_same(Color::RGB::Cyan, Color::RGB.by_name("cyan"))
376
241
 
377
242
  fetch_error = if RUBY_VERSION < "1.9"
378
- IndexError
379
- else
380
- KeyError
381
- end
243
+ IndexError
244
+ else
245
+ KeyError
246
+ end
382
247
 
383
- assert_raises(fetch_error) { Color::RGB.by_name('cyanide') }
384
- assert_equal(:boom, Color::RGB.by_name('cyanide') { :boom })
248
+ assert_raises(fetch_error) { Color::RGB.by_name("cyanide") }
249
+ assert_equal(:boom, Color::RGB.by_name("cyanide") { :boom })
385
250
  end
386
251
 
387
252
  def test_by_css
388
- assert_same(Color::RGB::Cyan, Color::RGB.by_css('#0ff'))
389
- assert_same(Color::RGB::Cyan, Color::RGB.by_css('cyan'))
390
- assert_raises(ArgumentError) { Color::RGB.by_css('cyanide') }
391
- assert_equal(:boom, Color::RGB.by_css('cyanide') { :boom })
253
+ assert_same(Color::RGB::Cyan, Color::RGB.by_css("#0ff"))
254
+ assert_same(Color::RGB::Cyan, Color::RGB.by_css("cyan"))
255
+ assert_raises(ArgumentError) { Color::RGB.by_css("cyanide") }
392
256
  end
393
257
 
394
258
  def test_extract_colors
395
- assert_equal([ Color::RGB::BlanchedAlmond, Color::RGB::Cyan ],
396
- Color::RGB.extract_colors('BlanchedAlmond is a nice shade, but #00ffff is not.'))
259
+ assert_equal([Color::RGB::BlanchedAlmond, Color::RGB::Cyan],
260
+ Color::RGB.extract_colors("BlanchedAlmond is a nice shade, but #00ffff is not."))
397
261
  end
398
262
 
399
263
  def test_inspect
@@ -403,5 +267,69 @@ module TestColor
403
267
  assert_equal("RGB [#ff0000]", Color::RGB::Red.inspect)
404
268
  assert_equal("RGB [#ffffff]", Color::RGB::White.inspect)
405
269
  end
270
+
271
+ def test_delta_e2000
272
+ # test data:
273
+ # http://www.ece.rochester.edu/~gsharma/ciede2000/
274
+ # http://www.ece.rochester.edu/~gsharma/ciede2000/dataNprograms/CIEDE2000.xls
275
+
276
+ # this will also test to_degrees and to_radians by association
277
+
278
+ test_data = JSON.parse(File.read("test/fixtures/cielab.json")).map.with_index { |e, i|
279
+ {
280
+ i: i,
281
+ c1: Color::CIELAB.from_values(e["c1"]["L"].to_f, e["c1"]["a"].to_f, e["c1"]["b"].to_f),
282
+ c2: Color::CIELAB.from_values(e["c2"]["L"].to_f, e["c2"]["a"].to_f, e["c2"]["b"].to_f),
283
+ correct_delta: e["∂E2000"].to_f
284
+ }
285
+ }
286
+
287
+ test_data.each do |e|
288
+ e => c1:, c2:, correct_delta:
289
+
290
+ e2000_c1_c2 = c1.delta_e2000(c2)
291
+ e2000_c2_c1 = c2.delta_e2000(c1)
292
+
293
+ assert_in_tolerance e2000_c1_c2, e2000_c2_c1
294
+ assert_in_tolerance e2000_c1_c2, correct_delta
295
+ end
296
+ end
297
+
298
+ def test_contrast
299
+ data = [
300
+ [[171, 215, 103], [195, 108, 197], 0.18117],
301
+ [[223, 133, 234], [64, 160, 101], 0.229530],
302
+ [[7, 30, 49], [37, 225, 31], 0.377786],
303
+ [[65, 119, 130], [70, 63, 212], 0.10323],
304
+ [[211, 77, 232], [5, 113, 139], 0.233503],
305
+ [[166, 185, 41], [87, 193, 137], 0.07275],
306
+ [[1, 120, 37], [195, 70, 33], 0.1474640],
307
+ [[99, 206, 21], [228, 204, 155], 0.22611],
308
+ [[15, 18, 41], [90, 202, 208], 0.552434]
309
+ ]
310
+
311
+ data.each do |(c1, c2, value)|
312
+ c1 = Color::RGB.from_values(c1[0], c1[1], c1[2])
313
+ c2 = Color::RGB.from_values(c2[0], c2[1], c2[2])
314
+
315
+ assert_in_delta(0.0001, c1.contrast(c2), value)
316
+ assert_equal(c1.contrast(c2), c2.contrast(c1))
317
+ end
318
+ end
319
+
320
+ # # An RGB color round-tripped through CIELAB should still have more or less the same
321
+ # # RGB values, but this doesn't really work because the color math here is slightly
322
+ # # wrong.
323
+ # def test_to_lab_automated
324
+ # 10.times do |t|
325
+ # c1 = Color::RGB.from_values(rand(256), rand(256), rand(256))
326
+ # l1 = c1.to_lab
327
+ # c2 = l1.to_rgb
328
+ #
329
+ # assert_in_tolerance(c1.r, c2.r)
330
+ # assert_in_tolerance(c1.g, c2.g)
331
+ # assert_in_tolerance(c1.b, c2.b)
332
+ # end
333
+ # end
406
334
  end
407
335
  end
data/test/test_yiq.rb CHANGED
@@ -1,7 +1,7 @@
1
- # -*- ruby encoding: utf-8 -*-
1
+ # frozen_string_literal: true
2
2
 
3
- require 'color'
4
- require 'minitest_helper'
3
+ require "color"
4
+ require "minitest_helper"
5
5
 
6
6
  module TestColor
7
7
  class TestYIQ < Minitest::Test
@@ -10,33 +10,21 @@ module TestColor
10
10
  end
11
11
 
12
12
  def test_brightness
13
- assert_in_delta(0.1, @yiq.brightness, Color::COLOR_TOLERANCE)
13
+ assert_in_tolerance(0.1, @yiq.brightness)
14
14
  end
15
15
 
16
16
  def test_i
17
- assert_in_delta(0.2, @yiq.i, Color::COLOR_TOLERANCE)
18
- assert_in_delta(0.2, @yiq.i, Color::COLOR_TOLERANCE)
19
- @yiq.i = 0.5
20
- assert_in_delta(0.5, @yiq.i, Color::COLOR_TOLERANCE)
21
- @yiq.i = 5
22
- assert_in_delta(1.0, @yiq.i, Color::COLOR_TOLERANCE)
23
- @yiq.i = -5
24
- assert_in_delta(0.0, @yiq.i, Color::COLOR_TOLERANCE)
17
+ assert_in_tolerance(0.2, @yiq.i)
18
+ assert_in_tolerance(0.2, @yiq.i)
25
19
  end
26
20
 
27
21
  def test_q
28
- assert_in_delta(0.3, @yiq.q, Color::COLOR_TOLERANCE)
29
- assert_in_delta(0.3, @yiq.q, Color::COLOR_TOLERANCE)
30
- @yiq.q = 0.5
31
- assert_in_delta(0.5, @yiq.q, Color::COLOR_TOLERANCE)
32
- @yiq.q = 5
33
- assert_in_delta(1.0, @yiq.q, Color::COLOR_TOLERANCE)
34
- @yiq.q = -5
35
- assert_in_delta(0.0, @yiq.q, Color::COLOR_TOLERANCE)
22
+ assert_in_tolerance(0.3, @yiq.q)
23
+ assert_in_tolerance(0.3, @yiq.q)
36
24
  end
37
25
 
38
26
  def test_to_grayscale
39
- assert_equal(Color::GrayScale.new(0.1), @yiq.to_grayscale)
27
+ assert_equal(Color::Grayscale.from_fraction(0.1), @yiq.to_grayscale)
40
28
  end
41
29
 
42
30
  def test_to_yiq
@@ -44,18 +32,12 @@ module TestColor
44
32
  end
45
33
 
46
34
  def test_y
47
- assert_in_delta(0.1, @yiq.y, Color::COLOR_TOLERANCE)
48
- assert_in_delta(0.1, @yiq.y, Color::COLOR_TOLERANCE)
49
- @yiq.y = 0.5
50
- assert_in_delta(0.5, @yiq.y, Color::COLOR_TOLERANCE)
51
- @yiq.y = 5
52
- assert_in_delta(1.0, @yiq.y, Color::COLOR_TOLERANCE)
53
- @yiq.y = -5
54
- assert_in_delta(0.0, @yiq.y, Color::COLOR_TOLERANCE)
35
+ assert_in_tolerance(0.1, @yiq.y)
36
+ assert_in_tolerance(0.1, @yiq.y)
55
37
  end
56
38
 
57
39
  def test_inspect
58
- assert_equal("YIQ [10.00%, 20.00%, 30.00%]", @yiq.inspect)
40
+ assert_equal("YIQ [10.00% 20.00% 30.00%]", @yiq.inspect)
59
41
  end
60
42
  end
61
43
  end