17.10.2024, 13:49
Try this updated conversion function:
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 round(v)
if v <= 0 then
return 0
else
return math.round(v * level)
end
end
x = x / 0xFFFE
y = y / 0xFFFE
local z = 1.0 - x - y
local Y = level
local X = (Y / y) * x
local Z = (Y / y) * z
local r = X * 1.656492 - Y * 0.354851 - Z * 0.255038
local g = -X * 0.707196 + Y * 1.655397 + Z * 0.036152
local b = X * 0.051713 - Y * 0.121364 + Z * 1.01153
if r > b and r > g and r > 1 then
g = g / r
b = b / r
r = 1
elseif g > b and g > r and g > 1 then
r = r / g
b = b / g
g = 1
elseif b > r and b > g and b > 1 then
r = r / b
g = g / b
b = 1
end
r = round(revgamma(r))
g = round(revgamma(g))
b = round(revgamma(b))
return r * 0x10000 + g * 0x100 + b
end