Skip to content

Commit

Permalink
RGB to LAB improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
boatbomber committed Nov 14, 2021
1 parent d20d855 commit c1220f9
Showing 1 changed file with 6 additions and 38 deletions.
44 changes: 6 additions & 38 deletions src/Util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,9 @@ function Util.RGBtoLAB(c)
do -- Convert RGB to XYZ
local r, g, b = c.R, c.G, c.B

if r > 0.04045 then
r = ((r + 0.055) / 1.055) ^ 2.4
else
r /= 12.92
end

if g > 0.04045 then
g = ((g + 0.055) / 1.055) ^ 2.4
else
g /= 12.92
end

if b > 0.04045 then
b = ((b + 0.055) / 1.055) ^ 2.4
else
b /= 12.92
end

r *= 100
g *= 100
b *= 100
r = if r > 0.04045 then (((r + 0.055) / 1.055) ^ 2.4) * 100 else r / 0.1292
g = if g > 0.04045 then (((g + 0.055) / 1.055) ^ 2.4) * 100 else g / 0.1292
b = if b > 0.04045 then (((b + 0.055) / 1.055) ^ 2.4) * 100 else b / 0.1292

X = 0.4124 * r + 0.3576 * g + 0.1805 * b
Y = 0.2126 * r + 0.7152 * g + 0.0722 * b
Expand All @@ -45,23 +27,9 @@ function Util.RGBtoLAB(c)
do -- Convert XYZ to LAB
local xr, yr, zr = X / Xr, Y / Yr, Z / Zr

if xr > 0.008856 then
xr = xr ^ (1 / 3)
else
xr = ((7.787 * xr) + 16 / 116.0)
end

if yr > 0.008856 then
yr = yr ^ (1 / 3)
else
yr = ((7.787 * yr) + 16 / 116.0)
end

if zr > 0.008856 then
zr = zr ^ (1 / 3)
else
zr = ((7.787 * zr) + 16 / 116.0)
end
xr = if xr > 0.008856 then xr ^ (1 / 3) else ((7.787 * xr) + 0.13793103448276)
yr = if yr > 0.008856 then yr ^ (1 / 3) else ((7.787 * yr) + 0.13793103448276)
zr = if zr > 0.008856 then zr ^ (1 / 3) else ((7.787 * zr) + 0.13793103448276)

l, a, b = (116 * yr) - 16, 500 * (xr - yr), 200 * (yr - zr)
end
Expand Down

0 comments on commit c1220f9

Please sign in to comment.