Debian Codegraph @

node/colorjs.io/src/spaces/oklab.js

raw
1 import ColorSpace from "../ColorSpace.js";
2 import { multiply_v3_m3x3 } from "../util.js";
3 import XYZ_D65 from "./xyz-d65.js";
4
5 /** @import { Matrix3x3 } from "../types.js" */
6
7 /**
8 * Matrices used by this color space, also available as `Oklab.M`.
9 * Recalculated for consistent reference white,
10 * see https://github.com/w3c/csswg-drafts/issues/6642#issuecomment-943521484
11 * @type {Record<string, Matrix3x3>}
12 */
13 // prettier-ignore
14 export const M = {
15 XYZtoLMS: [
16 [ 0.8190224379967030, 0.3619062600528904, -0.1288737815209879 ],
17 [ 0.0329836539323885, 0.9292868615863434, 0.0361446663506424 ],
18 [ 0.0481771893596242, 0.2642395317527308, 0.6335478284694309 ],
19 ],
20 // inverse of XYZtoLMS
21 LMStoXYZ: [
22 [ 1.2268798758459243, -0.5578149944602171, 0.2813910456659647 ],
23 [ -0.0405757452148008, 1.1122868032803170, -0.0717110580655164 ],
24 [ -0.0763729366746601, -0.4214933324022432, 1.5869240198367816 ],
25 ],
26 LMStoLab: [
27 [ 0.2104542683093140, 0.7936177747023054, -0.0040720430116193 ],
28 [ 1.9779985324311684, -2.4285922420485799, 0.4505937096174110 ],
29 [ 0.0259040424655478, 0.7827717124575296, -0.8086757549230774 ],
30 ],
31 // LMStoLab inverted
32 LabtoLMS: [
33 [ 1.0000000000000000, 0.3963377773761749, 0.2158037573099136 ],
34 [ 1.0000000000000000, -0.1055613458156586, -0.0638541728258133 ],
35 [ 1.0000000000000000, -0.0894841775298119, -1.2914855480194092 ],
36 ],
37 };
38
39 export default new ColorSpace({
40 id: "oklab",
41 name: "Oklab",
42 coords: {
43 l: {
44 refRange: [0, 1],
45 name: "Lightness",
46 },
47 a: {
48 refRange: [-0.4, 0.4],
49 },
50 b: {
51 refRange: [-0.4, 0.4],
52 },
53 },
54
55 // Note that XYZ is relative to D65
56 white: "D65",
57 base: XYZ_D65,
58 M,
59 fromBase (XYZ) {
60 // move to LMS cone domain
61 let LMS = multiply_v3_m3x3(XYZ, M.XYZtoLMS);
62
63 // non-linearity
64 LMS[0] = Math.cbrt(LMS[0]);
65 LMS[1] = Math.cbrt(LMS[1]);
66 LMS[2] = Math.cbrt(LMS[2]);
67
68 return multiply_v3_m3x3(LMS, M.LMStoLab, LMS);
69 },
70 toBase (OKLab) {
71 // move to LMS cone domain
72 let LMSg = multiply_v3_m3x3(OKLab, M.LabtoLMS);
73
74 // restore linearity
75 LMSg[0] = LMSg[0] ** 3;
76 LMSg[1] = LMSg[1] ** 3;
77 LMSg[2] = LMSg[2] ** 3;
78
79 return multiply_v3_m3x3(LMSg, M.LMStoXYZ, LMSg);
80 },
81
82 formats: {
83 oklab: {
84 coords: [
85 "<percentage> | <number>",
86 "<number> | <percentage>",
87 "<number> | <percentage>",
88 ],
89 },
90 },
91 });