ยปCore Development>Code coverage>Lib/test/test_colorsys.py

Python code coverage for Lib/test/test_colorsys.py

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