forked from plotly/colorlover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
111 lines (94 loc) · 3.89 KB
/
tests.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
import unittest
import colorlover as cl
rgb_hsl_colors = {
(0, 0, 0): (0, 0, 0), # Black
(255, 255, 255): (0, 0, 100), # White
(255, 0, 0): (0, 100, 50), # Red
(0, 255, 0): (120, 100, 50), # Lime
(0, 0, 255): (240, 100, 50), # Blue
(255, 255, 0): (60, 100, 50), # Yellow
(0, 255, 255): (180, 100, 50), # Cyan
(255, 0, 255): (300, 100, 50), # Magenta
(192, 192, 192): (0, 0, 75), # Silver
(128, 128, 128): (0, 0, 50), # Gray
(128, 0, 0): (0, 100, 25), # Maroon
(128, 128, 0): (60, 100, 25), # Olive
(0, 128, 0): (120, 100, 25), # Green
(128, 0, 128): (300, 100, 25), # Purple
(0, 128, 128): (180, 100, 25), # Teal
(0, 0, 128): (240, 100, 25), # Navy
(20, 30, 128): (234, 72, 29) # Near navy
}
class UsageTests(unittest.TestCase):
def test_scale_type(self):
test_colors = {'rgb': ['rgb(252, 141, 89)', 'rgb(255, 255, 191)', 'rgb(145, 191, 219)'],
'hsl': ['hsl(19,96,67)', 'hsl(60,100,87)', 'hsl(203,51,71)'],
'hex': ['#fc8d59', '#ffffbf', '#91bfdb'],
'numeric': [(252, 141, 89), (255, 255, 191), (145, 191, 219)],
}
for type_scale, scale in test_colors.items():
self.assertEqual(
cl.scale_type(test_colors.get(type_scale)),
type_scale
)
def test_scales(self):
scales = cl.scales['3']['div']['RdYlBu']
self.assertEqual(
scales,
['rgb(252,141,89)', 'rgb(255,255,191)', 'rgb(145,191,219)'])
def test_get_scales(self):
scales = cl.get_scale(scale_type='div', scale_seq='RdYlBu', length=3)
self.assertEqual(
scales,
['rgb(252,141,89)', 'rgb(255,255,191)', 'rgb(145,191,219)'])
def test_to_numeric(self):
scales = cl.to_numeric(cl.scales['3']['div']['RdYlBu'])
self.assertEqual(
scales,
[(252, 141, 89),
(255, 255, 191), (145, 191, 219)]
)
def test_to_hex(self):
scales = cl.to_hex(cl.scales['3']['div']['RdYlBu'])
self.assertEqual(
scales,
['#fc8d59', '#ffffbf', '#91bfdb']
)
def test_to_hsl(self):
scales = cl.to_hsl(cl.scales['3']['div']['RdYlBu'])
self.assertEqual(
scales,
['hsl(19, 96%, 67%)', 'hsl(60, 100%, 87%)',
'hsl(203, 51%, 71%)']
)
def test_to_rgb(self):
scales = cl.to_rgb(cl.scales['3']['div']['RdYlBu'])
self.assertEqual(
scales,
['rgb(252,141,89)', 'rgb(255,255,191)', 'rgb(145,191,219)']
)
def test_to_html(self):
html = cl.to_html(cl.scales['3']['div']['RdYlBu'])
self.assertEqual(
html,
('<div style="background-color:rgb(252,141,89);'
'height:20px;width:20px;margin-bottom:0px;display:inline-block;"></div>'
'<div style="background-color:rgb(255,255,191);'
'height:20px;width:20px;margin-bottom:0px;display:inline-block;"></div>'
'<div style="background-color:rgb(145,191,219);'
'height:20px;width:20px;margin-bottom:0px;display:inline-block;"></div>')
)
def test_flipper(self):
flipped = cl.flipper()['div']['3']['RdYlBu']
self.assertEqual(
flipped,
['rgb(252,141,89)', 'rgb(255,255,191)', 'rgb(145,191,219)']
)
def test_rgb_to_hsl(self):
# Make sure interpolating a colorscale to the same length does nothing
# but change colorspace from hsl to rgb
for rgb, expected_hsl in rgb_hsl_colors.items():
hsl = cl.rgb_to_hsl(rgb)
self.assertEqual(hsl, expected_hsl)
if __name__ == '__main__':
unittest.main()