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