I’m writing a Python application right now, and I want to display the current light color to the user. I’ve written a HSBKtoRGB
function:
def HSBKtoRGB(hsvk: Tuple[int, int, int, int]) -> Tuple[int, int, int]:
""" Converted from PHP https://gist.github.com/joshrp/5200913 """
iH, iS, iV, iK = hsvk
dS = (100 * iS / 65535) / 100.0 # Saturation: 0.0-1.0
dV = (100 * iV / 65535) / 100.0 # Lightness: 0.0-1.0
dC = dV * dS # Chroma: 0.0-1.0
dH = (360 * iH / 65535) / 60.0 # H-prime: 0.0-6.0
dT = dH # Temp variable
while dT >= 2.0: # php modulus does not work with float
dT -= 2.0
dX = dC * (1 - abs(dT - 1))
dHf = floor(dH)
if dHf == 0:
dR = dC
dG = dX
dB = 0.0
elif dHf == 1:
dR = dX
dG = dC
dB = 0.0
elif dHf == 2:
dR = 0.0
dG = dC
dB = dX
elif dHf == 3:
dR = 0.0
dG = dX
dB = dC
elif dHf == 4:
dR = dX
dG = 0.0
dB = dC
elif dHf == 5:
dR = dC
dG = 0.0
dB = dX
else:
dR = 0.0
dG = 0.0
dB = 0.0
dM = dV - dC
dR += dM
dG += dM
dB += dM
return int(dR * 255), int(dG * 255), int(dB * 255)
Its adapted from some PHP code, so sorry if it’s ugly. But, the code doesn’t factor in the Kelvin from the HSBK, and I was wondering how I could do that.
I can’t find any way to do it online, but obviously the light is doing it somehow. I’ve tried to mix the RGB value of the kelvin temperature. I weight the temperature inversely with the Chroma (iC).
rgb_prime = int(dR * 255), int(dG * 255), int(dB * 255)
rgb_k = KelvinToRGB(iK)
return_rgb = tuple(int(min(255, ((dC)*a)+((1-dC)*b))) for (a, b) in zip(rgb_prime, rgb_k)) # Light model
return return_rgb
The values returned are ok, but not great. It’s honestly more accurate if I just don’t mix the Kelvin value to begin with. I was wondering what the most accurate way to blend these colors.