|
1 | 1 | <html> |
2 | 2 | <script src="math.js"></script> |
3 | 3 | <script> |
4 | | - |
| 4 | + |
5 | 5 | // Make matrices for converting to and from an arbitrary RGB colorspace, |
6 | 6 | // given the x,y chromaticities of red, green, blue and white. |
7 | | -// To use these matrices, the RGB components are assumed be be |
| 7 | +// To use these matrices, the RGB components are assumed be be |
8 | 8 | // linearized and in the range 0 to 1. |
9 | 9 | // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html |
10 | 10 |
|
|
13 | 13 |
|
14 | 14 |
|
15 | 15 | // these are for ITU Rec BT.2020 |
16 | | - /* |
| 16 | + /* |
17 | 17 | const xred=0.708 ; |
18 | 18 | const yred=0.292 ; |
19 | | - |
| 19 | +
|
20 | 20 | const xgreen=0.170 ; |
21 | 21 | const ygreen=0.797 ; |
22 | | - |
| 22 | +
|
23 | 23 | const xblue=0.131 ; |
24 | 24 | const yblue=0.046 ; |
25 | 25 | */ |
26 | | - |
| 26 | + |
27 | 27 | // these are for DCI P3 |
28 | | - const xred=0.680 ; |
29 | | - const yred=0.320 ; |
30 | | - |
31 | | - const xgreen=0.265 ; |
32 | | - const ygreen=0.690 ; |
33 | | - |
34 | | - const xblue=0.150 ; |
35 | | - const yblue=0.060 ; |
36 | | - |
| 28 | + // const xred=0.680 ; |
| 29 | + // const yred=0.320 ; |
| 30 | + |
| 31 | + // const xgreen=0.265 ; |
| 32 | + // const ygreen=0.690 ; |
| 33 | + |
| 34 | + // const xblue=0.150 ; |
| 35 | + // const yblue=0.060 ; |
| 36 | + |
| 37 | + // ProPhoto |
| 38 | + const xred=0.7347 ; |
| 39 | + const yred=0.2653 ; |
| 40 | + |
| 41 | + const xgreen=0.1596 ; |
| 42 | + const ygreen=0.8404 ; |
| 43 | + |
| 44 | + const xblue=0.0366 ; |
| 45 | + const yblue=0.0001 ; |
| 46 | + |
37 | 47 | // AdobeRGB, for testing |
38 | 48 | /* |
39 | 49 | const xred=0.640 ; |
40 | 50 | const yred=0.330 ; |
41 | | - |
| 51 | +
|
42 | 52 | const xgreen=0.210 ; |
43 | 53 | const ygreen=0.710 ; |
44 | | - |
| 54 | +
|
45 | 55 | const xblue=0.150 ; |
46 | 56 | const yblue=0.060 ; |
47 | 57 | */ |
48 | | - |
| 58 | + |
49 | 59 | // D65 |
50 | | - const xwhite=0.3127 ; |
51 | | - const ywhite=0.3290 ; |
52 | | - |
53 | | - |
| 60 | + // const xwhite=0.3127 ; |
| 61 | + // const ywhite=0.3290 ; |
| 62 | + |
| 63 | + // D50 |
| 64 | + const xwhite=0.3457 ; |
| 65 | + const ywhite=0.3585 ; |
| 66 | + |
| 67 | + |
54 | 68 | // Relative XYZ values. Copy-paste-o-rama |
55 | 69 | var XWhite=xwhite/ywhite; |
56 | 70 | var YWhite=1; |
57 | 71 | var ZWhite=(1 - xwhite - ywhite)/ywhite; |
58 | | - |
| 72 | + |
59 | 73 | var XRed=xred/yred; |
60 | 74 | var YRed=1; |
61 | 75 | var ZRed=(1 - xred - yred)/yred; |
62 | | - |
| 76 | + |
63 | 77 | var XGreen=xgreen/ygreen; |
64 | 78 | var YGreen=1; |
65 | 79 | var ZGreen=(1 - xgreen - ygreen)/ygreen; |
66 | | - |
| 80 | + |
67 | 81 | var XBlue=xblue/yblue; |
68 | 82 | var YBlue=1; |
69 | 83 | var ZBlue=(1 - xblue - yblue)/yblue; |
70 | | - |
| 84 | + |
71 | 85 | var white = math.matrix([XWhite, YWhite, ZWhite]); |
72 | 86 | console.log(white.valueOf()); |
73 | 87 | var primaries = math.matrix([ |
|
76 | 90 | [ZRed, ZGreen, ZBlue] |
77 | 91 | ]); |
78 | 92 | console.log(primaries.valueOf()); |
79 | | - |
| 93 | + |
80 | 94 | var iprimaries = math.inv(primaries); |
81 | 95 | console.log(iprimaries.valueOf()); |
82 | | - |
| 96 | + |
83 | 97 | // S is easier if it ends up as an array than a matrix |
84 | 98 | // but we use matrix math to calculate it |
85 | 99 | var S = math.multiply(math.inv(primaries),white).valueOf(); |
86 | | - |
| 100 | + |
87 | 101 | var M = [ |
88 | 102 | [S[0] * XRed, S[1] * XGreen, S[2] * XBlue], |
89 | 103 | [S[0] * YRed, S[1] * YGreen, S[2] * YBlue], |
90 | 104 | [S[0] * ZRed, S[1] * ZGreen, S[2] * ZBlue] |
91 | 105 | ]; |
92 | | - |
| 106 | + |
93 | 107 | console.log("RGB to XYZ"); |
94 | 108 | console.log(M); |
95 | 109 | console.log("XYZ to RGB"); |
96 | 110 | console.log(math.inv(math.matrix(M)).valueOf()); |
97 | | - |
| 111 | + |
98 | 112 | // for code verification: with AdobeRGB primaries, correct result is |
99 | | - // M = |
| 113 | + // M = |
100 | 114 | // [[0.5767309 0.1855540 0.1881852], |
101 | 115 | // [0.2973769 0.6273491 0.0752741], |
102 | 116 | // [0.0270343 0.0706872 0.9911085]]; |
|
0 commit comments