Crea una funcion en lua para convertir colores en "rgb" a colores "oklch".
- User
Crea una funcion en lua para convertir colores en "rgb" a colores "oklch".
- Assistant
Crea una funcion en lua para convertir colores en "rgb" a colores "oklch".
Para convertir colores de RGB a OKLCH en Lua, necesitamos dividir el proceso en varias etapas, ya que no hay una función incorporada para esta conversión directa en Lua. Involucra varias transformaciones de espacio de color.
RGB a Linear RGB: Primero, debes convertir los valores de color sRGB no lineales a RGB lineales.
Linear RGB a CIEXYZ: Luego, convierte RGB lineal a espacio de color CIEXYZ.
CIEXYZ a CIELAB: A continuación, lleva los valores de CIEXYZ a CIELAB.
CIELAB a OKLAB: Convierte CIELAB a OKLAB.
OKLAB a OKLCH: Finalmente, transforma OKLAB a OKLCH.
Aquí tienes cómo podrías implementar esto en Lua:
function srgbToLinear(c)
if c <= 0.04045 then
return c / 12.92
else
return ((c + 0.055) / 1.055)^2.4
end
end
function linearToSrgb(c)
if c <= 0.0031308 then
return 12.92 * c
else
return 1.055 * c^(1/2.4) - 0.055
end
end
function rgbToXyz(r, g, b)
r = srgbToLinear(r / 255)
g = srgbToLinear(g / 255)
b = srgbToLinear(b / 255)
local x = r * 0.4124564 + g * 0.3575761 + b * 0.1804375
local y = r * 0.2126729 + g * 0.7151522 + b * 0.0721750
local z = r * 0.0193339 + g * 0.1191920 + b * 0.9503041
return x, y, z
end
function xyzToOklab(x, y, z)
-- Normalizing for the D65 illuminant
local l = 0.57735027 * (0.8189330101 * x + 0.3618667424 * y + 0.0194802320 * z)
local m = 0.57735027 * (0.0329845436 * x + 0.9293118715 * y + 0.0361456387 * z)
local s = 0.57735027 * (0.0482003018 * x + 0.2643662691 * y + 0.6338517070 * z)
l = l^(1/3)
m = m^(1/3)
s = s^(1/3)
local L = 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s
local a = 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s
local b = 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s
return L, a, b
end
function oklabToOklch(L, a, b)
local C = math.sqrt(a * a + b * b)
local h = math.atan2(b, a)
-- Convert hue to degrees
h = h * (180 / math.pi)
if h < 0 then
h = h + 360
end
return L, C, h
end
function rgbToOklch(r, g, b)
local x, y, z = rgbToXyz(r, g, b)
local L, a, b = xyzToOklab(x, y, z)
return oklabToOklch(L, a, b)
end
-- Ejemplo de uso
local L, C, h = rgbToOklch(255, 0, 0) -- Rojo
print("L:", L, "C:", C, "h:", h)
Ten en cuenta que este script realiza varias conversiones complejas a través de múltiples espacios de color. Las constantes utilizadas para las transformaciones están basadas en el estándar de iluminación D65 y podrían necesitar ajustes dependiendo de tus necesidades específicas. Además, esta implementación está sujeta a limitaciones de precisión de punto flotante en Lua.
Not yet included in a published release.