Debian Codegraph @

node/colorjs.io/src/spaces/lab-d65.js

raw
1 import ColorSpace from "../ColorSpace.js";
2 import { WHITES } from "../adapt.js";
3 import xyz_d65 from "./xyz-d65.js";
4
5 // κ * ε = 2^3 = 8
6 const ε = 216 / 24389; // 6^3/29^3 == (24/116)^3
7 const ε3 = 24 / 116;
8 const κ = 24389 / 27; // 29^3/3^3
9
10 let white = WHITES.D65;
11
12 export default new ColorSpace({
13 id: "lab-d65",
14 name: "Lab D65",
15 coords: {
16 l: {
17 refRange: [0, 100],
18 name: "Lightness",
19 },
20 a: {
21 refRange: [-125, 125],
22 },
23 b: {
24 refRange: [-125, 125],
25 },
26 },
27
28 // Assuming XYZ is relative to D65, convert to CIE Lab
29 // from CIE standard, which now defines these as a rational fraction
30 white,
31
32 base: xyz_d65,
33 // Convert D65-adapted XYZ to Lab
34 // CIE 15.3:2004 section 8.2.1.1
35 fromBase (XYZ) {
36 // compute xyz, which is XYZ scaled relative to reference white
37 let xyz = XYZ.map((value, i) => value / white[i]);
38
39 // now compute f
40 let f = xyz.map(value => (value > ε ? Math.cbrt(value) : (κ * value + 16) / 116));
41
42 return [
43 116 * f[1] - 16, // L
44 500 * (f[0] - f[1]), // a
45 200 * (f[1] - f[2]), // b
46 ];
47 },
48 // Convert Lab to D65-adapted XYZ
49 // Same result as CIE 15.3:2004 Appendix D although the derivation is different
50 // http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
51 toBase (Lab) {
52 // compute f, starting with the luminance-related term
53 let f = [];
54 f[1] = (Lab[0] + 16) / 116;
55 f[0] = Lab[1] / 500 + f[1];
56 f[2] = f[1] - Lab[2] / 200;
57
58 // compute xyz
59 let xyz = [
60 f[0] > ε3 ? Math.pow(f[0], 3) : (116 * f[0] - 16) / κ,
61 Lab[0] > 8 ? Math.pow((Lab[0] + 16) / 116, 3) : Lab[0] / κ,
62 f[2] > ε3 ? Math.pow(f[2], 3) : (116 * f[2] - 16) / κ,
63 ];
64
65 // Compute XYZ by scaling xyz by reference white
66 return xyz.map((value, i) => value * white[i]);
67 },
68
69 formats: {
70 "lab-d65": {
71 coords: [
72 "<number> | <percentage>",
73 "<number> | <percentage>",
74 "<number> | <percentage>",
75 ],
76 },
77 },
78 });