1 | n/a | import unittest |
---|
2 | n/a | import colorsys |
---|
3 | n/a | |
---|
4 | n/a | def frange(start, stop, step): |
---|
5 | n/a | while start <= stop: |
---|
6 | n/a | yield start |
---|
7 | n/a | start += step |
---|
8 | n/a | |
---|
9 | n/a | class ColorsysTest(unittest.TestCase): |
---|
10 | n/a | |
---|
11 | n/a | def assertTripleEqual(self, tr1, tr2): |
---|
12 | n/a | self.assertEqual(len(tr1), 3) |
---|
13 | n/a | self.assertEqual(len(tr2), 3) |
---|
14 | n/a | self.assertAlmostEqual(tr1[0], tr2[0]) |
---|
15 | n/a | self.assertAlmostEqual(tr1[1], tr2[1]) |
---|
16 | n/a | self.assertAlmostEqual(tr1[2], tr2[2]) |
---|
17 | n/a | |
---|
18 | n/a | def test_hsv_roundtrip(self): |
---|
19 | n/a | for r in frange(0.0, 1.0, 0.2): |
---|
20 | n/a | for g in frange(0.0, 1.0, 0.2): |
---|
21 | n/a | for b in frange(0.0, 1.0, 0.2): |
---|
22 | n/a | rgb = (r, g, b) |
---|
23 | n/a | self.assertTripleEqual( |
---|
24 | n/a | rgb, |
---|
25 | n/a | colorsys.hsv_to_rgb(*colorsys.rgb_to_hsv(*rgb)) |
---|
26 | n/a | ) |
---|
27 | n/a | |
---|
28 | n/a | def test_hsv_values(self): |
---|
29 | n/a | values = [ |
---|
30 | n/a | # rgb, hsv |
---|
31 | n/a | ((0.0, 0.0, 0.0), ( 0 , 0.0, 0.0)), # black |
---|
32 | n/a | ((0.0, 0.0, 1.0), (4./6., 1.0, 1.0)), # blue |
---|
33 | n/a | ((0.0, 1.0, 0.0), (2./6., 1.0, 1.0)), # green |
---|
34 | n/a | ((0.0, 1.0, 1.0), (3./6., 1.0, 1.0)), # cyan |
---|
35 | n/a | ((1.0, 0.0, 0.0), ( 0 , 1.0, 1.0)), # red |
---|
36 | n/a | ((1.0, 0.0, 1.0), (5./6., 1.0, 1.0)), # purple |
---|
37 | n/a | ((1.0, 1.0, 0.0), (1./6., 1.0, 1.0)), # yellow |
---|
38 | n/a | ((1.0, 1.0, 1.0), ( 0 , 0.0, 1.0)), # white |
---|
39 | n/a | ((0.5, 0.5, 0.5), ( 0 , 0.0, 0.5)), # grey |
---|
40 | n/a | ] |
---|
41 | n/a | for (rgb, hsv) in values: |
---|
42 | n/a | self.assertTripleEqual(hsv, colorsys.rgb_to_hsv(*rgb)) |
---|
43 | n/a | self.assertTripleEqual(rgb, colorsys.hsv_to_rgb(*hsv)) |
---|
44 | n/a | |
---|
45 | n/a | def test_hls_roundtrip(self): |
---|
46 | n/a | for r in frange(0.0, 1.0, 0.2): |
---|
47 | n/a | for g in frange(0.0, 1.0, 0.2): |
---|
48 | n/a | for b in frange(0.0, 1.0, 0.2): |
---|
49 | n/a | rgb = (r, g, b) |
---|
50 | n/a | self.assertTripleEqual( |
---|
51 | n/a | rgb, |
---|
52 | n/a | colorsys.hls_to_rgb(*colorsys.rgb_to_hls(*rgb)) |
---|
53 | n/a | ) |
---|
54 | n/a | |
---|
55 | n/a | def test_hls_values(self): |
---|
56 | n/a | values = [ |
---|
57 | n/a | # rgb, hls |
---|
58 | n/a | ((0.0, 0.0, 0.0), ( 0 , 0.0, 0.0)), # black |
---|
59 | n/a | ((0.0, 0.0, 1.0), (4./6., 0.5, 1.0)), # blue |
---|
60 | n/a | ((0.0, 1.0, 0.0), (2./6., 0.5, 1.0)), # green |
---|
61 | n/a | ((0.0, 1.0, 1.0), (3./6., 0.5, 1.0)), # cyan |
---|
62 | n/a | ((1.0, 0.0, 0.0), ( 0 , 0.5, 1.0)), # red |
---|
63 | n/a | ((1.0, 0.0, 1.0), (5./6., 0.5, 1.0)), # purple |
---|
64 | n/a | ((1.0, 1.0, 0.0), (1./6., 0.5, 1.0)), # yellow |
---|
65 | n/a | ((1.0, 1.0, 1.0), ( 0 , 1.0, 0.0)), # white |
---|
66 | n/a | ((0.5, 0.5, 0.5), ( 0 , 0.5, 0.0)), # grey |
---|
67 | n/a | ] |
---|
68 | n/a | for (rgb, hls) in values: |
---|
69 | n/a | self.assertTripleEqual(hls, colorsys.rgb_to_hls(*rgb)) |
---|
70 | n/a | self.assertTripleEqual(rgb, colorsys.hls_to_rgb(*hls)) |
---|
71 | n/a | |
---|
72 | n/a | def test_yiq_roundtrip(self): |
---|
73 | n/a | for r in frange(0.0, 1.0, 0.2): |
---|
74 | n/a | for g in frange(0.0, 1.0, 0.2): |
---|
75 | n/a | for b in frange(0.0, 1.0, 0.2): |
---|
76 | n/a | rgb = (r, g, b) |
---|
77 | n/a | self.assertTripleEqual( |
---|
78 | n/a | rgb, |
---|
79 | n/a | colorsys.yiq_to_rgb(*colorsys.rgb_to_yiq(*rgb)) |
---|
80 | n/a | ) |
---|
81 | n/a | |
---|
82 | n/a | def test_yiq_values(self): |
---|
83 | n/a | values = [ |
---|
84 | n/a | # rgb, yiq |
---|
85 | n/a | ((0.0, 0.0, 0.0), (0.0, 0.0, 0.0)), # black |
---|
86 | n/a | ((0.0, 0.0, 1.0), (0.11, -0.3217, 0.3121)), # blue |
---|
87 | n/a | ((0.0, 1.0, 0.0), (0.59, -0.2773, -0.5251)), # green |
---|
88 | n/a | ((0.0, 1.0, 1.0), (0.7, -0.599, -0.213)), # cyan |
---|
89 | n/a | ((1.0, 0.0, 0.0), (0.3, 0.599, 0.213)), # red |
---|
90 | n/a | ((1.0, 0.0, 1.0), (0.41, 0.2773, 0.5251)), # purple |
---|
91 | n/a | ((1.0, 1.0, 0.0), (0.89, 0.3217, -0.3121)), # yellow |
---|
92 | n/a | ((1.0, 1.0, 1.0), (1.0, 0.0, 0.0)), # white |
---|
93 | n/a | ((0.5, 0.5, 0.5), (0.5, 0.0, 0.0)), # grey |
---|
94 | n/a | ] |
---|
95 | n/a | for (rgb, yiq) in values: |
---|
96 | n/a | self.assertTripleEqual(yiq, colorsys.rgb_to_yiq(*rgb)) |
---|
97 | n/a | self.assertTripleEqual(rgb, colorsys.yiq_to_rgb(*yiq)) |
---|
98 | n/a | |
---|
99 | n/a | if __name__ == "__main__": |
---|
100 | n/a | unittest.main() |
---|