forked from igraph/python-igraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_colortests.py
More file actions
118 lines (95 loc) · 4.02 KB
/
Copy pathtest_colortests.py
File metadata and controls
118 lines (95 loc) · 4.02 KB
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
import unittest
from igraph import (
hsv_to_rgb,
hsva_to_rgba,
hsl_to_rgb,
hsla_to_rgba,
rgb_to_hsl,
rgba_to_hsla,
rgba_to_hsva,
rgb_to_hsv,
GradientPalette,
AdvancedGradientPalette,
)
class ColorTests(unittest.TestCase):
def assertAlmostEqualMany(self, items1, items2, eps):
for idx, (item1, item2) in enumerate(zip(items1, items2)):
self.assertAlmostEqual(
item1,
item2,
places=eps,
msg="mismatch at index %d, %r != %r with %d digits"
% (idx, items1, items2, eps),
)
def setUp(self):
columns = ["r", "g", "b", "h", "v", "l", "s_hsv", "s_hsl", "alpha"]
# Examples taken from http://en.wikipedia.org/wiki/HSL_and_HSV
values = [
(1, 1, 1, 0, 1, 1, 0, 0, 1),
(0.5, 0.5, 0.5, 0, 0.5, 0.5, 0, 0, 0.5),
(0, 0, 0, 0, 0, 0, 0, 0, 1),
(1, 0, 0, 0, 1, 0.5, 1, 1, 0.5),
(0.75, 0.75, 0, 60, 0.75, 0.375, 1, 1, 0.25),
(0, 0.5, 0, 120, 0.5, 0.25, 1, 1, 0.75),
(0.5, 1, 1, 180, 1, 0.75, 0.5, 1, 1),
(0.5, 0.5, 1, 240, 1, 0.75, 0.5, 1, 1),
(0.75, 0.25, 0.75, 300, 0.75, 0.5, 0.666666667, 0.5, 0.25),
(0.211, 0.149, 0.597, 248.3, 0.597, 0.373, 0.750, 0.601, 1),
(0.495, 0.493, 0.721, 240.5, 0.721, 0.607, 0.316, 0.290, 0.75),
]
self.data = [dict(list(zip(columns, value))) for value in values]
for row in self.data:
row["h"] /= 360.0
def _testGeneric(self, method, args1, args2=("r", "g", "b")):
if len(args1) == len(args2) + 1:
args2 += ("alpha",)
for data in self.data:
vals1 = [data.get(arg, 0.0) for arg in args1]
vals2 = [data.get(arg, 0.0) for arg in args2]
self.assertAlmostEqualMany(method(*vals1), vals2, 2)
def testHSVtoRGB(self):
self._testGeneric(hsv_to_rgb, "h s_hsv v".split())
def testHSVAtoRGBA(self):
self._testGeneric(hsva_to_rgba, "h s_hsv v alpha".split())
def testHSLtoRGB(self):
self._testGeneric(hsl_to_rgb, "h s_hsl l".split())
def testHSLAtoRGBA(self):
self._testGeneric(hsla_to_rgba, "h s_hsl l alpha".split())
def testRGBtoHSL(self):
self._testGeneric(rgb_to_hsl, "r g b".split(), "h s_hsl l".split())
def testRGBAtoHSLA(self):
self._testGeneric(
rgba_to_hsla, "r g b alpha".split(), "h s_hsl l alpha".split()
)
def testRGBtoHSV(self):
self._testGeneric(rgb_to_hsv, "r g b".split(), "h s_hsv v".split())
def testRGBAtoHSVA(self):
self._testGeneric(
rgba_to_hsva, "r g b alpha".split(), "h s_hsv v alpha".split()
)
class PaletteTests(unittest.TestCase):
def testGradientPalette(self):
gp = GradientPalette("red", "blue", 3)
self.assertTrue(gp.get(0) == (1.0, 0.0, 0.0, 1.0))
self.assertTrue(gp.get(1) == (0.5, 0.0, 0.5, 1.0))
self.assertTrue(gp.get(2) == (0.0, 0.0, 1.0, 1.0))
def testAdvancedGradientPalette(self):
agp = AdvancedGradientPalette(["red", "black", "blue"], n=9)
self.assertTrue(agp.get(0) == (1.0, 0.0, 0.0, 1.0))
self.assertTrue(agp.get(2) == (0.5, 0.0, 0.0, 1.0))
self.assertTrue(agp.get(4) == (0.0, 0.0, 0.0, 1.0))
self.assertTrue(agp.get(5) == (0.0, 0.0, 0.25, 1.0))
self.assertTrue(agp.get(8) == (0.0, 0.0, 1.0, 1.0))
agp = AdvancedGradientPalette(["red", "black", "blue"], [0, 8, 2], 9)
self.assertTrue(agp.get(0) == (1.0, 0.0, 0.0, 1.0))
self.assertTrue(agp.get(1) == (0.5, 0.0, 0.5, 1.0))
self.assertTrue(agp.get(5) == (0.0, 0.0, 0.5, 1.0))
def suite():
color_suite = unittest.defaultTestLoader.loadTestsFromTestCase(ColorTests)
palette_suite = unittest.defaultTestLoader.loadTestsFromTestCase(PaletteTests)
return unittest.TestSuite([color_suite, palette_suite])
def test():
runner = unittest.TextTestRunner()
runner.run(suite())
if __name__ == "__main__":
test()