source: node_modules/d3-color/src/lab.js

Last change on this file was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago

Prototype 1.1

  • Property mode set to 100644
File size: 3.5 KB
Line 
1import define, {extend} from "./define.js";
2import {Color, rgbConvert, Rgb} from "./color.js";
3import {degrees, radians} from "./math.js";
4
5// https://observablehq.com/@mbostock/lab-and-rgb
6const K = 18,
7 Xn = 0.96422,
8 Yn = 1,
9 Zn = 0.82521,
10 t0 = 4 / 29,
11 t1 = 6 / 29,
12 t2 = 3 * t1 * t1,
13 t3 = t1 * t1 * t1;
14
15function labConvert(o) {
16 if (o instanceof Lab) return new Lab(o.l, o.a, o.b, o.opacity);
17 if (o instanceof Hcl) return hcl2lab(o);
18 if (!(o instanceof Rgb)) o = rgbConvert(o);
19 var r = rgb2lrgb(o.r),
20 g = rgb2lrgb(o.g),
21 b = rgb2lrgb(o.b),
22 y = xyz2lab((0.2225045 * r + 0.7168786 * g + 0.0606169 * b) / Yn), x, z;
23 if (r === g && g === b) x = z = y; else {
24 x = xyz2lab((0.4360747 * r + 0.3850649 * g + 0.1430804 * b) / Xn);
25 z = xyz2lab((0.0139322 * r + 0.0971045 * g + 0.7141733 * b) / Zn);
26 }
27 return new Lab(116 * y - 16, 500 * (x - y), 200 * (y - z), o.opacity);
28}
29
30export function gray(l, opacity) {
31 return new Lab(l, 0, 0, opacity == null ? 1 : opacity);
32}
33
34export default function lab(l, a, b, opacity) {
35 return arguments.length === 1 ? labConvert(l) : new Lab(l, a, b, opacity == null ? 1 : opacity);
36}
37
38export function Lab(l, a, b, opacity) {
39 this.l = +l;
40 this.a = +a;
41 this.b = +b;
42 this.opacity = +opacity;
43}
44
45define(Lab, lab, extend(Color, {
46 brighter(k) {
47 return new Lab(this.l + K * (k == null ? 1 : k), this.a, this.b, this.opacity);
48 },
49 darker(k) {
50 return new Lab(this.l - K * (k == null ? 1 : k), this.a, this.b, this.opacity);
51 },
52 rgb() {
53 var y = (this.l + 16) / 116,
54 x = isNaN(this.a) ? y : y + this.a / 500,
55 z = isNaN(this.b) ? y : y - this.b / 200;
56 x = Xn * lab2xyz(x);
57 y = Yn * lab2xyz(y);
58 z = Zn * lab2xyz(z);
59 return new Rgb(
60 lrgb2rgb( 3.1338561 * x - 1.6168667 * y - 0.4906146 * z),
61 lrgb2rgb(-0.9787684 * x + 1.9161415 * y + 0.0334540 * z),
62 lrgb2rgb( 0.0719453 * x - 0.2289914 * y + 1.4052427 * z),
63 this.opacity
64 );
65 }
66}));
67
68function xyz2lab(t) {
69 return t > t3 ? Math.pow(t, 1 / 3) : t / t2 + t0;
70}
71
72function lab2xyz(t) {
73 return t > t1 ? t * t * t : t2 * (t - t0);
74}
75
76function lrgb2rgb(x) {
77 return 255 * (x <= 0.0031308 ? 12.92 * x : 1.055 * Math.pow(x, 1 / 2.4) - 0.055);
78}
79
80function rgb2lrgb(x) {
81 return (x /= 255) <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
82}
83
84function hclConvert(o) {
85 if (o instanceof Hcl) return new Hcl(o.h, o.c, o.l, o.opacity);
86 if (!(o instanceof Lab)) o = labConvert(o);
87 if (o.a === 0 && o.b === 0) return new Hcl(NaN, 0 < o.l && o.l < 100 ? 0 : NaN, o.l, o.opacity);
88 var h = Math.atan2(o.b, o.a) * degrees;
89 return new Hcl(h < 0 ? h + 360 : h, Math.sqrt(o.a * o.a + o.b * o.b), o.l, o.opacity);
90}
91
92export function lch(l, c, h, opacity) {
93 return arguments.length === 1 ? hclConvert(l) : new Hcl(h, c, l, opacity == null ? 1 : opacity);
94}
95
96export function hcl(h, c, l, opacity) {
97 return arguments.length === 1 ? hclConvert(h) : new Hcl(h, c, l, opacity == null ? 1 : opacity);
98}
99
100export function Hcl(h, c, l, opacity) {
101 this.h = +h;
102 this.c = +c;
103 this.l = +l;
104 this.opacity = +opacity;
105}
106
107function hcl2lab(o) {
108 if (isNaN(o.h)) return new Lab(o.l, 0, 0, o.opacity);
109 var h = o.h * radians;
110 return new Lab(o.l, Math.cos(h) * o.c, Math.sin(h) * o.c, o.opacity);
111}
112
113define(Hcl, hcl, extend(Color, {
114 brighter(k) {
115 return new Hcl(this.h, this.c, this.l + K * (k == null ? 1 : k), this.opacity);
116 },
117 darker(k) {
118 return new Hcl(this.h, this.c, this.l - K * (k == null ? 1 : k), this.opacity);
119 },
120 rgb() {
121 return hcl2lab(this).rgb();
122 }
123}));
Note: See TracBrowser for help on using the repository browser.