-
Notifications
You must be signed in to change notification settings - Fork 79
/
CATs.js
146 lines (128 loc) · 4.29 KB
/
CATs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import hooks from "./hooks.js";
import {multiplyMatrices} from "./util.js";
import {WHITES} from "./adapt.js";
// Type "imports"
/** @typedef {import("./types.js").White} White */
/** @typedef {import("./types.js").CAT} CAT */
/** @type {Record<string, CAT>} */
export const CATs = {};
hooks.add("chromatic-adaptation-start", env => {
if (env.options.method) {
env.M = adapt(env.W1, env.W2, env.options.method);
}
});
hooks.add("chromatic-adaptation-end", env => {
if (!env.M) {
env.M = adapt(env.W1, env.W2, env.options.method);
}
});
/**
* @param {CAT} param0
*/
export function defineCAT ({id, toCone_M, fromCone_M}) {
// Use id, toCone_M, fromCone_M like variables
CATs[id] = arguments[0];
}
/**
*
* @param {White} W1
* @param {White} W2
* @param {string} id
* @returns {number[]}
*/
export function adapt (W1, W2, id = "Bradford") {
// adapt from a source whitepoint or illuminant W1
// to a destination whitepoint or illuminant W2,
// using the given chromatic adaptation transform (CAT)
// debugger;
let method = CATs[id];
let [ρs, γs, βs] = multiplyMatrices(method.toCone_M, W1);
let [ρd, γd, βd] = multiplyMatrices(method.toCone_M, W2);
// all practical illuminants have non-zero XYZ so no division by zero can occur below
let scale = [
[ρd / ρs, 0, 0 ],
[0, γd / γs, 0 ],
[0, 0, βd / βs],
];
// console.log({scale});
let scaled_cone_M = multiplyMatrices(scale, method.toCone_M);
let adapt_M = multiplyMatrices(method.fromCone_M, scaled_cone_M);
// console.log({scaled_cone_M, adapt_M});
return adapt_M;
}
defineCAT({
id: "von Kries",
toCone_M: [
[ 0.4002400, 0.7076000, -0.0808100 ],
[ -0.2263000, 1.1653200, 0.0457000 ],
[ 0.0000000, 0.0000000, 0.9182200 ],
],
fromCone_M: [
[ 1.8599363874558397, -1.1293816185800916, 0.21989740959619328 ],
[ 0.3611914362417676, 0.6388124632850422, -0.000006370596838649899 ],
[ 0, 0, 1.0890636230968613 ],
],
});
defineCAT({
id: "Bradford",
// Convert an array of XYZ values in the range 0.0 - 1.0
// to cone fundamentals
toCone_M: [
[ 0.8951000, 0.2664000, -0.1614000 ],
[ -0.7502000, 1.7135000, 0.0367000 ],
[ 0.0389000, -0.0685000, 1.0296000 ],
],
// and back
fromCone_M: [
[ 0.9869929054667121, -0.14705425642099013, 0.15996265166373122 ],
[ 0.4323052697233945, 0.5183602715367774, 0.049291228212855594 ],
[ -0.00852866457517732, 0.04004282165408486, 0.96848669578755 ],
],
});
defineCAT({
id: "CAT02",
// with complete chromatic adaptation to W2, so D = 1.0
toCone_M: [
[ 0.7328000, 0.4296000, -0.1624000 ],
[ -0.7036000, 1.6975000, 0.0061000 ],
[ 0.0030000, 0.0136000, 0.9834000 ],
],
fromCone_M: [
[ 1.0961238208355142, -0.27886900021828726, 0.18274517938277307 ],
[ 0.4543690419753592, 0.4735331543074117, 0.07209780371722911 ],
[ -0.009627608738429355, -0.00569803121611342, 1.0153256399545427 ],
],
});
defineCAT({
id: "CAT16",
toCone_M: [
[ 0.401288, 0.650173, -0.051461 ],
[ -0.250268, 1.204414, 0.045854 ],
[ -0.002079, 0.048952, 0.953127 ],
],
// the extra precision is needed to avoid roundtripping errors
fromCone_M: [
[ 1.862067855087233, -1.0112546305316845, 0.14918677544445172 ],
[ 0.3875265432361372, 0.6214474419314753, -0.008973985167612521 ],
[ -0.01584149884933386, -0.03412293802851557, 1.0499644368778496 ],
],
});
Object.assign(WHITES, {
// whitepoint values from ASTM E308-01 with 10nm spacing, 1931 2 degree observer
// all normalized to Y (luminance) = 1.00000
// Illuminant A is a tungsten electric light, giving a very warm, orange light.
A: [1.09850, 1.00000, 0.35585],
// Illuminant C was an early approximation to daylight: illuminant A with a blue filter.
C: [0.98074, 1.000000, 1.18232],
// The daylight series of illuminants simulate natural daylight.
// The color temperature (in degrees Kelvin/100) ranges from
// cool, overcast daylight (D50) to bright, direct sunlight (D65).
D55: [0.95682, 1.00000, 0.92149],
D75: [0.94972, 1.00000, 1.22638],
// Equal-energy illuminant, used in two-stage CAT16
E: [1.00000, 1.00000, 1.00000],
// The F series of illuminants represent fluorescent lights
F2: [0.99186, 1.00000, 0.67393],
F7: [0.95041, 1.00000, 1.08747],
F11: [1.00962, 1.00000, 0.64350],
});