color: Linear alpha in linear<->gamma conversions

Maintain linear alpha when converting to gamma.

Update to latest wikipedia links.

Add simple test that does the round trip conversion.
This commit is contained in:
David Briscoe 2022-03-27 01:06:41 -07:00
parent aec3b9fd7f
commit 0c12f17805
2 changed files with 12 additions and 6 deletions

View File

@ -294,7 +294,7 @@ function color.value(col, percent)
return hsv_to_color(c)
end
-- http://en.wikipedia.org/wiki/SRGB#The_reverse_transformation
-- https://en.wikipedia.org/wiki/SRGB#From_sRGB_to_CIE_XYZ
function color.gamma_to_linear(r, g, b, a)
local function convert(c)
if c > 1.0 then
@ -314,14 +314,14 @@ function color.gamma_to_linear(r, g, b, a)
c[i] = convert(r[i])
end
c[4] = convert(r[4])
c[4] = r[4]
return c
else
return convert(r), convert(g), convert(b), a or 1
end
end
-- http://en.wikipedia.org/wiki/SRGB#The_forward_transformation_.28CIE_xyY_or_CIE_XYZ_to_sRGB.29
-- https://en.wikipedia.org/wiki/SRGB#From_CIE_XYZ_to_sRGB
function color.linear_to_gamma(r, g, b, a)
local function convert(c)
if c > 1.0 then
@ -341,7 +341,7 @@ function color.linear_to_gamma(r, g, b, a)
c[i] = convert(r[i])
end
c[4] = convert(r[4])
c[4] = r[4]
return c
else
return convert(r), convert(g), convert(b), a or 1

View File

@ -169,10 +169,16 @@ describe("color:", function()
end
end)
it("linear_to_gamma -> gamma_to_linear round trip", function()
local c = color(0.25, 0.25, 0.25, 1)
local r = color.gamma_to_linear(c:linear_to_gamma())
for i=1,4 do
assert_is_approx_equal(c[i], r[i])
end
end)
end)
--[[
gamma_to_linear(r, g, b, a)
linear_to_gamma(r, g, b, a)
to_string(a)
--]]