Skip to content

Commit a57445b

Browse files
committed
[css-color-4] extended range on all transfer functions
1 parent 56cd618 commit a57445b

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

css-color-4/conversions.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
// sRGB-related functions
66

77
function lin_sRGB(RGB) {
8-
// convert an array of sRGB values in the range 0.0 - 1.0
8+
// convert an array of sRGB values
9+
// where in-gamut values are in the range [0 - 1]
910
// to linear light (un-companded) form.
1011
// https://en.wikipedia.org/wiki/SRGB
11-
// TODO for negative values, extend linear portion on reflection of axis, then add pow below that
12+
// Extended transfer function:
13+
// for negative values, linear portion is extended on reflection of axis,
14+
// then reflected power function is used.
1215
return RGB.map(function (val) {
1316
let sign = val < 0? -1 : 1;
1417
let abs = Math.abs(val);
@@ -25,6 +28,7 @@ function gam_sRGB(RGB) {
2528
// convert an array of linear-light sRGB values in the range 0.0-1.0
2629
// to gamma corrected form
2730
// https://en.wikipedia.org/wiki/SRGB
31+
// Extended transfer function:
2832
// For negative values, linear portion extends on reflection
2933
// of axis, then uses reflected pow below that
3034
return RGB.map(function (val) {
@@ -108,17 +112,21 @@ function XYZ_to_lin_P3(XYZ) {
108112
// prophoto-rgb functions
109113

110114
function lin_ProPhoto(RGB) {
111-
// convert an array of prophoto-rgb values in the range 0.0 - 1.0
115+
// convert an array of prophoto-rgb values
116+
// where in-gamut colors are in the range [0.0 - 1.0]
112117
// to linear light (un-companded) form.
113118
// Transfer curve is gamma 1.8 with a small linear portion
114-
// TODO for negative values, extend linear portion on reflection of axis, then add pow below that
119+
// Extended transfer function
115120
const Et2 = 16/512;
116121
return RGB.map(function (val) {
117-
if (val <= Et2) {
122+
let sign = val < 0? -1 : 1;
123+
let abs = Math.abs(val);
124+
125+
if (abs <= Et2 {
118126
return val / 16;
119127
}
120128

121-
return Math.pow(val, 1.8);
129+
return sign * Math.pow(val, 1.8);
122130
});
123131
}
124132

@@ -129,8 +137,11 @@ function gam_ProPhoto(RGB) {
129137
// TODO for negative values, extend linear portion on reflection of axis, then add pow below that
130138
const Et = 1/512;
131139
return RGB.map(function (val) {
132-
if (val >= Et) {
133-
return Math.pow(val, 1/1.8);
140+
let sign = val < 0? -1 : 1;
141+
let abs = Math.abs(val);
142+
143+
if (abs >= Et) {
144+
return sign * Math.pow(abs, 1/1.8);
134145
}
135146

136147
return 16 * val;
@@ -168,7 +179,10 @@ function lin_a98rgb(RGB) {
168179
// to linear light (un-companded) form.
169180
// negative values are also now accepted
170181
return RGB.map(function (val) {
171-
return Math.pow(Math.abs(val), 563/256)*Math.sign(val);
182+
let sign = val < 0? -1 : 1;
183+
let abs = Math.abs(val);
184+
185+
return sign * Math.pow(abs, 563/256);
172186
});
173187
}
174188

@@ -177,7 +191,10 @@ function gam_a98rgb(RGB) {
177191
// to gamma corrected form
178192
// negative values are also now accepted
179193
return RGB.map(function (val) {
180-
return Math.pow(Math.abs(val), 256/563)*Math.sign(val);
194+
let sign = val < 0? -1 : 1;
195+
let abs = Math.abs(val);
196+
197+
return sign * Math.pow(abs, 256/563);
181198
});
182199
}
183200

0 commit comments

Comments
 (0)