Skip to content

Commit a4472c1

Browse files
committed
[css-color-4] update sRGB tranfer functions to reflect below zero
1 parent 5dc1198 commit a4472c1

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

css-color-4/conversions.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,29 @@ function lin_sRGB(RGB) {
1010
// https://en.wikipedia.org/wiki/SRGB
1111
// TODO for negative values, extend linear portion on reflection of axis, then add pow below that
1212
return RGB.map(function (val) {
13-
if (val < 0.04045) {
13+
let sign = val < 0? -1 : 1;
14+
let abs = Math.abs(val);
15+
16+
if (abs < 0.04045) {
1417
return val / 12.92;
1518
}
1619

17-
return Math.pow((val + 0.055) / 1.055, 2.4);
20+
return sign * Math.pow((abs + 0.055) / 1.055, 2.4);
1821
});
1922
}
2023

2124
function gam_sRGB(RGB) {
2225
// convert an array of linear-light sRGB values in the range 0.0-1.0
2326
// to gamma corrected form
2427
// https://en.wikipedia.org/wiki/SRGB
25-
// TODO for negative values, extend linear portion on reflection of axis, then add pow below that
28+
// For negative values, linear portion extends on reflection
29+
// of axis, then uses reflected pow below that
2630
return RGB.map(function (val) {
27-
if (val > 0.0031308) {
28-
return 1.055 * Math.pow(val, 1/2.4) - 0.055;
31+
let sign = val < 0? -1 : 1;
32+
let abs = Math.abs(val);
33+
34+
if (abs > 0.0031308) {
35+
return sign * 1.055 * Math.pow(abs, 1/2.4) - 0.055;
2936
}
3037

3138
return 12.92 * val;

0 commit comments

Comments
 (0)