-
Notifications
You must be signed in to change notification settings - Fork 1
/
oklch.js
46 lines (39 loc) · 1.11 KB
/
oklch.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
import { fromOklab, toOklab } from "./oklab.js";
import { lchToLab, labToLch } from "./utils.js";
/**
* @typedef {number[]} oklch Cylindrical form using D65 standard illuminant.
*
* All components in the range 0 <= x <= 1
* @see {@link https://drafts.csswg.org/css-color/#color-conversion-code}
*/
/**
* Updates a color based on Oklch values and alpha.
* @alias module:pex-color.fromOklch
* @param {import("./color.js").color} color
* @param {number} l
* @param {number} c
* @param {number} h
* @param {number} [a]
* @returns {import("./color.js").color}
*/
export function fromOklch(color, l, c, h, a) {
lchToLab(l, c, h, color);
// Range is [0, 150]
color[1] /= 1.5;
color[2] /= 1.5;
return fromOklab(color, color[0], color[1], color[2], a);
}
/**
* Returns an Oklch representation of a given color.
* @alias module:pex-color.toOklch
* @param {import("./color.js").color} color
* @param {Array} out
* @returns {oklch}
*/
export function toOklch(color, out = []) {
toOklab(color, out);
// Range is [0, 150]
out[1] *= 1.5;
out[2] *= 1.5;
return labToLch(out[0], out[1], out[2], out);
}