-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathmake_color4_hwb.py
43 lines (33 loc) · 1.58 KB
/
make_color4_hwb.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
import colorsys
from decimal import Decimal
def fix_rounding_error(val):
return round(val * 100000) / 100000
# Based on https://github.com/web-platform-tests/wpt/blob/master/css/css-color/color-resolving-hwb.html
def hwb_to_rgb(hue, white, black):
if white + black >= 1:
gray = min(max(round(white / (white + black) * 255.0), 0.0), 255.0)
return (gray, gray, gray)
rgb = colorsys.hls_to_rgb(
hue / 360.0,
0.5,
1.0
)
t = (1.0 - white - black)
return tuple([min(max(round((fix_rounding_error(x) * t + white) * 255.0), 0.0), 255.0) for x in rgb])
items = []
for hue in [0, 30, 60, 90, 120, 180, 210, 240, 270, 300, 330, 360]:
for white in [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]:
for black in [0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875, 1.0]:
(r, g, b) = hwb_to_rgb(hue, white, black)
items.append('"hwb({:g}deg {:g}% {:g}%)", [{:g}, {:g}, {:g}, 1.0]'.format(
hue, white * 100, black * 100, r, g, b))
items.append('"hwb({:g} {:g}% {:g}%)", [{:g}, {:g}, {:g}, 1.0]'.format(
hue, white * 100, black * 100, r, g, b))
for alpha in [0, 0.2, 1]:
items.append('"hwb({:g}deg {:g}% {:g}% / {:g})", [{:g}, {:g}, {:g}, {:g}]'.format(
hue, white * 100, black * 100, alpha, r, g, b, alpha))
items.append('"hwb({:g} {:g}% {:g}% / {:g})", [{:g}, {:g}, {:g}, {:g}]'.format(
hue, white * 100, black * 100, alpha, r, g, b, alpha))
print('[')
print(',\n'.join(items))
print(']')