/
husl_test.py
75 lines (61 loc) · 2.97 KB
/
husl_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
import unittest
import json
import os.path
import husl
rgb_range_tolerance = 0.00000000001
snapshot_tolerance = 0.00000000001
class TestHusl(unittest.TestCase):
def setUp(self):
# Load snapshot into memory
name = os.path.join(os.path.dirname(__file__), 'snapshot-rev3.json')
json_data = open(name)
self.snapshot = json.load(json_data)
json_data.close()
def test_within_rgb_range(self):
for H in range(0, 361, 5):
for S in range(0, 101, 5):
for L in range(0, 101, 5):
RGB = husl.husl_to_rgb(H, S, L)
for channel in RGB:
assert (channel >= -rgb_range_tolerance and channel <= 1 + rgb_range_tolerance), ((H, S, L), RGB)
RGB = husl.huslp_to_rgb(H, S, L)
for channel in RGB:
assert (channel >= -rgb_range_tolerance and channel <= 1 + rgb_range_tolerance), ((H, S, L), RGB)
def test_snapshot(self):
for hex_color, colors in self.snapshot.items():
# Test forward functions
test_rgb = husl.hex_to_rgb(hex_color)
self.assertTuplesClose(test_rgb, colors['rgb'])
test_xyz = husl.rgb_to_xyz(test_rgb)
self.assertTuplesClose(test_xyz, colors['xyz'])
test_luv = husl.xyz_to_luv(test_xyz)
self.assertTuplesClose(test_luv, colors['luv'])
test_lch = husl.luv_to_lch(test_luv)
self.assertTuplesClose(test_lch, colors['lch'])
test_husl = husl.lch_to_husl(test_lch)
self.assertTuplesClose(test_husl, colors['husl'])
test_huslp = husl.lch_to_huslp(test_lch)
self.assertTuplesClose(test_huslp, colors['huslp'])
# Test backward functions
test_lch = husl.husl_to_lch(colors['husl'])
self.assertTuplesClose(test_lch, colors['lch'])
test_lch = husl.huslp_to_lch(colors['huslp'])
self.assertTuplesClose(test_lch, colors['lch'])
test_luv = husl.lch_to_luv(test_lch)
self.assertTuplesClose(test_luv, colors['luv'])
test_xyz = husl.luv_to_xyz(test_luv)
self.assertTuplesClose(test_xyz, colors['xyz'])
test_rgb = husl.xyz_to_rgb(test_xyz)
self.assertTuplesClose(test_rgb, colors['rgb'])
self.assertEqual(husl.rgb_to_hex(test_rgb), hex_color)
# Full test
self.assertEqual(husl.husl_to_hex(*colors['husl']), hex_color)
self.assertTuplesClose(husl.hex_to_husl(hex_color), colors['husl'])
self.assertEqual(husl.huslp_to_hex(*colors['huslp']), hex_color)
self.assertTuplesClose(husl.hex_to_huslp(hex_color), colors['huslp'])
def assertTuplesClose(self, tup1, tup2):
for a, b in zip(tup1, tup2):
if abs(a - b) > snapshot_tolerance:
raise Exception("Mismatch: {} {}".format(a, b))
if __name__ == '__main__':
unittest.main()