-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamic lossy combined with CIE76 deltaE
- Loading branch information
1 parent
200cd4d
commit 04e59ad
Showing
3 changed files
with
86 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,86 @@ | ||
local Util = {} | ||
|
||
-- D65/2° | ||
local Xr = 95.047 | ||
local Yr = 100.0 | ||
local Zr = 108.883 | ||
local labCache = {} | ||
function Util.RGBtoLAB(c) | ||
if labCache[c] then | ||
return table.unpack(labCache[c]) | ||
end | ||
|
||
local X, Y, Z | ||
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 | ||
|
||
X = 0.4124 * r + 0.3576 * g + 0.1805 * b | ||
Y = 0.2126 * r + 0.7152 * g + 0.0722 * b | ||
Z = 0.0193 * r + 0.1192 * g + 0.9505 * b | ||
end | ||
|
||
local l, a, b | ||
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 | ||
|
||
l, a, b = (116 * yr) - 16, 500 * (xr - yr), 200 * (yr - zr) | ||
end | ||
|
||
labCache[c] = { l, a, b } | ||
|
||
return l, a, b | ||
end | ||
|
||
function Util.DeltaRGB(a: Color3, b: Color3) | ||
local r1, g1, b1 = a.R,a.G,a.B | ||
local r2, g2, b2 = b.R,b.G,b.B | ||
local drp2 = (r1 - r2)^2 | ||
local dgp2 = (g1 - g2)^2 | ||
local dbp2 = (b1 - b2)^2 | ||
local t = (r1 + r2) / 2 | ||
|
||
return math.sqrt(2 * drp2 + 4 * dgp2 + 3 * dbp2 + t * (drp2 - dbp2) / 256) / 3 | ||
local l1, a1, b1 = Util.RGBtoLAB(a) | ||
local l2, a2, b2 = Util.RGBtoLAB(b) | ||
|
||
local delta = math.sqrt((l2 - l1) ^ 2 + (a2 - a1) ^ 2 + (b2 - b1) ^ 2) | ||
if delta < 30 then | ||
return 0.03 | ||
else | ||
return delta / 200 | ||
end | ||
end | ||
|
||
return Util |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters