Created
September 9, 2021 12:40
-
-
Save behreajj/377cb57ad2a8a6ad78c5e67ebc97f66b to your computer and use it in GitHub Desktop.
Convert sRGB AdobeRGB
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
-- Ported from the Python code of Mark Ransom | |
-- https://stackoverflow.com/a/40231268 | |
-- For more on the magic numbers, see | |
-- http://brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html | |
ColorConvert = {} | |
ColorConvert.__index = ColorConvert | |
setmetatable(ColorConvert, { | |
__call = function (cls, ...) | |
return cls.new(...) | |
end}) | |
function ColorConvert.linear_sRGB(c) | |
if c <= 0.04045 then | |
return c / 12.92 | |
else | |
return ((c + 0.055) / 1.055) ^ 2.4 | |
end | |
end | |
function ColorConvert.sRGB_to_XYZn(r, g, b) | |
local Rlin = ColorConvert.linear_sRGB(r / 255.0) | |
local Glin = ColorConvert.linear_sRGB(g / 255.0) | |
local Blin = ColorConvert.linear_sRGB(b / 255.0) | |
local Xn = Rlin * 0.4124 + Glin * 0.3576 + Blin * 0.1805 | |
local Yn = Rlin * 0.2126 + Glin * 0.7152 + Blin * 0.0722 | |
local Zn = Rlin * 0.0193 + Glin * 0.1192 + Blin * 0.9505 | |
return Xn, Yn, Zn | |
end | |
function ColorConvert.gamma_sRGB(c) | |
if c <= 0.0031308 then | |
return 12.92 * c | |
else | |
return 1.055 * (c ^ (1 / 2.4)) - 0.055 | |
end | |
end | |
function ColorConvert.XYZn_to_sRGB(Xn, Yn, Zn) | |
local Rlin = Xn * 3.2406255 + Yn * -1.5372080 + Zn * -0.4986286 | |
local Glin = Xn * -0.9689307 + Yn * 1.8757561 + Zn * 0.0415175 | |
local Blin = Xn * 0.0557101 + Yn * -0.2040211 + Zn * 1.0569959 | |
local R = 255 * ColorConvert.gamma_sRGB(Rlin) | |
local G = 255 * ColorConvert.gamma_sRGB(Glin) | |
local B = 255 * ColorConvert.gamma_sRGB(Blin) | |
if R < -0.0 then R = R - 0.5 elseif R > 0.0 then R = R + 0.5 end | |
if G < -0.0 then G = G - 0.5 elseif G > 0.0 then G = G + 0.5 end | |
if B < -0.0 then B = B - 0.5 elseif B > 0.0 then B = B + 0.5 end | |
R = math.tointeger(R) | |
G = math.tointeger(G) | |
B = math.tointeger(B) | |
return R, G, B | |
end | |
function ColorConvert.linear_AdobeRGB(c) | |
if c <= 0.0 then | |
return 0.0 | |
else | |
return c ^ 2.19921875 | |
end | |
end | |
function ColorConvert.AdobeRGB_to_XYZn(R, G, B) | |
local Rlin = ColorConvert.linear_AdobeRGB(R / 255.0) | |
local Glin = ColorConvert.linear_AdobeRGB(G / 255.0) | |
local Blin = ColorConvert.linear_AdobeRGB(B / 255.0) | |
local Xn = Rlin * 0.57667 + Glin * 0.18556 + Blin * 0.18823 | |
local Yn = Rlin * 0.29734 + Glin * 0.62736 + Blin * 0.07529 | |
local Zn = Rlin * 0.02703 + Glin * 0.07069 + Blin * 0.99134 | |
return Xn, Yn, Zn | |
end | |
function ColorConvert.gamma_AdobeRGB(c) | |
if c <= 0.0 then | |
return 0.0 | |
else | |
return c ^ (1 / 2.19921875) | |
end | |
end | |
function ColorConvert.XYZn_to_AdobeRGB(Xn, Yn, Zn) | |
local Rlin = Xn * 2.04159 + Yn * -0.56501 + Zn * -0.34473 | |
local Glin = Xn * -0.96924 + Yn * 1.87597 + Zn * 0.04156 | |
local Blin = Xn * 0.01344 + Yn * -0.11836 + Zn * 1.01517 | |
local R = 255 * ColorConvert.gamma_AdobeRGB(Rlin) | |
local G = 255 * ColorConvert.gamma_AdobeRGB(Glin) | |
local B = 255 * ColorConvert.gamma_AdobeRGB(Blin) | |
if R < -0.0 then R = R - 0.5 elseif R > 0.0 then R = R + 0.5 end | |
if G < -0.0 then G = G - 0.5 elseif G > 0.0 then G = G + 0.5 end | |
if B < -0.0 then B = B - 0.5 elseif B > 0.0 then B = B + 0.5 end | |
R = math.tointeger(R) | |
G = math.tointeger(G) | |
B = math.tointeger(B) | |
return R, G, B | |
end | |
function ColorConvert.sRGB_to_AdobeRGB(r, g, b) | |
Xn, Yn, Zn = ColorConvert.sRGB_to_XYZn(r, g, b) | |
return ColorConvert.XYZn_to_AdobeRGB(Xn, Yn, Zn) | |
end | |
function ColorConvert.AdobeRGB_to_sRGB(r, g, b) | |
Xn, Yn, Zn = ColorConvert.AdobeRGB_to_XYZn(r, g, b) | |
return ColorConvert.XYZn_to_sRGB(Xn, Yn, Zn) | |
end | |
return ColorConvert |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment