-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgrapefruit_test.py
529 lines (445 loc) · 21.8 KB
/
grapefruit_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#!/usr/bin/python
# -*- coding: utf-8 -*-#
# Copyright (c) 2008-2016, Xavier Basty
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit tests for the grapefruit module."""
import grapefruit
from nose.tools import *
def assert_items_almost_equal(first, second, places=3, msg=None, delta=None):
"""assert_almost_equal for iterables"""
assert_equal(len(first), len(second))
for f, s in zip(first, second):
assert_almost_equal(f, s, places, msg, delta)
class TestConversion():
"""Test the static color conversion methods."""
def test_rgb_to_hsl_tuple(self):
assert_items_almost_equal((30.0, 1.0, 0.5), grapefruit.rgb_to_hsl((1, 0.5, 0)))
def test_rgb_to_hsl(self):
assert_items_almost_equal((30.0, 1.0, 0.5), grapefruit.rgb_to_hsl(1, 0.5, 0))
assert_items_almost_equal((20.0, 1.0, 0.625), grapefruit.rgb_to_hsl(1, 0.5, 0.25)) #ff8040
assert_items_almost_equal((40.0, 1.0, 0.375), grapefruit.rgb_to_hsl(0.75, 0.5, 0)) #bf8000
def test_hsl_to_rgb_tuple(self):
assert_items_almost_equal((1, 0.5, 0), grapefruit.hsl_to_rgb(((30.0, 1.0, 0.5))))
def test_hsl_to_rgb(self):
assert_items_almost_equal((1, 0.5, 0), grapefruit.hsl_to_rgb(30.0, 1.0, 0.5))
assert_items_almost_equal((1, 0.5, 0.25), grapefruit.hsl_to_rgb(20.0, 1.0, 0.625))
assert_items_almost_equal((0.75, 0.5, 0), grapefruit.hsl_to_rgb(40.0, 1.0, 0.375))
def test_rgb_to_hsv_tuple(self):
assert_equal((30.0, 1.0, 1.0), grapefruit.rgb_to_hsv((1, 0.5, 0)))
def test_rgb_to_hsv(self):
assert_equal((30.0, 1.0, 1.0), grapefruit.rgb_to_hsv(1, 0.5, 0))
assert_equal((1, 0.5, 0), grapefruit.hsv_to_rgb(30.0, 1.0, 1.0))
def test_rgb_to_yiq_tuple(self):
assert_items_almost_equal((0.5923, 0.4589, -0.05), grapefruit.rgb_to_yiq((1, 0.5, 0)))
def test_rgb_to_yiq(self):
assert_items_almost_equal((0.5923, 0.4589, -0.05), grapefruit.rgb_to_yiq(1, 0.5, 0))
assert_items_almost_equal((1, 0.5, 0), grapefruit.yiq_to_rgb(0.5923, 0.4589, -0.05))
def test_rgb_to_yuv_tuple(self):
assert_items_almost_equal((0.5925, -0.2916, 0.3575), grapefruit.rgb_to_yuv((1, 0.5, 0)))
def test_rgb_to_yuv(self):
assert_items_almost_equal((0.5925, -0.2916, 0.3575), grapefruit.rgb_to_yuv(1, 0.5, 0))
assert_items_almost_equal((1, 0.5, 0), grapefruit.yuv_to_rgb(0.5925, -0.2916, 0.3575))
def test_rgb_to_xyz_tuple(self):
assert_items_almost_equal((0.4890, 0.3657, 0.04485), grapefruit.rgb_to_xyz((1, 0.5, 0)))
def test_rgb_to_xyz(self):
assert_items_almost_equal((0.4890, 0.3657, 0.04485), grapefruit.rgb_to_xyz(1, 0.5, 0))
assert_items_almost_equal((1, 0.5, 0), grapefruit.xyz_to_rgb(0.488941, 0.365682, 0.0448137))
def test_xyz_tolab_tuple(self):
assert_items_almost_equal((66.9518, 0.4308, 0.7397), grapefruit.xyz_to_lab((0.488941, 0.365682, 0.0448137)))
def test_xyz_tolab(self):
assert_items_almost_equal((66.9518, 0.4308, 0.7397), grapefruit.xyz_to_lab(0.488941, 0.365682, 0.0448137))
assert_items_almost_equal((66.9518, 0.4117, 0.6728), grapefruit.xyz_to_lab(0.488941, 0.365682, 0.0448137, grapefruit.WHITE_REFERENCE["std_D50"]))
def test_xyz_to_lab_tuple(self):
assert_items_almost_equal((0.4890, 0.3657, 0.0449), grapefruit.lab_to_xyz((66.9518, 0.4308, 0.7397)))
def test_xyz_to_lab(self):
assert_items_almost_equal((0.4890, 0.3657, 0.0449), grapefruit.lab_to_xyz(66.9518, 0.4308, 0.7397))
assert_items_almost_equal((0.4890, 0.3657, 0.0449), grapefruit.lab_to_xyz(66.9518, 0.4117, 0.6728, grapefruit.WHITE_REFERENCE["std_D50"]))
def test_cmy_to_cmyk_tuple(self):
assert_items_almost_equal((1, 0.32, 0, 0.5), grapefruit.cmy_to_cmyk((1.0, 0.66, 0.5)))
def test_cmy_to_cmyk(self):
assert_items_almost_equal((1, 0.32, 0, 0.5), grapefruit.cmy_to_cmyk(1.0, 0.66, 0.5))
def test_cmyk_to_cmy_tuple(self):
assert_items_almost_equal((1.0, 0.66, 0.5), grapefruit.cmyk_to_cmy((1, 0.32, 0, 0.5)))
def test_cmyk_to_cmy(self):
assert_items_almost_equal((1.0, 0.66, 0.5), grapefruit.cmyk_to_cmy(1, 0.32, 0, 0.5))
def test_rgb_to_cmy_tuple(self):
assert_equal((0, 0.5, 1), grapefruit.rgb_to_cmy((1, 0.5, 0)))
def test_rgb_to_cmy(self):
assert_equal((0, 0.5, 1), grapefruit.rgb_to_cmy(1, 0.5, 0))
assert_equal((1, 0.5, 0), grapefruit.cmy_to_rgb(0, 0.5, 1))
def test_rgb_to_html(self):
assert_equal("#ff8000", grapefruit.rgb_to_html(1, 0.5, 0))
assert_items_almost_equal((1.0, 0.5020, 0.0), grapefruit.html_to_rgb("#ff8000"))
assert_items_almost_equal((1.0, 0.5020, 0.0), grapefruit.html_to_rgb("ff8000"))
assert_items_almost_equal((1.0, 0.4, 0.0), grapefruit.html_to_rgb("#f60"))
assert_items_almost_equal((1.0, 0.4, 0.0), grapefruit.html_to_rgb("f60"))
assert_items_almost_equal((1.000000, 0.980392, 0.803922), grapefruit.html_to_rgb("lemonchiffon"))
def test_rgb_to_pil_tuple(self):
assert_almost_equal(0x0080ff, grapefruit.rgb_to_pil((1, 0.5, 0)))
def test_rgb_to_pil(self):
assert_almost_equal(0x0080ff, grapefruit.rgb_to_pil(1, 0.5, 0))
assert_items_almost_equal((1.0, 0.5020, 0), grapefruit.pil_to_rgb(0x0080ff))
def test_websafe_component(self):
assert_equal(0.2, grapefruit._websafe_component(0.2))
assert_equal(0.2, grapefruit._websafe_component(0.25))
assert_equal(0.4, grapefruit._websafe_component(0.3))
assert_equal(0.4, grapefruit._websafe_component(0.25, True))
assert_equal(0.2, grapefruit._websafe_component(0.2, True))
assert_equal(0.2, grapefruit._websafe_component(0.3, True))
def test_rgb_to_to_websafe_tuple(self):
assert_equal((1.0, 0.6, 0.0), grapefruit.rgb_to_websafe((1, 0.55, 0.0)))
def test_rgb_to_to_websafe(self):
assert_equal((1.0, 0.6, 0.0), grapefruit.rgb_to_websafe(1, 0.55, 0.0))
assert_equal((1.0, 0.4, 0.0), grapefruit.rgb_to_websafe(1, 0.55, 0.0, True))
assert_equal((1.0, 0.4, 0.0), grapefruit.rgb_to_websafe(1, 0.5, 0.0, True))
def test_rgb_to_greyscale_tuple(self):
assert_equal((0.6, 0.6, 0.6), grapefruit.rgb_to_greyscale((1, 0.8, 0)))
def test_rgb_to_greyscale(self):
assert_equal((0.6, 0.6, 0.6), grapefruit.rgb_to_greyscale(1, 0.8, 0))
class TestNewFrom():
def test_from_rgb(self):
c = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(c, (1.0, 0.5, 0.0, 1.0))
c = grapefruit.Color.from_rgb(1.0, 0.5, 0.0, 0.5)
assert_equal(c, (1.0, 0.5, 0.0, 0.5))
def test_from_hsl(self):
c = grapefruit.Color.from_hsl(30, 1, 0.5)
assert_equal(c, (1.0, 0.5, 0.0, 1.0))
c = grapefruit.Color.from_hsl(30, 1, 0.5, 0.5)
assert_equal(c, (1.0, 0.5, 0.0, 0.5))
def test_from_hsv(self):
c = grapefruit.Color.from_hsv(30, 1, 1)
assert_equal(c, (1.0, 0.5, 0.0, 1.0))
c = grapefruit.Color.from_hsv(30, 1, 1, 0.5)
assert_equal(c, (1.0, 0.5, 0.0, 0.5))
def test_from_yiq(self):
c = grapefruit.Color.from_yiq(0.5923, 0.4589, -0.0499818)
assert_items_almost_equal(c, (1, 0.5, 0, 1))
c = grapefruit.Color.from_yiq(0.5923, 0.4589,-0.05, 0.5)
assert_items_almost_equal(c, (1, 0.5, 0, 0.5))
def test_from_yuv(self):
c = grapefruit.Color.from_yuv(0.5925, -0.2916, 0.3575)
assert_items_almost_equal(c, (1, 0.5, 0, 1))
c = grapefruit.Color.from_yuv(0.5925, -0.2916, 0.3575, 0.5)
assert_items_almost_equal(c, (1, 0.5, 0, 0.5))
def test_from_xyz(self):
c = grapefruit.Color.from_xyz(0.488941, 0.365682, 0.0448137)
assert_items_almost_equal(c, (1, 0.5, 0, 1))
c = grapefruit.Color.from_xyz(0.488941, 0.365682, 0.0448137, 0.5)
assert_items_almost_equal(c, (1, 0.5, 0, 0.5))
def test_from_lab(self):
c = grapefruit.Color.from_lab(66.9518, 0.43084, 0.739692)
assert_items_almost_equal(c, (1, 0.5, 0, 1))
c = grapefruit.Color.from_lab(66.9518, 0.43084, 0.739692, wref=grapefruit.WHITE_REFERENCE["std_D50"])
assert_items_almost_equal(c, (1.0123754, 0.492012, -0.143110, 1))
c = grapefruit.Color.from_lab(66.9518, 0.43084, 0.739692, 0.5)
assert_items_almost_equal(c, (1, 0.5, 0, 0.5))
c = grapefruit.Color.from_lab(66.9518, 0.43084, 0.739692, 0.5, grapefruit.WHITE_REFERENCE["std_D50"])
assert_items_almost_equal(c, (1.0123754, 0.492012, -0.143110, 0.5))
def test_from_labInteger(self):
# Allow specifying lightness as an integer.
lab = (60, 0.3, 0.3)
c = grapefruit.Color.from_lab(*lab)
assert_items_almost_equal(c.lab, lab)
assert_true(c.is_legal)
def test_from_cmy(self):
c = grapefruit.Color.from_cmy(0, 0.5, 1)
assert_equal(c, (1, 0.5, 0, 1.0))
c = grapefruit.Color.from_cmy(0, 0.5, 1, 0.5)
assert_equal(c, (1, 0.5, 0, 0.5))
def test_from_cmyk(self):
c = grapefruit.Color.from_cmyk(1, 0.32, 0, 0.5)
assert_items_almost_equal(c, (0, 0.34, 0.5, 1))
c = grapefruit.Color.from_cmyk(1, 0.32, 0, 0.5, 0.5)
assert_items_almost_equal(c, (0, 0.34, 0.5, 0.5))
def test_from_html(self):
c = grapefruit.Color.from_html("#ff8000")
assert_items_almost_equal(c, (1, 0.5020, 0, 1))
c = grapefruit.Color.from_html("ff8000")
assert_items_almost_equal(c, (1, 0.5020, 0, 1))
c = grapefruit.Color.from_html("#f60")
assert_items_almost_equal(c, (1, 0.4, 0, 1))
c = grapefruit.Color.from_html("f60")
assert_items_almost_equal(c, (1, 0.4, 0, 1))
c = grapefruit.Color.from_html("lemonchiffon")
assert_items_almost_equal(c, (1, 0.9804, 0.8039, 1))
c = grapefruit.Color.from_html("#ff8000", 0.5)
assert_items_almost_equal(c, (1, 0.5020, 0, 0.5))
def test_from_pil(self):
c = grapefruit.Color.from_pil(0x0080ff)
assert_items_almost_equal(c, (1, 0.5020, 0, 1))
c = grapefruit.Color.from_pil(0x0080ff, 0.5)
assert_items_almost_equal(c, (1, 0.5020, 0, 0.5))
class TestColorProperties():
def test_get_alpha(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.alpha, 1.0)
def test_set_alpha(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.alpha = 0.5
assert_equal(col.alpha, 0.5)
def test_get_white_ref(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.white_ref, grapefruit.WHITE_REFERENCE['std_D65'])
def test_set_white_ref(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.white_ref = grapefruit.WHITE_REFERENCE['std_A']
assert_equal(grapefruit.WHITE_REFERENCE['std_A'], col.white_ref)
def test_get_rgb(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.rgb, (1, 0.5, 0))
def test_set_rgb(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.rgb = (0.1, 0.2, 0.3)
assert_equal(col.rgb, (0.1, 0.2, 0.3))
assert_equal(col.hsl, grapefruit.rgb_to_hsl(0.1, 0.2, 0.3))
def test_get_rgba(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.rgba, (1, 0.5, 0, 1))
def test_set_rgba(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.rgba = (0.1, 0.2, 0.3, 0.5)
assert_equal(col.rgb, (0.1, 0.2, 0.3))
assert_equal(col.alpha, 0.5)
assert_equal(col.hsl, grapefruit.rgb_to_hsl(0.1, 0.2, 0.3))
def test_get_red(self):
col = grapefruit.Color.from_rgb(0.1, 0.2, 0.3)
assert_equal(col.red, 0.1)
def test_set_red(self):
col = grapefruit.Color.from_rgb(0.1, 0.2, 0.3)
col.red = 0.9
assert_equal(col.rgb, (0.9, 0.2, 0.3))
assert_equal(col.hsl, grapefruit.rgb_to_hsl(0.9, 0.2, 0.3))
def test_get_green(self):
col = grapefruit.Color.from_rgb(0.1, 0.2, 0.3)
assert_equal(col.green, 0.2)
def test_set_green(self):
col = grapefruit.Color.from_rgb(0.1, 0.2, 0.3)
col.green = 0.9
assert_equal(col.rgb, (0.1, 0.9, 0.3))
assert_equal(col.hsl, grapefruit.rgb_to_hsl(0.1, 0.9, 0.3))
def test_get_blue(self):
col = grapefruit.Color.from_rgb(0.1, 0.2, 0.3)
assert_equal(col.blue, 0.3)
def test_set_blue(self):
col = grapefruit.Color.from_rgb(0.1, 0.2, 0.3)
col.blue = 0.9
assert_equal(col.rgb, (0.1, 0.2, 0.9))
assert_equal(col.hsl, grapefruit.rgb_to_hsl(0.1, 0.2, 0.9))
def test_get_hsl(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.hsl, (30, 1, 0.5))
def test_set_hsl(self):
col = grapefruit.Color.from_hsl(30, 1, 0.5)
col.hsl = (40, 0.5, 0.7)
assert_equal(col.hsl, (40, 0.5, 0.7))
assert_equal(col.rgb, grapefruit.hsl_to_rgb(40, 0.5, 0.7))
def test_get_hsl_hue(self):
col = grapefruit.Color.from_hsl(30, 1, 0.5)
assert_equal(col.hsl_hue, 30)
def test_set_hsl_hue(self):
col = grapefruit.Color.from_hsl(30, 1, 0.5)
col.hsl_hue = 40
assert_equal(col.hsl, (40, 1, 0.5))
assert_equal(col.rgb, grapefruit.hsl_to_rgb(40, 1, 0.5))
def test_get_hsv(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.hsv, (30, 1, 1))
def test_set_hsv(self):
col = grapefruit.Color.from_hsv(30, 1, 1)
col.hsv = (40, 0.5, 0.2)
assert_items_almost_equal(col.hsv, (40, 0.5, 0.2))
assert_equal(col.rgb, grapefruit.hsv_to_rgb(40, 0.5, 0.2))
assert_almost_equal(col.hsl, grapefruit.rgb_to_hsl(grapefruit.hsv_to_rgb(40, 0.5, 0.2)))
def test_get_yiq(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_items_almost_equal(col.yiq, (0.5923, 0.4589, -0.05))
def test_set_yiq(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.yiq = (0.1, 0.2, 0.3)
assert_items_almost_equal(col.yiq, (0.1, 0.2, 0.3))
assert_equal(col.rgb, grapefruit.yiq_to_rgb(0.1, 0.2, 0.3))
assert_almost_equal(col.hsl, grapefruit.rgb_to_hsl(grapefruit.yiq_to_rgb(0.1, 0.2, 0.3)))
def test_get_yuv(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_items_almost_equal(col.yuv, (0.5925, -0.2916, 0.3575))
def test_set_yuv(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.yuv = (0.1, 0.2, 0.3)
assert_items_almost_equal(col.yuv, (0.1, 0.2, 0.3))
assert_equal(col.rgb, grapefruit.yuv_to_rgb(0.1, 0.2, 0.3))
assert_almost_equal(col.hsl, grapefruit.rgb_to_hsl(grapefruit.yuv_to_rgb(0.1, 0.2, 0.3)))
def test_get_xyz(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_items_almost_equal(col.xyz, (0.4890, 0.3657, 0.04485))
def test_set_xyz(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.xyz = (0.1, 0.2, 0.3)
assert_items_almost_equal(col.xyz, (0.1, 0.2, 0.3))
assert_equal(col.rgb, grapefruit.xyz_to_rgb(0.1, 0.2, 0.3))
assert_almost_equal(col.hsl, grapefruit.rgb_to_hsl(grapefruit.xyz_to_rgb(0.1, 0.2, 0.3)))
def test_get_lab(self):
c = grapefruit.Color.from_rgb(1, 0.5, 0, wref=grapefruit.WHITE_REFERENCE['std_D50'])
assert_items_almost_equal(c.lab, (66.9518, 0.4117, 0.6728))
def test_get_cmy(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.cmy, (0, 0.5, 1))
def test_set_cmy(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.cmy = (0.1, 0.2, 0.3)
assert_items_almost_equal(col.cmy, (0.1, 0.2, 0.3))
assert_equal(col.rgb, grapefruit.cmy_to_rgb(0.1, 0.2, 0.3))
assert_almost_equal(col.hsl, grapefruit.rgb_to_hsl(grapefruit.cmy_to_rgb(0.1, 0.2, 0.3)))
def test_get_cmyk(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.cmyk, (0, 0.5, 1, 0))
def test_set_cmyk(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.cmyk = (0, 0.111, 0.222, 0.46)
assert_items_almost_equal(col.cmyk, (0, 0.111, 0.222, 0.46))
assert_equal(col.rgb, grapefruit.cmy_to_rgb(grapefruit.cmyk_to_cmy(0, 0.111, 0.222, 0.46)))
assert_almost_equal(col.hsl, grapefruit.rgb_to_hsl(grapefruit.cmy_to_rgb(grapefruit.cmyk_to_cmy(0, 0.111, 0.222, 0.46))))
def test_get_ints(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.ints, (255, 128, 0))
def test_set_ints(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.ints = (0, 128, 255)
assert_equal(col.rgb, (0.0, 128.0/255.0, 1.0))
assert_equal(col.hsl, grapefruit.rgb_to_hsl(0.0, 128.0/255.0, 1.0))
def test_get_html(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.html, '#ff8000')
def test_set_html(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.html = '#0080ff'
assert_equal(col.rgb, (0.0, 128.0/255.0, 1.0))
assert_equal(col.hsl, grapefruit.rgb_to_hsl(0.0, 128.0/255.0, 1.0))
def test_get_pil(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.pil, 0x0080ff)
def test_set_pil(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
col.pil = 0xff8000
assert_equal(col.rgb, (0.0, 128.0/255.0, 1.0))
assert_equal(col.hsl, grapefruit.rgb_to_hsl(0.0, 128.0/255.0, 1.0))
def test_get_websafe(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.websafe, (1, 0.6, 0))
def test_get_greyscale(self):
col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
assert_equal(col.greyscale, (0.5, 0.5, 0.5))
class TestColorMethods():
@classmethod
def setup_class(self):
self.rgb_col = grapefruit.Color.from_rgb(1.0, 0.5, 0.0)
self.hsl_col = grapefruit.Color.from_hsl(30, 1, 0.5)
self.hsl_col2 = grapefruit.Color.from_hsl(30, 0.5, 0.5)
def test_Init(self):
assert_equal(grapefruit.Color((1.0, 0.5, 0.0)), (1.0, 0.5, 0.0, 1.0))
assert_equal(grapefruit.Color((1.0, 0.5, 0.0), mode='rgb'), (1.0, 0.5, 0.0, 1.0))
assert_equal(grapefruit.Color((30, 1, 0.5), mode='hsl'), (1.0, 0.5, 0.0, 1.0))
assert_raises(ValueError, grapefruit.Color, (30, 1, 0.5), 'hsv')
def test_Eq(self):
assert_equal(self.rgb_col, self.hsl_col)
assert_equal(self.rgb_col, (1.0, 0.5, 0.0, 1.0))
assert_equal(self.rgb_col, [1.0, 0.5, 0.0, 1.0])
assert_equal([1.0, 0.5, 0.0, 1.0], self.rgb_col)
assert_not_equal(self.rgb_col, '(1.0, 0.5, 0.0, 1.0)')
def test_Repr(self):
assert_equal(repr(self.rgb_col), 'Color(1.0, 0.5, 0.0, 1.0)')
assert_equal(repr(self.hsl_col), 'Color(1.0, 0.5, 0.0, 1.0)')
def test_Str(self):
assert_equal(str(self.rgb_col), '(1.0, 0.5, 0.0, 1.0)')
assert_equal(str(self.hsl_col), '(1.0, 0.5, 0.0, 1.0)')
def test_Iter(self):
assert_equal([1, 0.5, 0, 1], list(iter(self.rgb_col)))
def test_with_alpha(self):
assert_equal(self.rgb_col.with_alpha(0.5), (1, 0.5, 0, 0.5))
def test_with_white_ref(self):
assert_equal(self.hsl_col.with_white_ref((0.1, 0.2, 0.3)).white_ref, (0.1, 0.2, 0.3))
def test_with_hue(self):
assert_equal(self.hsl_col.with_hue(60), (1.0, 1.0, 0.0, 1.0))
assert_equal(self.hsl_col.with_hue(60).hsl, (60, 1, 0.5))
def test_with_saturation(self):
assert_equal(self.hsl_col.with_saturation(0.5), (0.75, 0.5, 0.25, 1.0))
assert_equal(self.hsl_col.with_saturation(0.5).hsl, (30, 0.5, 0.5))
def test_with_lightness(self):
assert_equal(self.hsl_col.with_lightness(1), (1.0, 1.0, 1.0, 1.0))
assert_equal(self.hsl_col.with_lightness(1).hsl, (30, 1.0, 1.0))
def test_darker(self):
assert_items_almost_equal(self.hsl_col.darker(0.2), (0.6, 0.3, 0.0, 1.0))
assert_items_almost_equal(self.hsl_col.darker(0.2).hsl, (30, 1, 0.3))
def test_lighter(self):
assert_items_almost_equal(self.hsl_col.lighter(0.2), (1.0, 0.7, 0.4, 1.0))
assert_items_almost_equal(self.hsl_col.lighter(0.2).hsl, (30, 1, 0.7))
def test_saturate(self):
assert_items_almost_equal(self.hsl_col2.saturate(0.25), (0.875, 0.5, 0.125, 1.0))
assert_items_almost_equal(self.hsl_col2.saturate(0.25).hsl, (30, 0.75, 0.5))
def test_desaturate(self):
assert_items_almost_equal(self.hsl_col2.desaturate(0.25), (0.625, 0.5, 0.375, 1.0))
assert_items_almost_equal(self.hsl_col2.desaturate(0.25).hsl, (30, 0.25, 0.5))
def test_websafe_dither(self):
dithered = (
(1.0, 0.6, 0.0, 1.0),
(1.0, 0.4, 0.0, 1.0))
assert_equal(self.rgb_col.websafe_dither(), dithered)
def test_make_gradient(self):
gradient = [
(0.75, 0.25, 0.0, 1.0),
(0.5, 0.5, 0.0, 1.0),
(0.25, 0.75, 0.0, 1.0)]
c1 = grapefruit.Color.from_rgb(1.0, 0.0, 0.0)
c2 = grapefruit.Color.from_rgb(0.0, 1.0, 0.0)
assert_equal(gradient, c1.make_gradient(c2, 3))
def test_complementary_color(self):
assert_equal(self.hsl_col.complementary_color(mode='rgb').hsl, (210, 1, 0.5))
def test_make_monochrome_scheme(self):
monochrome = (
(0.94, 0.8, 0.66, 1.0), # hsl(30, 0.7, 0.8)
(0.6, 0.3, 0.0, 1.0), # hsl(30, 1, 0.3)
(0.88, 0.6, 0.32, 1.0), # hsl(30, 0.7, 0.6)
(1.0, 0.8, 0.6, 1.0)) # hsl(30, 1, 0.8)
scheme = self.rgb_col.make_monochrome_scheme()
for i in range(len(monochrome)):
assert_items_almost_equal(scheme[i], monochrome[i])
def test_make_triadic_scheme(self):
triad = (
(0.0, 1.0, 0.5, 1.0),
(0.5, 0.0, 1.0, 1.0))
assert_equal(self.rgb_col.make_triadic_scheme(mode='rgb'), triad)
def test_make_tetradic_scheme(self):
tetrad = (
(0.5, 1.0, 0.0, 1.0),
(0.0, 0.5, 1.0, 1.0),
(0.5, 0.0, 1.0, 1.0))
assert_equal(self.rgb_col.make_tetradic_scheme(mode='rgb'), tetrad)
def test_make_analogous_scheme(self):
scheme = (
(1.0, 0.0, 0.0, 1.0),
(1.0, 1.0, 0.0, 1.0))
assert_equal(self.rgb_col.make_analogous_scheme(mode='rgb'), scheme)
def test_alpha_blend(self):
c1 = grapefruit.Color.from_rgb(1, 0.5, 0, alpha = 0.2)
c2 = grapefruit.Color.from_rgb(1, 1, 1, alpha = 0.8)
assert_items_almost_equal(c1.alpha_blend(c2), (1, 0.875, 0.75, 0.84))
def test_blend(self):
c1 = grapefruit.Color.from_rgb(1, 0.5, 0, alpha = 0.2)
c2 = grapefruit.Color.from_rgb(1, 1, 1, alpha = 0.6)
assert_equal(c1.blend(c2), (1, 0.75, 0.5, 0.4))
def test_nearest_legal(self):
c = grapefruit.Color.from_rgb(1.1, -0.1, 0.5, alpha=1.1)
assert_false(c.is_legal)
assert_items_almost_equal(c.nearest_legal().rgb, (1.0, 0.0, 0.5))
assert_almost_equal(c.nearest_legal().alpha, 1.0)