24.09.2024, 06:42
Use this conversion function:
You also need to pass the light level (0..254) for the conversion to work.
Code:
function xytorgb(x, y, level)
local function revgamma(v)
if v <= 0.0031308 then
return v * 12.92
else
return (1 + 0.055) * math.pow(v, (1 / 2.4)) - 0.055
end
end
local function clampconvert(v, max)
v = math.max(0, v * 255 / max)
v = math.min(255, v)
return math.round(v)
end
local x = x / 0xFFFE
local y = y / 0xFFFE
local z = 1 - x - y
local Y = math.min(254, level) / 254
local X = (Y / y) * x
local Z = (Y / y) * z
local r = revgamma(3.2404542 * X - 1.5371385 * Y - 0.4985314 * Z)
local g = revgamma(-0.9692660 * X + 1.8760108 * Y + 0.0415560 * Z)
local b = revgamma(0.0556434 * X - 0.2040259 * Y + 1.0572252 * Z)
local max = math.max(r, g, b)
r = clampconvert(r, max)
g = clampconvert(g, max)
b = clampconvert(b, max)
return r * 0x10000 + g * 0x100 + b
end
x = 45913
y = 19614
level = 254
rgb = xytorgb(x, y, level)
log(rgb)
You also need to pass the light level (0..254) for the conversion to work.