44 lines
686 B
Python
44 lines
686 B
Python
|
import numpy as np
|
||
|
|
||
|
# h = 0..359
|
||
|
# s = 0..255
|
||
|
# v = 0..511 (values above 255 add white channel)
|
||
|
def hsv2rgbw(h, s, v):
|
||
|
|
||
|
h %= 360
|
||
|
|
||
|
v_ = min(255, v)
|
||
|
|
||
|
# C = 0..255
|
||
|
C = (v_ * s) // 256
|
||
|
|
||
|
h_ = 128 * h // 60
|
||
|
|
||
|
# X = 0..255
|
||
|
X = C * (128 - abs(h_ % 256 - 128)) // 128
|
||
|
|
||
|
if h < 60:
|
||
|
r, g, b = C, X, 0
|
||
|
elif h < 120:
|
||
|
r, g, b = X, C, 0
|
||
|
elif h < 180:
|
||
|
r, g, b = 0, C, X
|
||
|
elif h < 240:
|
||
|
r, g, b = 0, X, C
|
||
|
elif h < 300:
|
||
|
r, g, b = X, 0, C
|
||
|
else:
|
||
|
r, g, b = C, 0, X
|
||
|
|
||
|
m = v_ - C
|
||
|
r += m
|
||
|
g += m
|
||
|
b += m
|
||
|
|
||
|
if v >= 256:
|
||
|
w = v - 255
|
||
|
else:
|
||
|
w = 0
|
||
|
|
||
|
return np.array([r, g, b, w])
|