source: trip-planner-front/node_modules/@csstools/convert-colors/lib/lab-xyz.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.8 KB
RevLine 
[6a3a178]1import { epsilon, kappa, wd50X, wd50Y, wd50Z, matrix } from './util';
2
3/* Convert between Lab and XYZ
4/* ========================================================================== */
5
6export function lab2xyz(labL, labA, labB) {
7 // compute f, starting with the luminance-related term
8 const f2 = (labL + 16) / 116;
9 const f1 = labA / 500 + f2;
10 const f3 = f2 - labB / 200;
11
12 // compute pre-scaled XYZ
13 const [ initX, initY, initZ ] = [
14 Math.pow(f1, 3) > epsilon ? Math.pow(f1, 3) : (116 * f1 - 16) / kappa,
15 labL > kappa * epsilon ? Math.pow((labL + 16) / 116, 3) : labL / kappa,
16 Math.pow(f3, 3) > epsilon ? Math.pow(f3, 3) : (116 * f3 - 16) / kappa
17 ];
18
19 const [ xyzX, xyzY, xyzZ ] = matrix(
20 // compute XYZ by scaling pre-scaled XYZ by reference white
21 [ initX * wd50X, initY * wd50Y, initZ * wd50Z ],
22 // calculate D65 XYZ from D50 XYZ
23 [
24 [ 0.9555766, -0.0230393, 0.0631636],
25 [-0.0282895, 1.0099416, 0.0210077],
26 [ 0.0122982, -0.0204830, 1.3299098]
27 ]
28 );
29
30 return [ xyzX, xyzY, xyzZ ];
31}
32
33export function xyz2lab(xyzX, xyzY, xyzZ) {
34 // calculate D50 XYZ from D65 XYZ
35 const [ d50X, d50Y, d50Z ] = matrix([ xyzX, xyzY, xyzZ ], [
36 [ 1.0478112, 0.0228866, -0.0501270],
37 [ 0.0295424, 0.9904844, -0.0170491],
38 [-0.0092345, 0.0150436, 0.7521316]
39 ]);
40
41 // calculate f
42 const [ f1, f2, f3 ] = [
43 d50X / wd50X,
44 d50Y / wd50Y,
45 d50Z / wd50Z
46 ].map(
47 value => value > epsilon ? Math.cbrt(value) : (kappa * value + 16) / 116
48 );
49
50 const [ labL, labA, labB ] = [
51 116 * f2 - 16,
52 500 * (f1 - f2),
53 200 * (f2 - f3)
54 ];
55
56 return [ labL, labA, labB ];
57}
58
59/*
60
61References
62----------
63
64- https://www.w3.org/TR/css-color-4/#rgb-to-lab
65- https://www.w3.org/TR/css-color-4/#color-conversion-code
66- https://www.easyrgb.com/en/math.php
67
68/* ========================================================================== */
Note: See TracBrowser for help on using the repository browser.