red-colors 0.1.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.
data/test/test-husl.rb ADDED
@@ -0,0 +1,241 @@
1
+ class ColorsHUSLTest < Test::Unit::TestCase
2
+ include TestHelper
3
+
4
+ sub_test_case(".new") do
5
+ test("with integer values") do
6
+ c = Colors::HUSL.new(1, 128, 255)
7
+ assert_equal(1r, c.hue)
8
+ assert_equal(128/255r, c.saturation)
9
+ assert_equal(255/255r, c.lightness)
10
+
11
+ c = Colors::HUSL.new(-1, 128, 255)
12
+ assert_equal(359r, c.hue)
13
+
14
+ c = Colors::HUSL.new(361, 128, 255)
15
+ assert_equal(1r, c.hue)
16
+
17
+ assert_raise(ArgumentError) do
18
+ Colors::HUSL.new(0, 0x100, 0)
19
+ end
20
+
21
+ assert_raise(ArgumentError) do
22
+ Colors::HUSL.new(0, 0, 0x100)
23
+ end
24
+
25
+ assert_raise(ArgumentError) do
26
+ Colors::HUSL.new(0, -1, 0)
27
+ end
28
+
29
+ assert_raise(ArgumentError) do
30
+ Colors::HUSL.new(0, 0, -1)
31
+ end
32
+ end
33
+
34
+ test("with float values") do
35
+ c = Colors::HUSL.new(0.0.next_float, 0.55, 1)
36
+ assert_equal(0.0.next_float.to_r, c.hue)
37
+ assert_equal(0.55.to_r, c.saturation)
38
+ assert_equal(1.0.to_r, c.lightness)
39
+
40
+ c = Colors::HUSL.new(-0.1, 0.55, 1)
41
+ assert_equal(360 - 0.1, c.hue)
42
+
43
+ c = Colors::HUSL.new(360.1, 0.55, 1)
44
+ assert_equal(Rational(360.1) - 360, c.hue)
45
+
46
+ assert_raise(ArgumentError) do
47
+ Colors::HUSL.new(0.0, 1.0.next_float, 0.0)
48
+ end
49
+
50
+ assert_raise(ArgumentError) do
51
+ Colors::HUSL.new(0.0, 0.0, 1.0.next_float)
52
+ end
53
+
54
+ assert_raise(ArgumentError) do
55
+ Colors::HUSL.new(0, -0.1, 0)
56
+ end
57
+
58
+ assert_raise(ArgumentError) do
59
+ Colors::HUSL.new(0, 0, -0.1)
60
+ end
61
+ end
62
+
63
+ test("with rational values") do
64
+ c = Colors::HUSL.new(1r, 500/1000r, 1)
65
+ assert_equal(1r, c.hue)
66
+ assert_equal(500/1000r, c.saturation)
67
+ assert_equal(1r, c.lightness)
68
+
69
+ c = Colors::HUSL.new(-1r, 500/1000r, 1)
70
+ assert_equal(359r, c.hue)
71
+
72
+ c = Colors::HUSL.new(361r, 500/1000r, 1)
73
+ assert_equal(1r, c.hue)
74
+
75
+ assert_raise(ArgumentError) do
76
+ Colors::HUSL.new(0r, 1001/1000r, 0r)
77
+ end
78
+
79
+ assert_raise(ArgumentError) do
80
+ Colors::HUSL.new(0r, 0r, 1001/1000r)
81
+ end
82
+
83
+ assert_raise(ArgumentError) do
84
+ Colors::HUSL.new(0r, -1/1000r, 0r)
85
+ end
86
+
87
+ assert_raise(ArgumentError) do
88
+ Colors::HUSL.new(0r, 0r, -1/1000r)
89
+ end
90
+ end
91
+ end
92
+
93
+ test("#hue=") do
94
+ c = Colors::HUSL.new(0, 0, 0)
95
+ c.hue = 1r
96
+ assert_equal(1r, c.hue)
97
+ c.hue = 1.0r
98
+ assert_equal(1r, c.hue)
99
+ c.hue = 1
100
+ assert_equal(1r, c.hue)
101
+ c.hue = -1
102
+ assert_equal(359r, c.hue)
103
+ c.hue = 360
104
+ assert_equal(0r, c.hue)
105
+ c.hue = 361
106
+ assert_equal(1r, c.hue)
107
+ end
108
+
109
+ test("#saturation=") do
110
+ c = Colors::HUSL.new(0, 0, 0)
111
+ c.saturation = 1r
112
+ assert_equal(1r, c.saturation)
113
+ c.saturation = 1.0r
114
+ assert_equal(1r, c.saturation)
115
+ c.saturation = 1
116
+ assert_equal(1/255r, c.saturation)
117
+ assert_raise(ArgumentError) do
118
+ c.saturation = 1001/1000r
119
+ end
120
+ assert_raise(ArgumentError) do
121
+ c.saturation = -1/1000r
122
+ end
123
+ assert_raise(ArgumentError) do
124
+ c.saturation = -0.1
125
+ end
126
+ assert_raise(ArgumentError) do
127
+ c.saturation = 1.0.next_float
128
+ end
129
+ assert_raise(ArgumentError) do
130
+ c.saturation = 256
131
+ end
132
+ assert_raise(ArgumentError) do
133
+ c.saturation = -1
134
+ end
135
+ end
136
+
137
+ test("#lightness=") do
138
+ c = Colors::HUSL.new(0, 0, 0)
139
+ c.lightness = 1r
140
+ assert_equal(1r, c.lightness)
141
+ c.lightness = 1.0r
142
+ assert_equal(1r, c.lightness)
143
+ c.lightness = 1
144
+ assert_equal(1/255r, c.lightness)
145
+ assert_raise(ArgumentError) do
146
+ c.lightness = 1001/1000r
147
+ end
148
+ assert_raise(ArgumentError) do
149
+ c.lightness = -1/1000r
150
+ end
151
+ assert_raise(ArgumentError) do
152
+ c.lightness = -0.1
153
+ end
154
+ assert_raise(ArgumentError) do
155
+ c.lightness = 1.0.next_float
156
+ end
157
+ assert_raise(ArgumentError) do
158
+ c.lightness = 256
159
+ end
160
+ assert_raise(ArgumentError) do
161
+ c.lightness = -1
162
+ end
163
+ end
164
+
165
+ test("==") do
166
+ assert { Colors::HUSL.new(0, 0, 0) == Colors::HUSL.new(0, 0, 0) }
167
+ #assert { Colors::HUSL.new(0, 0, 0) == Colors::HUSLA.new(0, 0, 0, 1r) }
168
+ end
169
+
170
+ test("!=") do
171
+ assert { Colors::HUSL.new(0, 0, 0) != Colors::HUSL.new(1, 0, 0) }
172
+ assert { Colors::HUSL.new(0, 0, 0) != Colors::HUSL.new(0, 1, 0) }
173
+ assert { Colors::HUSL.new(0, 0, 0) != Colors::HUSL.new(0, 0, 1) }
174
+ #assert { Colors::HUSL.new(0, 0, 0) != Colors::HUSLA.new(0, 0, 0, 0) }
175
+ end
176
+
177
+ test("#desaturate") do
178
+ c = Colors::HUSL.from_rgb(1r, 1r, 0r).desaturate(0.8)
179
+ assert_instance_of(Colors::HUSL, c)
180
+ assert_near(Colors::HUSL.new(85.87432021817473r, 0.9838589961976354r, 0.8850923805142681r), c)
181
+ end
182
+
183
+ test("to_husl") do
184
+ black = Colors::HUSL.new(0, 0, 0)
185
+ assert_same(black, black.to_hsl)
186
+ end
187
+
188
+ test(".from_rgb") do
189
+ # black
190
+ assert_equal(Colors::HUSL.new(0, 0, 0),
191
+ Colors::HUSL.from_rgb(0, 0, 0))
192
+ # red
193
+ assert_near(Colors::HUSL.new(12.177050630061776r, 1r, 0.5323711559542933r),
194
+ Colors::HUSL.from_rgb(1r, 0, 0))
195
+ ## yellow
196
+ assert_near(Colors::HUSL.new(85.87432021817473r, 1r, 0.9713855934179674r),
197
+ Colors::HUSL.from_rgb(1r, 1r, 0))
198
+ ## green
199
+ assert_near(Colors::HUSL.new(127.71501294924047r, 1r, 0.8773551910965973r),
200
+ Colors::HUSL.from_rgb(0r, 1r, 0))
201
+ ## cyan
202
+ assert_near(Colors::HUSL.new(192.17705063006116r, 1r, 0.9111475231670507r),
203
+ Colors::HUSL.from_rgb(0r, 1r, 1r))
204
+ ## blue
205
+ assert_near(Colors::HUSL.new(265.8743202181779r, 1r, 0.3230087290398002r),
206
+ Colors::HUSL.from_rgb(0r, 0r, 1r))
207
+ ## magenta
208
+ assert_near(Colors::HUSL.new(307.7150129492436r, 1r, 0.60322731354551294r),
209
+ Colors::HUSL.from_rgb(1r, 0r, 1r))
210
+ ## white
211
+ assert_near(Colors::HUSL.new(0r, 0r, 1r),
212
+ Colors::HUSL.from_rgb(1r, 1r, 1r))
213
+ end
214
+
215
+ test("to_rgb") do
216
+ # black
217
+ assert_equal(Colors::RGB.new(0, 0, 0),
218
+ Colors::HUSL.new(0, 0, 0).to_rgb)
219
+ # red
220
+ assert_near(Colors::RGB.new(1r, 0, 0),
221
+ Colors::HUSL.new(12.177050630061776r, 1r, 0.5323711559542933r).to_rgb)
222
+ # yellow
223
+ assert_near(Colors::RGB.new(1r, 1r, 0),
224
+ Colors::HUSL.new(85.87432021817473r, 1r, 0.9713855934179674r).to_rgb)
225
+ # green
226
+ assert_near(Colors::RGB.new(0r, 1r, 0),
227
+ Colors::HUSL.new(127.71501294924047r, 1r, 0.8773551910965973r).to_rgb)
228
+ # cyan
229
+ assert_near(Colors::RGB.new(0r, 1r, 1r),
230
+ Colors::HUSL.new(192.17705063006116r, 1r, 0.9111475231670507r).to_rgb)
231
+ # blue
232
+ assert_near(Colors::RGB.new(0r, 0r, 1r),
233
+ Colors::HUSL.new(265.8743202181779r, 1r, 0.3230087290398002r).to_rgb)
234
+ # magenta
235
+ assert_near(Colors::RGB.new(1r, 0r, 1r),
236
+ Colors::HUSL.new(307.7150129492436r, 1r, 0.60322731354551294r).to_rgb)
237
+ # white
238
+ assert_near(Colors::RGB.new(1r, 1r, 1r),
239
+ Colors::HUSL.new(0r, 1r, 1r).to_rgb)
240
+ end
241
+ end
@@ -0,0 +1,43 @@
1
+ class ColorsNamedColorTest < Test::Unit::TestCase
2
+ sub_test_case("Colors[]") do
3
+ Colors::ColorData::DEFAULT_COLOR_CYCLE.each_with_index do |c, i|
4
+ name = "C#{i}"
5
+ expected = Colors::RGB.parse(c)
6
+ data("Color cycle: #{name}=#{c}", [name, expected])
7
+ end
8
+ Colors::ColorData::BASE_COLORS.each do |name, (r, g, b)|
9
+ expected = Colors::RGB.new(r, g, b)
10
+ data("Base color: #{name}=(#{r}, #{g}, #{b})", [name, expected])
11
+ end
12
+ Colors::ColorData::TABLEAU_COLORS.each do |name, hex_string|
13
+ expected = Colors::RGB.parse(hex_string)
14
+ data("Tableau color: #{name}=#{hex_string}", [name, expected])
15
+ if name.include? "gray"
16
+ name = name.sub("gray", "grey")
17
+ data("Tableau color: #{name}=#{hex_string}", [name, expected])
18
+ end
19
+ end
20
+ Colors::ColorData::CSS4_COLORS.each do |name, hex_string|
21
+ expected = Colors::RGB.parse(hex_string)
22
+ data("CSS4 color: #{name}=#{hex_string}", [name, expected])
23
+ end
24
+ Colors::ColorData::XKCD_COLORS.each do |name, hex_string|
25
+ expected = Colors::RGB.parse(hex_string)
26
+ data("XKCD color: #{name}=#{hex_string}", [name, expected])
27
+ if name.include? "grey"
28
+ name = name.sub("grey", "gray")
29
+ data("Tableau color: #{name}=#{hex_string}", [name, expected])
30
+ end
31
+ end
32
+ def test_lookup_named_color(data)
33
+ name, expected = data
34
+ assert_equal(expected, Colors[name])
35
+ end
36
+ end
37
+
38
+ test("Colors::NamedColors.nth_color?") do
39
+ assert do
40
+ Colors::NamedColors.nth_color?("C1")
41
+ end
42
+ end
43
+ end
data/test/test-rgb.rb ADDED
@@ -0,0 +1,294 @@
1
+ class ColorsRGBTest < Test::Unit::TestCase
2
+ include TestHelper
3
+
4
+ sub_test_case(".new") do
5
+ test("with integer values") do
6
+ c = Colors::RGB.new(1, 128, 255)
7
+ assert_equal(1/255r, c.red)
8
+ assert_equal(128/255r, c.green)
9
+ assert_equal(255/255r, c.blue)
10
+
11
+ assert_raise(ArgumentError) do
12
+ Colors::RGB.new(0, 0, 0x100)
13
+ end
14
+
15
+ assert_raise(ArgumentError) do
16
+ Colors::RGB.new(0, 0, -1)
17
+ end
18
+ end
19
+
20
+ test("with float values") do
21
+ c = Colors::RGB.new(0.0.next_float, 0.55, 1)
22
+ assert_equal(0.0.next_float.to_r, c.red)
23
+ assert_equal(0.55.to_r, c.green)
24
+ assert_equal(1.0.to_r, c.blue)
25
+
26
+ assert_raise(ArgumentError) do
27
+ Colors::RGB.new(0.0, 0.0, 1.0.next_float)
28
+ end
29
+
30
+ assert_raise(ArgumentError) do
31
+ Colors::RGB.new(0, 0, -0.1)
32
+ end
33
+ end
34
+
35
+ test("with rational values") do
36
+ c = Colors::RGB.new(1/1000r, 500/1000r, 1)
37
+ assert_equal(1/1000r, c.red)
38
+ assert_equal(500/1000r, c.green)
39
+ assert_equal(1r, c.blue)
40
+
41
+ assert_raise(ArgumentError) do
42
+ Colors::RGB.new(0.0, 0.0, 1001/1000r)
43
+ end
44
+
45
+ assert_raise(ArgumentError) do
46
+ Colors::RGB.new(0, 0, -1/1000r)
47
+ end
48
+ end
49
+ end
50
+
51
+ test("#red=") do
52
+ c = Colors::RGB.new(0, 0, 0)
53
+ c.red = 1r
54
+ assert_equal(1r, c.red)
55
+ c.red = 1.0r
56
+ assert_equal(1r, c.red)
57
+ c.red = 1
58
+ assert_equal(1/255r, c.red)
59
+ assert_raise(ArgumentError) do
60
+ c.red = 1001/1000r
61
+ end
62
+ assert_raise(ArgumentError) do
63
+ c.red = -1/1000r
64
+ end
65
+ assert_raise(ArgumentError) do
66
+ c.red = -0.1
67
+ end
68
+ assert_raise(ArgumentError) do
69
+ c.red = 1.0.next_float
70
+ end
71
+ assert_raise(ArgumentError) do
72
+ c.red = 256
73
+ end
74
+ assert_raise(ArgumentError) do
75
+ c.red = -1
76
+ end
77
+ end
78
+
79
+ test("#green=") do
80
+ c = Colors::RGB.new(0, 0, 0)
81
+ c.green = 1r
82
+ assert_equal(1r, c.green)
83
+ c.green = 1.0r
84
+ assert_equal(1r, c.green)
85
+ c.green = 1
86
+ assert_equal(1/255r, c.green)
87
+ assert_raise(ArgumentError) do
88
+ c.green = 1001/1000r
89
+ end
90
+ assert_raise(ArgumentError) do
91
+ c.green = -1/1000r
92
+ end
93
+ assert_raise(ArgumentError) do
94
+ c.green = -0.1
95
+ end
96
+ assert_raise(ArgumentError) do
97
+ c.green = 1.0.next_float
98
+ end
99
+ assert_raise(ArgumentError) do
100
+ c.green = 256
101
+ end
102
+ assert_raise(ArgumentError) do
103
+ c.green = -1
104
+ end
105
+ end
106
+
107
+ test("#blue=") do
108
+ c = Colors::RGB.new(0, 0, 0)
109
+ c.blue = 1r
110
+ assert_equal(1r, c.blue)
111
+ c.blue = 1.0r
112
+ assert_equal(1r, c.blue)
113
+ c.blue = 1
114
+ assert_equal(1/255r, c.blue)
115
+ assert_raise(ArgumentError) do
116
+ c.blue = 1001/1000r
117
+ end
118
+ assert_raise(ArgumentError) do
119
+ c.blue = -1/1000r
120
+ end
121
+ assert_raise(ArgumentError) do
122
+ c.blue = -0.1
123
+ end
124
+ assert_raise(ArgumentError) do
125
+ c.blue = 1.0.next_float
126
+ end
127
+ assert_raise(ArgumentError) do
128
+ c.blue = 256
129
+ end
130
+ assert_raise(ArgumentError) do
131
+ c.blue = -1
132
+ end
133
+ end
134
+
135
+ test("==") do
136
+ assert { Colors::RGB.new(0, 0, 0) == Colors::RGB.new(0, 0, 0) }
137
+ assert { Colors::RGB.new(0, 0, 0) == Colors::RGBA.new(0, 0, 0, 1r) }
138
+ end
139
+
140
+ test("!=") do
141
+ assert { Colors::RGB.new(0, 0, 0) != Colors::RGB.new(1, 0, 0) }
142
+ assert { Colors::RGB.new(0, 0, 0) != Colors::RGB.new(0, 1, 0) }
143
+ assert { Colors::RGB.new(0, 0, 0) != Colors::RGB.new(0, 0, 1) }
144
+ assert { Colors::RGB.new(0, 0, 0) != Colors::RGBA.new(0, 0, 0, 0) }
145
+ end
146
+
147
+ test("#desaturate") do
148
+ c = Colors::RGB.new(1r, 1r, 1r).desaturate(0.8)
149
+ assert_instance_of(Colors::RGB, c)
150
+ assert_near(Colors::HSL.new(0r, 0.8r, 1r).to_rgb, c)
151
+ end
152
+
153
+ sub_test_case(".parse") do
154
+ test("for #rgb") do
155
+ assert_equal(Colors::RGB.new(0, 0, 0),
156
+ Colors::RGB.parse("#000"))
157
+ assert_equal(Colors::RGB.new(0x33, 0x66, 0x99),
158
+ Colors::RGB.parse("#369"))
159
+ assert_equal(Colors::RGB.new(255, 255, 255),
160
+ Colors::RGB.parse("#fff"))
161
+ end
162
+
163
+ test("for #rrggbb") do
164
+ assert_equal(Colors::RGB.new(0, 0, 0),
165
+ Colors::RGB.parse("#000000"))
166
+ assert_equal(Colors::RGB.new(1, 0, 0),
167
+ Colors::RGB.parse("#010000"))
168
+ assert_equal(Colors::RGB.new(0, 1, 0),
169
+ Colors::RGB.parse("#000100"))
170
+ assert_equal(Colors::RGB.new(0, 0, 1),
171
+ Colors::RGB.parse("#000001"))
172
+ end
173
+
174
+
175
+ test("error cases") do
176
+ # `#rgba` is error
177
+ assert_raise(ArgumentError) do
178
+ Colors::RGB.parse("#0000")
179
+ end
180
+
181
+ # `#rrggbbaa` is error
182
+ assert_raise(ArgumentError) do
183
+ Colors::RGB.parse("#00000000")
184
+ end
185
+
186
+ assert_raise(ArgumentError) do
187
+ Colors::RGB.parse("#00")
188
+ end
189
+
190
+ assert_raise(ArgumentError) do
191
+ Colors::RGB.parse("#00000")
192
+ end
193
+
194
+ assert_raise(ArgumentError) do
195
+ Colors::RGB.parse("#0000000")
196
+ end
197
+
198
+ assert_raise(ArgumentError) do
199
+ Colors::RGB.parse(nil)
200
+ end
201
+
202
+ assert_raise(ArgumentError) do
203
+ Colors::RGB.parse(1)
204
+ end
205
+
206
+ assert_raise(ArgumentError) do
207
+ Colors::RGB.parse("")
208
+ end
209
+
210
+ assert_raise(ArgumentError) do
211
+ Colors::RGB.parse("333")
212
+ end
213
+
214
+ assert_raise(ArgumentError) do
215
+ Colors::RGB.parse("#xxx")
216
+ end
217
+ end
218
+ end
219
+
220
+ test("#to_hex_string") do
221
+ assert_equal("#000000",
222
+ Colors::RGB.new(0, 0, 0).to_hex_string)
223
+ assert_equal("#ff0000",
224
+ Colors::RGB.new(1r, 0, 0).to_hex_string)
225
+ assert_equal("#00ff00",
226
+ Colors::RGB.new(0, 1r, 0).to_hex_string)
227
+ assert_equal("#0000ff",
228
+ Colors::RGB.new(0, 0, 1r).to_hex_string)
229
+ assert_equal("#ffffff",
230
+ Colors::RGB.new(1r, 1r, 1r).to_hex_string)
231
+ assert_equal("#808080",
232
+ Colors::RGB.new(0.5, 0.5, 0.5).to_hex_string)
233
+ assert_equal("#333333",
234
+ Colors::RGB.new(0x33, 0x33, 0x33).to_hex_string)
235
+ end
236
+
237
+ test("to_rgb") do
238
+ black = Colors::RGB.new(0, 0, 0)
239
+ assert_same(black, black.to_rgb)
240
+ end
241
+
242
+ test("#to_rgba") do
243
+ black = Colors::RGB.new(0, 0, 0)
244
+ assert_equal(Colors::RGBA.new(0, 0, 0, 255),
245
+ black.to_rgba)
246
+ assert_equal(Colors::RGBA.new(0, 0, 0, 0),
247
+ black.to_rgba(alpha: 0))
248
+ assert_equal(Colors::RGBA.new(0, 0, 0, 0.5),
249
+ black.to_rgba(alpha: 0.5))
250
+
251
+ assert_raise(ArgumentError) do
252
+ black.to_rgba(alpha: nil)
253
+ end
254
+
255
+ assert_raise(ArgumentError) do
256
+ black.to_rgba(alpha: 256)
257
+ end
258
+
259
+ assert_raise(ArgumentError) do
260
+ black.to_rgba(alpha: -0.1)
261
+ end
262
+
263
+ assert_raise(ArgumentError) do
264
+ black.to_rgba(alpha: 1.0.next_float)
265
+ end
266
+ end
267
+
268
+ test("#to_hsl") do
269
+ # black
270
+ assert_equal(Colors::HSL.new(0r, 0r, 0r),
271
+ Colors::RGB.new(0r, 0r, 0r).to_hsl)
272
+ # red
273
+ assert_equal(Colors::HSL.new(0r, 1r, 0.5r),
274
+ Colors::RGB.new(1r, 0r, 0r).to_hsl)
275
+ # yellow
276
+ assert_equal(Colors::HSL.new(60r, 1r, 0.5r),
277
+ Colors::RGB.new(1r, 1r, 0r).to_hsl)
278
+ # green
279
+ assert_equal(Colors::HSL.new(120r, 1r, 0.5r),
280
+ Colors::RGB.new(0r, 1r, 0r).to_hsl)
281
+ # cyan
282
+ assert_equal(Colors::HSL.new(180r, 1r, 0.5r),
283
+ Colors::RGB.new(0r, 1r, 1r).to_hsl)
284
+ # blue
285
+ assert_equal(Colors::HSL.new(240r, 1r, 0.5r),
286
+ Colors::RGB.new(0r, 0r, 1r).to_hsl)
287
+ # magenta
288
+ assert_equal(Colors::HSL.new(300r, 1r, 0.5r),
289
+ Colors::RGB.new(1r, 0r, 1r).to_hsl)
290
+ # white
291
+ assert_equal(Colors::HSL.new(0r, 0r, 1r),
292
+ Colors::RGB.new(1r, 1r, 1r).to_hsl)
293
+ end
294
+ end