Last active
March 9, 2018 19:36
-
-
Save riking/69542dd7da8a380b9bfde477ebe2aa7d to your computer and use it in GitHub Desktop.
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
XYZA.EPSILON = math.pow(6 / 29, 3) | |
XYZA.KAPPA = math.pow(29 / 3, 3) | |
XYZA.WHITE = XYZA.fromRGBA(255,255,255,1) | |
function bit2linear(channel) | |
--[[ | |
http://www.brucelindbloom.com/Eqn_RGB_to_XYZ.html | |
This converts rgb 8bit to rgb linear, lazy because the other algorithm is really really dumb | |
]] | |
-- return Math.pow(channel, 2.2); -- use in shader cores | |
-- CSS Colors Level 4 says 0.03928, Bruce Lindbloom who cared to write all algos says 0.04045, used bruce because whynawt | |
if channel <= 0.04045 then | |
return channel / 12.92 | |
else | |
return math.pow((channel + 0.055) / 1.055, 2.4) | |
end | |
end | |
local LUVA = {} | |
LUVA.deltaGammaFactor = 1 / (XYZA.WHITE.x + 15 * XYZA.WHITE.y + 3 * XYZA.WHITE.z) | |
LUVA.uDeltaGamma = 4 * XYZA.WHITE.x * LUVA.deltaGammaFactor | |
LUVA.vDeltaGamma = 4 * XYZA.WHITE.y * LUVA.deltaGammaFactor | |
function LUVA.new(L,u,v,a) | |
n = {l=l, u=u, v=v, a=a} | |
setmetatable(n, LUVA) | |
return n | |
end | |
function LUVA.fromRGBA(r,g,b,a) | |
-- XYZA first | |
local R = bit2linear(r) | |
local G = bit2linear(g) | |
local B = bit2linear(b) | |
local X = 0.412453 * R + 0.357580 * G + 0.180423 * B | |
local Y = 0.212671 * R + 0.715160 * G + 0.072169 * B | |
local Z = 0.019334 * R + 0.119193 * G + 0.950227 * B | |
local yGamma = Y / XYZA.WHITE.y | |
local deltaDivider = X + 15 * Y + 3 * Z | |
if deltaDivider == 0 then deltaDivider = 1 end | |
local deltaFactor = 1 / deltaDivider | |
local uDelta = 4 * X * deltaFactor | |
local vDelta = 9 * Y * deltaFactor | |
local L | |
if yGamma > XYZA.EPSILON then | |
L = 116 * math.pow(yGamma, 1./3) - 16 | |
else | |
L = XYZA.KAPPA * yGamma | |
end | |
local u = 13 * L * (uDelta - LUVA.uDeltaGamma) | |
local v = 13 * L * (vDelta - LUVA.vDeltaGamma) | |
return LUVA.new(L, u, v, a) | |
end | |
return LUVA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
code ripped from SirStendec here https://github.com/FrankerFaceZ/FrankerFaceZ/blob/master/src/utilities/color.js#L59