Skip to content

Commit b75ead1

Browse files
Fix Oklab colour blending (#436)
1 parent 0c7284c commit b75ead1

1 file changed

Lines changed: 9 additions & 17 deletions

File tree

src/Colour/sRGB.luau

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,32 @@ local sRGB = {}
1616
-- Equivalent to f_inv. Takes a linear sRGB channel and returns
1717
-- the sRGB channel
1818
local function transform(channel: number): number
19-
if channel >= 0.04045 then
20-
return ((channel + 0.055)/(1 + 0.055))^2.4
21-
else
19+
if channel < 0.04045 then
2220
return channel / 12.92
2321
end
22+
23+
return ((channel + 0.055) / 1.055) ^ 2.4
2424
end
2525

2626
-- Equivalent to f. Takes an sRGB channel and returns
2727
-- the linear sRGB channel
2828
local function inverse(channel: number): number
29-
if channel >= 0.0031308 then
30-
return (1.055) * channel^(1.0/2.4) - 0.055
31-
else
29+
if channel < 0.0031308 then
3230
return 12.92 * channel
3331
end
32+
33+
return (1.055 * channel ^ (1 / 2.4)) - 0.055
3434
end
3535

3636
-- Uses a transformation to convert linear RGB into sRGB.
3737
function sRGB.fromLinear(rgb: Color3): Color3
38-
return Color3.new(
39-
transform(rgb.R),
40-
transform(rgb.G),
41-
transform(rgb.B)
42-
)
38+
return Color3.new(inverse(rgb.R), inverse(rgb.G), inverse(rgb.B))
4339
end
4440

4541
-- Converts an sRGB into linear RGB using a
4642
-- (The inverse of sRGB.fromLinear).
4743
function sRGB.toLinear(srgb: Color3): Color3
48-
return Color3.new(
49-
inverse(srgb.R),
50-
inverse(srgb.G),
51-
inverse(srgb.B)
52-
)
44+
return Color3.new(transform(srgb.R), transform(srgb.G), transform(srgb.B))
5345
end
5446

55-
return sRGB
47+
return sRGB

0 commit comments

Comments
 (0)