-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcolorutils.py
57 lines (42 loc) · 1.33 KB
/
colorutils.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
import colorsys
def rgb_to_hsv(rgb):
rgb_0_1 = [d / 255.0 for d in rgb[:3]]
hsv_0_1 = colorsys.rgb_to_hsv(*rgb_0_1)
return tuple(d * r for d, r in zip(hsv_0_1, [360.0, 100.0, 100.0]))
# HSL <-> HSV conversion based on C code by Ariya Hidayat:
# http://ariya.ofilabs.com/2008/07/converting-between-hsl-and-hsv.html
def hsl_to_hsv(color):
'''
>>> hsl_to_hsv((120, 100, 50))
(120.0, 100.0, 100.0)
>>> hsl_to_hsv((0, 100, 100))
(0.0, 0.0, 100.0)
Saturation in HSV is undefined and arbitrarily 0 for black:
>>> hsl_to_hsv((240, 100, 0))
(240.0, 0.0, 0.0)
'''
hi, si, li = [float(d) for d in color]
ho = hi
si *= (li / 100.0) if li <= 50.0 else (1.0 - li / 100.0)
vo = li + si
so = (200.0 * si / vo) if vo else 0.0
return (ho, so, vo)
def hsv_to_hsl(color):
'''
>>> hsv_to_hsl((120, 100, 100))
(120.0, 100.0, 50.0)
Saturation in HSL is undefined and arbitrarily 0 for black and white:
>>> hsv_to_hsl((240, 0, 0))
(240.0, 0.0, 0.0)
>>> hsv_to_hsl((0, 0, 100))
(0.0, 0.0, 100.0)
'''
hi, si, vi = [float(d) for d in color]
ho = hi
lo = (200.0 - si) * vi / 200.0
so = si * vi / 200.0
if lo >= 100.0 or lo <= 0.0:
so = 0.0
else:
so /= (lo / 100.0) if lo <= 50.0 else (1.0 - lo / 100.0)
return (ho, so, lo)