-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathmake_color3_hsl.py
50 lines (48 loc) · 1.58 KB
/
make_color3_hsl.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
import colorsys # It turns out Python already does HSL -> RGB!
import math
# We sometimes get values of 127.499999 which
# may be 127.5000001 in the implementation. We'd like
# to exclude these values.
#
# 127.5 is the only case where the set of values used
# in the test causes rounding errors.
def has_rounding_error(hue, saturation, lightness):
rgb = colorsys.hls_to_rgb(
hue / 360.,
lightness / 1000.,
saturation / 1000.
)
return any(abs((x * 255) - 127.5) < 0.01 for x in rgb)
trim = lambda s: s if not s.endswith('.0') else s[:-2]
print('[')
print(',\n'.join(
function_format % tuple(
[
function_name,
hue,
trim(str(saturation / 10.)),
trim(str(lightness / 10.)),
alpha_format % round(alpha, 2) if alpha is not None else ''
] + [
trim(str(min(255, round(component * 255.))))
for component in colorsys.hls_to_rgb(
hue / 360.,
lightness / 1000.,
saturation / 1000.
)
] + [
alpha if alpha is not None else 1.0
]
)
for function_format, alpha_format in [
('"%s(%s, %s%%, %s%%%s)", [%s, %s, %s, %s]', ', %s'),
('"%s(%s %s%% %s%%%s)", [%s, %s, %s, %s]', ' / %s')
]
for function_name in ["hsl", "hsla"]
for alpha in [None, 1.0, 0.25, 0.0]
for lightness in range(0, 1001, 125)
for saturation in range(0, 1001, 125)
for hue in range(0, 360, 30)
if not has_rounding_error(hue, saturation, lightness)
))
print(']')