-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRGBtoLAB.js
50 lines (42 loc) · 1.23 KB
/
RGBtoLAB.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Thanks to http://www.easyrgb.com/en/math.php#text2 for formulas
'use strict'
const idunnowhatthisdoesRGBtoXYZ = (c) => {
if (c > 0.04045) {
return Math.pow(((c + 0.055) / 1.055), 2.4) * 100
}
return c / 12.92 * 100
}
const idunnowhatthisdoesXYZtoLAB = (c) => {
if (c > 0.008856) {
return Math.pow(c, 0.33333333333)
}
return 7.787 * c + 16 / 116
}
// smaller function == faster function?
const RGBtoXYZ = (rgb) => {
/* rgb is an array [r, g, b]
*/
const Rp = idunnowhatthisdoesRGBtoXYZ(rgb[0] / 255),
Gp = idunnowhatthisdoesRGBtoXYZ(rgb[1] / 255),
Bp = idunnowhatthisdoesRGBtoXYZ(rgb[2] / 255);
return [
Rp * 0.4124 + Gp * 0.3576 + Bp * 0.1805,
Rp * 0.2126 + Gp * 0.7152 + Bp * 0.0722,
Rp * 0.0193 + Gp * 0.1192 + Bp * 0.9505
]
}
const XYZtoLAB = (xyz) => {
/* reference colours, arbitrarily choosing mid morning daylight (from
* http://www.easyrgb.com/en/math.php#text2):
*/
// X2, Y2, Z10 constants
const Xp = idunnowhatthisdoesXYZtoLAB(xyz[0] / 95.799),
Yp = idunnowhatthisdoesXYZtoLAB(xyz[1] / 100),
Zp = idunnowhatthisdoesXYZtoLAB(xyz[2] / 90.926);
return [
(116 * Yp) - 16,
500 * (Xp - Yp),
200 * (Yp - Zp)
]
}
const RGBtoLAB = (rgb) => XYZtoLAB(RGBtoXYZ(rgb))