Skip to content

Commit

Permalink
Add color util to convert sRGB to XYZ
Browse files Browse the repository at this point in the history
  • Loading branch information
theJian committed Mar 18, 2024
1 parent 93c4814 commit 8ded9b0
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions test/color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ local M = {}

local SA98G = {
mainTRC = 2.4, -- 2.4 exponent for emulating actual monitor perception
-- sRGB coefficients
sRco = 0.2126729,
sGco = 0.7151522,
sBco = 0.0721750,

-- G-4g constants for use with 2.4 exponent
normBG = 0.56,
normTXT = 0.57,
Expand All @@ -27,6 +22,21 @@ local SA98G = {
loClip = 0.1,
}

-- sRGB coefficients
local RGBCO = {
sRcoX = 0.4124564,
sGcoX = 0.3575761,
sBcoX = 0.1804375,

sRcoY = 0.2126729,
sGcoY = 0.7151522,
sBcoY = 0.0721750,

sRcoZ = 0.0193339,
sGcoZ = 0.1191920,
sBcoZ = 0.9503041,
}

function M.rgb(num)
local r, g, b = rshift(num, 16), band(rshift(num, 8), 0x00ff), band(num, 0x0000ff)
return { r, g, b }
Expand All @@ -48,7 +58,7 @@ function M.sRGB2y(rgb)
local function simple_exp(chan)
return math.pow(chan / 255.0, SA98G.mainTRC)
end
return SA98G.sRco * simple_exp(rgb[1]) + SA98G.sGco * simple_exp(rgb[2]) + SA98G.sBco * simple_exp(rgb[3])
return RGBCO.sRcoY * simple_exp(rgb[1]) + RGBCO.sGcoY * simple_exp(rgb[2]) + RGBCO.sBcoY * simple_exp(rgb[3])
end

-- @ref: https://github.com/Myndex/apca-w3/blob/master/src/apca-w3.js
Expand Down Expand Up @@ -97,4 +107,21 @@ function M.calc_apca(text_color, bg_color)
return M.apca_contrast(M.sRGB2y(tx_clr), M.sRGB2y(bg_clr))
end

function M.sRGB2XYZ(rgb)
for i = 1,3 do
local c = rgb[i] / 255.0
if c > 0.04045 then
rgb[i] = math.pow((c + 0.055) / 1.055, SA98G.mainTRC)
else
rgb[i] = c / 12.92
end
rgb[i] = rgb[i] * 100
end

local x = RGBCO.sRcoX * rgb[1] + RGBCO.sGcoX * rgb[2] + RGBCO.sBcoX * rgb[3]
local y = RGBCO.sRcoY * rgb[1] + RGBCO.sGcoY * rgb[2] + RGBCO.sBcoY * rgb[3]
local z = RGBCO.sRcoZ * rgb[1] + RGBCO.sGcoZ * rgb[2] + RGBCO.sBcoZ * rgb[3]
return { x, y, z }
end

return M

0 comments on commit 8ded9b0

Please sign in to comment.