1+ < html >
2+ < script src ="math.js "> </ script >
3+ < script >
4+
15// Make matrices for converting to and from an arbitrary RGB colorspace,
26// given the x,y chromaticities of red, green, blue and white.
37// To use these matrices, the RGB components are assumed be be
711// Horribly hacky, non interactive utility. Edit the file to pick
812// another colorspace and read the results from the console :P
913
10- < html >
11- < script src ="math.js "> </ script >
12- < script >
14+
1315 // these are for ITU Rec BT.2020
16+ /*
1417 const xred=0.708 ;
1518 const yred=0.292 ;
1619
1922
2023 const xblue=0.131 ;
2124 const yblue=0.046 ;
25+ */
26+
27+ // AdobeRGB, for testing
28+ const xred = 0.640 ;
29+ const yred = 0.330 ;
30+
31+ const xgreen = 0.210 ;
32+ const ygreen = 0.710 ;
33+
34+ const xblue = 0.150 ;
35+ const yblue = 0.060 ;
2236
2337 // D65
2438 const xwhite = 0.3127 ;
4357 var ZBlue = ( 1 - xblue - yblue ) / yblue ;
4458
4559 var white = math . matrix ( [ XWhite , YWhite , ZWhite ] ) ;
46- var primaries = [
60+ console . log ( white . valueOf ( ) ) ;
61+ var primaries = math . matrix ( [
4762 [ XRed , XGreen , XBlue ] ,
4863 [ YRed , YGreen , YBlue ] ,
4964 [ ZRed , ZGreen , ZBlue ]
50- ] ;
65+ ] ) ;
66+ console . log ( primaries . valueOf ( ) ) ;
67+
68+ var iprimaries = math . inv ( primaries ) ;
69+ console . log ( iprimaries . valueOf ( ) ) ;
5170
5271 // S is easier if it ends up as an array than a matrix
5372 // but we use matrix math to calculate it
54- var S = math . multiply ( math . inv ( math . matrix ( primaries ) ) , white ) . value ;
73+ var S = math . multiply ( math . inv ( primaries ) , white ) . valueOf ( ) ;
5574
56- var M = math . matrix ( [
57- [ ]
58- ] ) ;
75+ var M = [
76+ [ S [ 0 ] * XRed , S [ 1 ] * XGreen , S [ 2 ] * XBlue ] ,
77+ [ S [ 0 ] * YRed , S [ 1 ] * YGreen , S [ 2 ] * YBlue ] ,
78+ [ S [ 0 ] * ZRed , S [ 1 ] * ZGreen , S [ 2 ] * ZBlue ]
79+ ] ;
80+
81+ console . log ( "RGB to XYZ" ) ;
82+ console . log ( M ) ;
83+ console . log ( "XYZ to RGB" ) ;
84+ console . log ( math . inv ( math . matrix ( M ) ) . valueOf ( ) ) ;
5985
86+ // for code verification: with AdobeRGB primaries, correct result is
87+ // M =
88+ // [[0.5767309 0.1855540 0.1881852],
89+ // [0.2973769 0.6273491 0.0752741],
90+ // [0.0270343 0.0706872 0.9911085]];
91+ // Minv =
92+ // [[2.0413690 -0.5649464 -0.3446944],
93+ // [-0.9692660 1.8760108 0.0415560],
94+ // [0.0134474 -0.1183897 1.0154096]];
6095 </ script >
6196</ html >
0 commit comments