11// sRGB-related functions
22
33function lin_sRGB ( RGB ) {
4- // convert an array of sRGB values in the range 0.0 - 1.0
4+ // convert an array of sRGB values in the range 0.0 - 1.0
55 // to linear light (un-companded) form.
66 // https://en.wikipedia.org/wiki/SRGB
77 return RGB . map ( function ( val ) {
88 if ( val < 0.04045 ) {
99 return val / 12.92 ;
1010 }
11-
11+
1212 return Math . pow ( ( val + 0.055 ) / 1.055 , 2.4 ) ;
1313 } ) ;
1414}
@@ -21,11 +21,11 @@ function gam_sRGB(RGB) {
2121 if ( val > 0.0031308 ) {
2222 return 1.055 * Math . pow ( val , 1 / 2.4 ) - 0.055 ;
2323 }
24-
24+
2525 return 12.92 * val ;
2626 } ) ;
2727}
28-
28+
2929function lin_sRGB_to_XYZ ( rgb ) {
3030 // convert an array of linear-light sRGB values to CIE XYZ
3131 // using sRGB's own white, D65 (no chromatic adaptation)
@@ -35,7 +35,7 @@ function lin_sRGB_to_XYZ(rgb) {
3535 [ 0.2126729 , 0.7151522 , 0.0721750 ] ,
3636 [ 0.0193339 , 0.1191920 , 0.9503041 ]
3737 ] ) ;
38-
38+
3939 return math . multiply ( M , rgb ) . valueOf ( ) ;
4040}
4141
@@ -46,15 +46,15 @@ function XYZ_to_lin_sRGB(XYZ) {
4646 [ - 0.9692660 , 1.8760108 , 0.0415560 ] ,
4747 [ 0.0556434 , - 0.2040259 , 1.0572252 ]
4848 ] ) ;
49-
49+
5050 return math . multiply ( M , XYZ ) . valueOf ( ) ;
5151}
5252
5353// DCI P3-related functions
5454
5555
5656function lin_P3 ( RGB ) {
57- // convert an array of DCI P3 RGB values in the range 0.0 - 1.0
57+ // convert an array of DCI P3 RGB values in the range 0.0 - 1.0
5858 // to linear light (un-companded) form.
5959
6060 return RGB . map ( function ( val ) {
@@ -65,23 +65,23 @@ function lin_P3(RGB) {
6565function gam_P3 ( RGB ) {
6666 // convert an array of linear-light P3 RGB in the range 0.0-1.0
6767 // to gamma corrected form
68-
68+
6969 return RGB . map ( function ( val ) {
7070 return Math . pow ( val , 1 / 2.6 ) ;
7171 } ) ;
7272}
73-
73+
7474function lin_P3_to_XYZ ( rgb ) {
7575 // convert an array of linear-light P3 values to CIE XYZ
7676 // using D65 (no chromatic adaptation)
7777 // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
7878 var M = math . matrix ( [
7979 [ 0.4865709486482162 , 0.26566769316909306 , 0.1982172852343625 ] ,
80- [ 0.2289745640697488 , 0.6917385218365064 , 0.079286914093745 ] ,
80+ [ 0.2289745640697488 , 0.6917385218365064 , 0.079286914093745 ] ,
8181 [ 0.0000000000000000 , 0.04511338185890264 , 1.043944368900976 ]
8282 ] ) ;
8383 // 0 was computed as -3.972075516933488e-17
84-
84+
8585 return math . multiply ( M , rgb ) . valueOf ( ) ;
8686}
8787
@@ -92,45 +92,45 @@ function XYZ_to_lin_P3(XYZ) {
9292 [ - 0.8294889695615747 , 1.7626640603183463 , 0.023624685841943577 ] ,
9393 [ 0.03584583024378447 , - 0.07617238926804182 , 0.9568845240076872 ]
9494 ] ) ;
95-
95+
9696 return math . multiply ( M , XYZ ) . valueOf ( ) ;
9797}
9898
99- //Rec.2020-related functions
99+ //Rec. 2020-related functions
100100
101101function lin_2020 ( RGB ) {
102- // convert an array of Rec.2020 RGB values in the range 0.0 - 1.0
102+ // convert an array of Rec. 2020 RGB values in the range 0.0 - 1.0
103103 // to linear light (un-companded) form.
104104 const α = 1.09929682680944 ;
105105 const β = 0.018053968510807 ;
106-
106+
107107 return RGB . map ( function ( val ) {
108108 if ( val < β * 4.5 ) {
109109 return val / 4.5 ;
110110 }
111-
111+
112112 return Math . pow ( ( val + α - 1 ) / α , 2.4 ) ;
113113 } ) ;
114114}
115115//check with standard this really is 2.4 and 1/2.4, not 0.45 was wikipedia claims
116116
117117function gam_2020 ( RGB ) {
118- // convert an array of linear-light Rec.2020 RGB in the range 0.0-1.0
118+ // convert an array of linear-light Rec. 2020 RGB in the range 0.0-1.0
119119 // to gamma corrected form
120120 const α = 1.09929682680944 ;
121121 const β = 0.018053968510807 ;
122-
122+
123123 return RGB . map ( function ( val ) {
124124 if ( val > β ) {
125125 return α * Math . pow ( val , 1 / 2.4 ) - ( α - 1 ) ;
126126 }
127-
127+
128128 return 4.5 * val ;
129129 } ) ;
130130}
131-
131+
132132function lin_2020_to_XYZ ( rgb ) {
133- // convert an array of linear-light rec. 2020 values to CIE XYZ
133+ // convert an array of linear-light Rec. 2020 values to CIE XYZ
134134 // using D65 (no chromatic adaptation)
135135 // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
136136 var M = math . matrix ( [
@@ -139,18 +139,18 @@ function lin_2020_to_XYZ(rgb) {
139139 [ 0.000000000000000 , 0.028072693049087428 , 1.060985057710791 ]
140140 ] ) ;
141141 // 0 is actually calculated as 4.994106574466076e-17
142-
142+
143143 return math . multiply ( M , rgb ) . valueOf ( ) ;
144144}
145145
146146function XYZ_to_lin_2020 ( XYZ ) {
147- // convert XYZ to linear-light rec. 2020
147+ // convert XYZ to linear-light Rec. 2020
148148 var M = math . matrix ( [
149149 [ 1.7166511879712674 , - 0.35567078377639233 , - 0.25336628137365974 ] ,
150150 [ - 0.6666843518324892 , 1.6164812366349395 , 0.01576854581391113 ] ,
151151 [ 0.017639857445310783 , - 0.042770613257808524 , 0.9421031212354738 ]
152152 ] ) ;
153-
153+
154154 return math . multiply ( M , XYZ ) . valueOf ( ) ;
155155}
156156
@@ -168,7 +168,7 @@ function D65_to_D50(XYZ) {
168168 [ 0.0295424 , 0.9904844 , - 0.0170491 ] ,
169169 [ - 0.0092345 , 0.0150436 , 0.7521316 ]
170170 ] ) ;
171-
171+
172172 return math . multiply ( M , XYZ ) . valueOf ( ) ;
173173}
174174
@@ -179,7 +179,7 @@ function D50_to_D65(XYZ) {
179179 [ - 0.0282895 , 1.0099416 , 0.0210077 ] ,
180180 [ 0.0122982 , - 0.0204830 , 1.3299098 ]
181181 ] ) ;
182-
182+
183183 return math . multiply ( M , XYZ ) . valueOf ( ) ;
184184}
185185
@@ -191,13 +191,13 @@ function XYZ_to_Lab(XYZ) {
191191 var ε = 216 / 24389 ; // 6^3/29^3
192192 var κ = 24389 / 27 ; // 29^3/3^3
193193 var white = [ 0.9642 , 1.0000 , 0.8249 ] ; // D50 reference white
194-
194+
195195 // compute xyz, which is XYZ scaled relative to reference white
196196 var xyz = XYZ . map ( ( value , i ) => value / white [ i ] ) ;
197-
197+
198198 // now compute f
199199 var f = xyz . map ( value => value > ε ? Math . cbrt ( value ) : ( κ * value + 16 ) / 116 ) ;
200-
200+
201201 return [
202202 ( 116 * f [ 1 ] ) - 16 , // L
203203 500 * ( f [ 0 ] - f [ 1 ] ) , // a
@@ -212,28 +212,28 @@ function Lab_to_XYZ(Lab) {
212212 var ε = 216 / 24389 ; // 6^3/29^3
213213 var white = [ 0.9642 , 1.0000 , 0.8249 ] ; // D50 reference white
214214 var f = [ ] ;
215-
215+
216216 // compute f, starting with the luminance-related term
217217 f [ 1 ] = ( Lab [ 0 ] + 16 ) / 116 ;
218218 f [ 0 ] = Lab [ 1 ] / 500 + f [ 1 ] ;
219219 f [ 2 ] = f [ 1 ] - Lab [ 2 ] / 200 ;
220-
220+
221221 // compute xyz
222222 var xyz = [
223223 Math . pow ( f [ 0 ] , 3 ) > ε ? Math . pow ( f [ 0 ] , 3 ) : ( 116 * f [ 0 ] - 16 ) / κ ,
224224 Lab [ 0 ] > κ * ε ? Math . pow ( ( Lab [ 0 ] + 16 ) / 116 , 3 ) : Lab [ 0 ] / κ ,
225225 Math . pow ( f [ 2 ] , 3 ) > ε ? Math . pow ( f [ 2 ] , 3 ) : ( 116 * f [ 2 ] - 16 ) / κ
226226 ] ;
227-
228- // Compute XYZ by scaling xyz by reference white
227+
228+ // Compute XYZ by scaling xyz by reference white
229229 return xyz . map ( ( value , i ) => value * white [ i ] ) ;
230230}
231231
232232function Lab_to_LCH ( Lab ) {
233233 // Convert to polar form
234234 return [
235235 Lab [ 0 ] , // L is still L
236- Math . sqrt ( Math . pow ( Lab [ 1 ] , 2 ) + Math . pow ( Lab [ 2 ] , 2 ) ) , // Chroma
236+ Math . sqrt ( Math . pow ( Lab [ 1 ] , 2 ) + Math . pow ( Lab [ 2 ] , 2 ) ) , // Chroma
237237 Math . atan2 ( Lab [ 2 ] , Lab [ 1 ] ) * 180 / Math . PI // Hue, in degrees
238238 ] ;
239239}
0 commit comments