Debian Codegraph @

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

raw
1 import ColorSpace from "../ColorSpace.js";
2 import HSV from "./hsv.js";
3 import sRGB from "./srgb.js";
4
5 // The Hue, Whiteness Blackness (HWB) colorspace
6 // See https://drafts.csswg.org/css-color-4/#the-hwb-notation
7 // Note that, like HSL, calculations are done directly on
8 // gamma-corrected sRGB values rather than linearising them first.
9
10 export default new ColorSpace({
11 id: "hwb",
12 name: "HWB",
13 coords: {
14 h: {
15 refRange: [0, 360],
16 type: "angle",
17 name: "Hue",
18 },
19 w: {
20 range: [0, 100],
21 name: "Whiteness",
22 },
23 b: {
24 range: [0, 100],
25 name: "Blackness",
26 },
27 },
28
29 base: HSV,
30 rgbGamut: sRGB,
31 fromBase (hsv) {
32 let [h, s, v] = hsv;
33
34 return [h, (v * (100 - s)) / 100, 100 - v];
35 },
36 toBase (hwb) {
37 let [h, w, b] = hwb;
38
39 // Now convert percentages to [0..1]
40 w /= 100;
41 b /= 100;
42
43 // Achromatic check (white plus black >= 1)
44 let sum = w + b;
45 if (sum >= 1) {
46 let gray = w / sum;
47 return [h, 0, gray * 100];
48 }
49
50 let v = 1 - b;
51 let s = v === 0 ? 0 : 1 - w / v;
52 return [h, s * 100, v * 100];
53 },
54
55 formats: {
56 hwb: {
57 coords: ["<number> | <angle>", "<percentage> | <number>", "<percentage> | <number>"],
58 },
59 },
60 });