5
5
// sRGB-related functions
6
6
7
7
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]
9
10
// to linear light (un-companded) form.
10
11
// 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.
12
15
return RGB . map ( function ( val ) {
13
16
let sign = val < 0 ? - 1 : 1 ;
14
17
let abs = Math . abs ( val ) ;
@@ -25,6 +28,7 @@ function gam_sRGB(RGB) {
25
28
// convert an array of linear-light sRGB values in the range 0.0-1.0
26
29
// to gamma corrected form
27
30
// https://en.wikipedia.org/wiki/SRGB
31
+ // Extended transfer function:
28
32
// For negative values, linear portion extends on reflection
29
33
// of axis, then uses reflected pow below that
30
34
return RGB . map ( function ( val ) {
@@ -108,17 +112,21 @@ function XYZ_to_lin_P3(XYZ) {
108
112
// prophoto-rgb functions
109
113
110
114
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]
112
117
// to linear light (un-companded) form.
113
118
// 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
115
120
const Et2 = 16 / 512 ;
116
121
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 {
118
126
return val / 16 ;
119
127
}
120
128
121
- return Math . pow ( val , 1.8 ) ;
129
+ return sign * Math . pow ( val , 1.8 ) ;
122
130
} ) ;
123
131
}
124
132
@@ -129,8 +137,11 @@ function gam_ProPhoto(RGB) {
129
137
// TODO for negative values, extend linear portion on reflection of axis, then add pow below that
130
138
const Et = 1 / 512 ;
131
139
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 ) ;
134
145
}
135
146
136
147
return 16 * val ;
@@ -168,7 +179,10 @@ function lin_a98rgb(RGB) {
168
179
// to linear light (un-companded) form.
169
180
// negative values are also now accepted
170
181
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 ) ;
172
186
} ) ;
173
187
}
174
188
@@ -177,7 +191,10 @@ function gam_a98rgb(RGB) {
177
191
// to gamma corrected form
178
192
// negative values are also now accepted
179
193
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 ) ;
181
198
} ) ;
182
199
}
183
200
0 commit comments