@sdxc/color 0.0.0-pre.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +223 -0
- package/dist/color-parse-error.d.ts +18 -0
- package/dist/color-parse-error.js +22 -0
- package/dist/color.d.ts +182 -0
- package/dist/color.js +346 -0
- package/dist/contrast-unreachable-error.d.ts +24 -0
- package/dist/contrast-unreachable-error.js +21 -0
- package/dist/contrast.d.ts +35 -0
- package/dist/contrast.js +15 -0
- package/dist/gamut.d.ts +19 -0
- package/dist/gamut.js +68 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +3 -0
- package/dist/interpolate.d.ts +29 -0
- package/dist/interpolate.js +79 -0
- package/dist/named.d.ts +10 -0
- package/dist/named.js +159 -0
- package/dist/parse.d.ts +20 -0
- package/dist/parse.js +272 -0
- package/dist/spaces.d.ts +65 -0
- package/dist/spaces.js +254 -0
- package/package.json +31 -0
package/dist/named.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The CSS Color 4 named colors as packed `0xRRGGBB` numbers, the table `Color.parse`
|
|
3
|
+
* reads a keyword from. `transparent` is absent because it alone carries alpha, so the
|
|
4
|
+
* parser answers it directly.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/** Every CSS named color, lower-cased, mapped to its sRGB value packed as `0xRRGGBB`. */
|
|
10
|
+
export const NAMED_COLORS = {
|
|
11
|
+
aliceblue: 0xf0f8ff,
|
|
12
|
+
antiquewhite: 0xfaebd7,
|
|
13
|
+
aqua: 0x00ffff,
|
|
14
|
+
aquamarine: 0x7fffd4,
|
|
15
|
+
azure: 0xf0ffff,
|
|
16
|
+
beige: 0xf5f5dc,
|
|
17
|
+
bisque: 0xffe4c4,
|
|
18
|
+
black: 0x000000,
|
|
19
|
+
blanchedalmond: 0xffebcd,
|
|
20
|
+
blue: 0x0000ff,
|
|
21
|
+
blueviolet: 0x8a2be2,
|
|
22
|
+
brown: 0xa52a2a,
|
|
23
|
+
burlywood: 0xdeb887,
|
|
24
|
+
cadetblue: 0x5f9ea0,
|
|
25
|
+
chartreuse: 0x7fff00,
|
|
26
|
+
chocolate: 0xd2691e,
|
|
27
|
+
coral: 0xff7f50,
|
|
28
|
+
cornflowerblue: 0x6495ed,
|
|
29
|
+
cornsilk: 0xfff8dc,
|
|
30
|
+
crimson: 0xdc143c,
|
|
31
|
+
cyan: 0x00ffff,
|
|
32
|
+
darkblue: 0x00008b,
|
|
33
|
+
darkcyan: 0x008b8b,
|
|
34
|
+
darkgoldenrod: 0xb8860b,
|
|
35
|
+
darkgray: 0xa9a9a9,
|
|
36
|
+
darkgreen: 0x006400,
|
|
37
|
+
darkgrey: 0xa9a9a9,
|
|
38
|
+
darkkhaki: 0xbdb76b,
|
|
39
|
+
darkmagenta: 0x8b008b,
|
|
40
|
+
darkolivegreen: 0x556b2f,
|
|
41
|
+
darkorange: 0xff8c00,
|
|
42
|
+
darkorchid: 0x9932cc,
|
|
43
|
+
darkred: 0x8b0000,
|
|
44
|
+
darksalmon: 0xe9967a,
|
|
45
|
+
darkseagreen: 0x8fbc8f,
|
|
46
|
+
darkslateblue: 0x483d8b,
|
|
47
|
+
darkslategray: 0x2f4f4f,
|
|
48
|
+
darkslategrey: 0x2f4f4f,
|
|
49
|
+
darkturquoise: 0x00ced1,
|
|
50
|
+
darkviolet: 0x9400d3,
|
|
51
|
+
deeppink: 0xff1493,
|
|
52
|
+
deepskyblue: 0x00bfff,
|
|
53
|
+
dimgray: 0x696969,
|
|
54
|
+
dimgrey: 0x696969,
|
|
55
|
+
dodgerblue: 0x1e90ff,
|
|
56
|
+
firebrick: 0xb22222,
|
|
57
|
+
floralwhite: 0xfffaf0,
|
|
58
|
+
forestgreen: 0x228b22,
|
|
59
|
+
fuchsia: 0xff00ff,
|
|
60
|
+
gainsboro: 0xdcdcdc,
|
|
61
|
+
ghostwhite: 0xf8f8ff,
|
|
62
|
+
gold: 0xffd700,
|
|
63
|
+
goldenrod: 0xdaa520,
|
|
64
|
+
gray: 0x808080,
|
|
65
|
+
green: 0x008000,
|
|
66
|
+
greenyellow: 0xadff2f,
|
|
67
|
+
grey: 0x808080,
|
|
68
|
+
honeydew: 0xf0fff0,
|
|
69
|
+
hotpink: 0xff69b4,
|
|
70
|
+
indianred: 0xcd5c5c,
|
|
71
|
+
indigo: 0x4b0082,
|
|
72
|
+
ivory: 0xfffff0,
|
|
73
|
+
khaki: 0xf0e68c,
|
|
74
|
+
lavender: 0xe6e6fa,
|
|
75
|
+
lavenderblush: 0xfff0f5,
|
|
76
|
+
lawngreen: 0x7cfc00,
|
|
77
|
+
lemonchiffon: 0xfffacd,
|
|
78
|
+
lightblue: 0xadd8e6,
|
|
79
|
+
lightcoral: 0xf08080,
|
|
80
|
+
lightcyan: 0xe0ffff,
|
|
81
|
+
lightgoldenrodyellow: 0xfafad2,
|
|
82
|
+
lightgray: 0xd3d3d3,
|
|
83
|
+
lightgreen: 0x90ee90,
|
|
84
|
+
lightgrey: 0xd3d3d3,
|
|
85
|
+
lightpink: 0xffb6c1,
|
|
86
|
+
lightsalmon: 0xffa07a,
|
|
87
|
+
lightseagreen: 0x20b2aa,
|
|
88
|
+
lightskyblue: 0x87cefa,
|
|
89
|
+
lightslategray: 0x778899,
|
|
90
|
+
lightslategrey: 0x778899,
|
|
91
|
+
lightsteelblue: 0xb0c4de,
|
|
92
|
+
lightyellow: 0xffffe0,
|
|
93
|
+
lime: 0x00ff00,
|
|
94
|
+
limegreen: 0x32cd32,
|
|
95
|
+
linen: 0xfaf0e6,
|
|
96
|
+
magenta: 0xff00ff,
|
|
97
|
+
maroon: 0x800000,
|
|
98
|
+
mediumaquamarine: 0x66cdaa,
|
|
99
|
+
mediumblue: 0x0000cd,
|
|
100
|
+
mediumorchid: 0xba55d3,
|
|
101
|
+
mediumpurple: 0x9370db,
|
|
102
|
+
mediumseagreen: 0x3cb371,
|
|
103
|
+
mediumslateblue: 0x7b68ee,
|
|
104
|
+
mediumspringgreen: 0x00fa9a,
|
|
105
|
+
mediumturquoise: 0x48d1cc,
|
|
106
|
+
mediumvioletred: 0xc71585,
|
|
107
|
+
midnightblue: 0x191970,
|
|
108
|
+
mintcream: 0xf5fffa,
|
|
109
|
+
mistyrose: 0xffe4e1,
|
|
110
|
+
moccasin: 0xffe4b5,
|
|
111
|
+
navajowhite: 0xffdead,
|
|
112
|
+
navy: 0x000080,
|
|
113
|
+
oldlace: 0xfdf5e6,
|
|
114
|
+
olive: 0x808000,
|
|
115
|
+
olivedrab: 0x6b8e23,
|
|
116
|
+
orange: 0xffa500,
|
|
117
|
+
orangered: 0xff4500,
|
|
118
|
+
orchid: 0xda70d6,
|
|
119
|
+
palegoldenrod: 0xeee8aa,
|
|
120
|
+
palegreen: 0x98fb98,
|
|
121
|
+
paleturquoise: 0xafeeee,
|
|
122
|
+
palevioletred: 0xdb7093,
|
|
123
|
+
papayawhip: 0xffefd5,
|
|
124
|
+
peachpuff: 0xffdab9,
|
|
125
|
+
peru: 0xcd853f,
|
|
126
|
+
pink: 0xffc0cb,
|
|
127
|
+
plum: 0xdda0dd,
|
|
128
|
+
powderblue: 0xb0e0e6,
|
|
129
|
+
purple: 0x800080,
|
|
130
|
+
rebeccapurple: 0x663399,
|
|
131
|
+
red: 0xff0000,
|
|
132
|
+
rosybrown: 0xbc8f8f,
|
|
133
|
+
royalblue: 0x4169e1,
|
|
134
|
+
saddlebrown: 0x8b4513,
|
|
135
|
+
salmon: 0xfa8072,
|
|
136
|
+
sandybrown: 0xf4a460,
|
|
137
|
+
seagreen: 0x2e8b57,
|
|
138
|
+
seashell: 0xfff5ee,
|
|
139
|
+
sienna: 0xa0522d,
|
|
140
|
+
silver: 0xc0c0c0,
|
|
141
|
+
skyblue: 0x87ceeb,
|
|
142
|
+
slateblue: 0x6a5acd,
|
|
143
|
+
slategray: 0x708090,
|
|
144
|
+
slategrey: 0x708090,
|
|
145
|
+
snow: 0xfffafa,
|
|
146
|
+
springgreen: 0x00ff7f,
|
|
147
|
+
steelblue: 0x4682b4,
|
|
148
|
+
tan: 0xd2b48c,
|
|
149
|
+
teal: 0x008080,
|
|
150
|
+
thistle: 0xd8bfd8,
|
|
151
|
+
tomato: 0xff6347,
|
|
152
|
+
turquoise: 0x40e0d0,
|
|
153
|
+
violet: 0xee82ee,
|
|
154
|
+
wheat: 0xf5deb3,
|
|
155
|
+
white: 0xffffff,
|
|
156
|
+
whitesmoke: 0xf5f5f5,
|
|
157
|
+
yellow: 0xffff00,
|
|
158
|
+
yellowgreen: 0x9acd32,
|
|
159
|
+
};
|
package/dist/parse.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The CSS color grammar `Color.parse` reads: hex, named colors, `rgb()`, `hsl()`, `hwb()`,
|
|
3
|
+
* `lab()`, `lch()`, `oklab()`, `oklch()` and `color()` in its predefined spaces. It turns
|
|
4
|
+
* each into extended sRGB channels and answers a `Result`, so bad input never throws.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Result } from "@sdxc/result";
|
|
10
|
+
import type { Rgb } from "./color.js";
|
|
11
|
+
import { ColorParseError } from "./color-parse-error.js";
|
|
12
|
+
/**
|
|
13
|
+
* Reads CSS color text into extended sRGB channels and alpha. Case and surrounding
|
|
14
|
+
* whitespace are ignored; values CSS clamps at parse time (rgb channels, alpha, HSL and
|
|
15
|
+
* Lab lightness, negative chroma) are clamped, and every other value is kept as written.
|
|
16
|
+
*
|
|
17
|
+
* @param input - Any CSS `<color>` except `currentcolor`, system colors and relative syntax.
|
|
18
|
+
* @returns The channels, or a `ColorParseError` that names the rejected text.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseColor(input: string): Result<Rgb, ColorParseError>;
|
package/dist/parse.js
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
import { failure, success } from "@sdxc/result";
|
|
2
|
+
import { ColorParseError } from "./color-parse-error.js";
|
|
3
|
+
import { NAMED_COLORS } from "./named.js";
|
|
4
|
+
import { RGB_SPACES, hslToSrgb, hwbToSrgb, labToSrgb, lchToLab, oklabToSrgb, oklchToOklab, rgbSpaceToSrgb, } from "./spaces.js";
|
|
5
|
+
/** A CSS number with an optional unit; `none` is handled before this matches. */
|
|
6
|
+
const TOKEN = /^([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(%|deg|rad|grad|turn)?$/;
|
|
7
|
+
/** A CSS function call, capturing its name and everything between the parentheses. */
|
|
8
|
+
const FUNCTION = /^([a-z][a-z0-9-]*)\((.*)\)$/s;
|
|
9
|
+
/** A hex color in each of its four lengths. */
|
|
10
|
+
const HEX = /^#(?:[\da-f]{3,4}|[\da-f]{6}|[\da-f]{8})$/;
|
|
11
|
+
/** The functions whose arguments may also be written comma-separated. */
|
|
12
|
+
const LEGACY_FUNCTIONS = new Set(["rgb", "rgba", "hsl", "hsla"]);
|
|
13
|
+
/** Degrees per unit of each CSS angle unit, with a bare number read as degrees. */
|
|
14
|
+
const DEGREES_PER_UNIT = {
|
|
15
|
+
"": 1,
|
|
16
|
+
deg: 1,
|
|
17
|
+
grad: 0.9,
|
|
18
|
+
rad: 180 / Math.PI,
|
|
19
|
+
turn: 360,
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Reads CSS color text into extended sRGB channels and alpha. Case and surrounding
|
|
23
|
+
* whitespace are ignored; values CSS clamps at parse time (rgb channels, alpha, HSL and
|
|
24
|
+
* Lab lightness, negative chroma) are clamped, and every other value is kept as written.
|
|
25
|
+
*
|
|
26
|
+
* @param input - Any CSS `<color>` except `currentcolor`, system colors and relative syntax.
|
|
27
|
+
* @returns The channels, or a `ColorParseError` that names the rejected text.
|
|
28
|
+
*/
|
|
29
|
+
export function parseColor(input) {
|
|
30
|
+
let text = input.trim().toLowerCase();
|
|
31
|
+
let parsed = readColor(text);
|
|
32
|
+
if (typeof parsed === "string")
|
|
33
|
+
return failure(new ColorParseError(input, parsed));
|
|
34
|
+
return success(parsed);
|
|
35
|
+
}
|
|
36
|
+
/** The channels `text` names, or the reason it names none. */
|
|
37
|
+
function readColor(text) {
|
|
38
|
+
if (text === "")
|
|
39
|
+
return "empty input";
|
|
40
|
+
if (text.startsWith("#"))
|
|
41
|
+
return readHex(text);
|
|
42
|
+
if (text === "transparent")
|
|
43
|
+
return { r: 0, g: 0, b: 0, alpha: 0 };
|
|
44
|
+
let named = NAMED_COLORS[text];
|
|
45
|
+
if (named !== undefined) {
|
|
46
|
+
return {
|
|
47
|
+
r: (named >> 16) / 255,
|
|
48
|
+
g: ((named >> 8) & 0xff) / 255,
|
|
49
|
+
b: (named & 0xff) / 255,
|
|
50
|
+
alpha: 1,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
let call = FUNCTION.exec(text);
|
|
54
|
+
if (call === null)
|
|
55
|
+
return "not a hex color, a named color or a color function";
|
|
56
|
+
let [, name = "", body = ""] = call;
|
|
57
|
+
if (name === "color")
|
|
58
|
+
return readColorFunction(body);
|
|
59
|
+
let args = splitArguments(body, LEGACY_FUNCTIONS.has(name));
|
|
60
|
+
if (typeof args === "string")
|
|
61
|
+
return args;
|
|
62
|
+
return readFunction(name, args);
|
|
63
|
+
}
|
|
64
|
+
/** The channels of a `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa` color. */
|
|
65
|
+
function readHex(text) {
|
|
66
|
+
if (!HEX.test(text))
|
|
67
|
+
return "a hex color has 3, 4, 6 or 8 hex digits";
|
|
68
|
+
let digits = text.slice(1);
|
|
69
|
+
if (digits.length <= 4)
|
|
70
|
+
digits = digits.replaceAll(/[\da-f]/g, "$&$&");
|
|
71
|
+
let byte = (index) => Number.parseInt(digits.slice(index, index + 2), 16) / 255;
|
|
72
|
+
return { r: byte(0), g: byte(2), b: byte(4), alpha: digits.length === 8 ? byte(6) : 1 };
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Splits a function's arguments. Commas select the legacy syntax, which only `rgb()` and
|
|
76
|
+
* `hsl()` accept and which forbids `none`; otherwise channels are space-separated with an
|
|
77
|
+
* optional `/ alpha`.
|
|
78
|
+
*/
|
|
79
|
+
function splitArguments(body, legacyAllowed) {
|
|
80
|
+
if (body.includes(",")) {
|
|
81
|
+
if (!legacyAllowed)
|
|
82
|
+
return "comma-separated arguments are only valid in rgb() and hsl()";
|
|
83
|
+
let parts = body.split(",").map((part) => part.trim());
|
|
84
|
+
if (parts.length !== 3 && parts.length !== 4)
|
|
85
|
+
return "expected 3 channels and an optional alpha";
|
|
86
|
+
if (parts.includes("none"))
|
|
87
|
+
return "none is not valid in comma-separated syntax";
|
|
88
|
+
return buildArguments(parts, true);
|
|
89
|
+
}
|
|
90
|
+
let [main = "", alpha, ...rest] = body.split("/");
|
|
91
|
+
if (rest.length > 0)
|
|
92
|
+
return "more than one / in the arguments";
|
|
93
|
+
let parts = main.trim().split(/\s+/);
|
|
94
|
+
if (parts.length !== 3)
|
|
95
|
+
return "expected 3 channels";
|
|
96
|
+
if (alpha !== undefined)
|
|
97
|
+
parts.push(alpha.trim());
|
|
98
|
+
return buildArguments(parts, false);
|
|
99
|
+
}
|
|
100
|
+
/** Reads each argument text into a token, failing on the first one that is not a number. */
|
|
101
|
+
function buildArguments(parts, legacy) {
|
|
102
|
+
let tokens = [];
|
|
103
|
+
for (let part of parts) {
|
|
104
|
+
let token = readToken(part);
|
|
105
|
+
if (token === null)
|
|
106
|
+
return `${JSON.stringify(part)} is not a number, percentage or angle`;
|
|
107
|
+
tokens.push(token);
|
|
108
|
+
}
|
|
109
|
+
let [first, second, third, alpha = null] = tokens;
|
|
110
|
+
if (first === undefined || second === undefined || third === undefined) {
|
|
111
|
+
return "expected 3 channels";
|
|
112
|
+
}
|
|
113
|
+
return { channels: [first, second, third], alpha, legacy };
|
|
114
|
+
}
|
|
115
|
+
/** One argument as a token, reading `none` as zero, or `null` when it is no CSS number. */
|
|
116
|
+
function readToken(text) {
|
|
117
|
+
if (text === "none")
|
|
118
|
+
return { value: 0, unit: "" };
|
|
119
|
+
let match = TOKEN.exec(text);
|
|
120
|
+
if (match === null)
|
|
121
|
+
return null;
|
|
122
|
+
return { value: Number(match[1]), unit: match[2] ?? "" };
|
|
123
|
+
}
|
|
124
|
+
/** Dispatches a named color function to the reader for its space. */
|
|
125
|
+
function readFunction(name, args) {
|
|
126
|
+
let alpha = readAlpha(args.alpha);
|
|
127
|
+
if (alpha === null)
|
|
128
|
+
return "alpha must be a number or a percentage";
|
|
129
|
+
let vector = readVector(name, args);
|
|
130
|
+
if (typeof vector === "string")
|
|
131
|
+
return vector;
|
|
132
|
+
return { r: vector[0], g: vector[1], b: vector[2], alpha };
|
|
133
|
+
}
|
|
134
|
+
/** The sRGB channels a non-`color()` function names, or why its arguments are invalid. */
|
|
135
|
+
function readVector(name, args) {
|
|
136
|
+
let [first, second, third] = args.channels;
|
|
137
|
+
switch (name) {
|
|
138
|
+
case "rgb":
|
|
139
|
+
case "rgba":
|
|
140
|
+
return readRgb(args);
|
|
141
|
+
case "hsl":
|
|
142
|
+
case "hsla":
|
|
143
|
+
case "hwb": {
|
|
144
|
+
let hue = readHue(first);
|
|
145
|
+
let x = readPercentLike(second, args.legacy);
|
|
146
|
+
let y = readPercentLike(third, args.legacy);
|
|
147
|
+
if (hue === null || x === null || y === null)
|
|
148
|
+
return `invalid ${name}() channels`;
|
|
149
|
+
if (name === "hwb")
|
|
150
|
+
return hwbToSrgb(hue, clamp(x, 0, 1), clamp(y, 0, 1));
|
|
151
|
+
return hslToSrgb(hue, clamp(x, 0, 1), clamp(y, 0, 1));
|
|
152
|
+
}
|
|
153
|
+
case "lab":
|
|
154
|
+
case "lch":
|
|
155
|
+
return readCieLab(name, args.channels);
|
|
156
|
+
case "oklab":
|
|
157
|
+
case "oklch":
|
|
158
|
+
return readOklab(name, args.channels);
|
|
159
|
+
default:
|
|
160
|
+
return `${name}() is not a supported color function`;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/** The sRGB channels of `rgb()`: numbers out of 255 or percentages, clamped to 0–1. */
|
|
164
|
+
function readRgb(args) {
|
|
165
|
+
let channels = args.channels.map((token) => {
|
|
166
|
+
if (token.unit === "%")
|
|
167
|
+
return clamp(token.value / 100, 0, 1);
|
|
168
|
+
if (token.unit === "")
|
|
169
|
+
return clamp(token.value / 255, 0, 1);
|
|
170
|
+
return null;
|
|
171
|
+
});
|
|
172
|
+
let [r, g, b] = channels;
|
|
173
|
+
if (r == null || g == null || b == null)
|
|
174
|
+
return "rgb() channels are numbers or percentages";
|
|
175
|
+
return [r, g, b];
|
|
176
|
+
}
|
|
177
|
+
/** The sRGB channels of `lab()` or `lch()`, where 100% of L is 100, of a/b 125 and of C 150. */
|
|
178
|
+
function readCieLab(name, channels) {
|
|
179
|
+
let lightness = readScaled(channels[0], 100);
|
|
180
|
+
if (lightness === null)
|
|
181
|
+
return `invalid ${name}() lightness`;
|
|
182
|
+
lightness = clamp(lightness, 0, 100);
|
|
183
|
+
if (name === "lab") {
|
|
184
|
+
let a = readScaled(channels[1], 125);
|
|
185
|
+
let b = readScaled(channels[2], 125);
|
|
186
|
+
if (a === null || b === null)
|
|
187
|
+
return "invalid lab() channels";
|
|
188
|
+
return labToSrgb([lightness, a, b]);
|
|
189
|
+
}
|
|
190
|
+
let chroma = readScaled(channels[1], 150);
|
|
191
|
+
let hue = readHue(channels[2]);
|
|
192
|
+
if (chroma === null || hue === null)
|
|
193
|
+
return "invalid lch() channels";
|
|
194
|
+
return labToSrgb(lchToLab([lightness, Math.max(chroma, 0), hue]));
|
|
195
|
+
}
|
|
196
|
+
/** The sRGB channels of `oklab()` or `oklch()`, where 100% of L is 1 and of a, b or C 0.4. */
|
|
197
|
+
function readOklab(name, channels) {
|
|
198
|
+
let lightness = readScaled(channels[0], 1);
|
|
199
|
+
if (lightness === null)
|
|
200
|
+
return `invalid ${name}() lightness`;
|
|
201
|
+
lightness = clamp(lightness, 0, 1);
|
|
202
|
+
if (name === "oklab") {
|
|
203
|
+
let a = readScaled(channels[1], 0.4);
|
|
204
|
+
let b = readScaled(channels[2], 0.4);
|
|
205
|
+
if (a === null || b === null)
|
|
206
|
+
return "invalid oklab() channels";
|
|
207
|
+
return oklabToSrgb([lightness, a, b]);
|
|
208
|
+
}
|
|
209
|
+
let chroma = readScaled(channels[1], 0.4);
|
|
210
|
+
let hue = readHue(channels[2]);
|
|
211
|
+
if (chroma === null || hue === null)
|
|
212
|
+
return "invalid oklch() channels";
|
|
213
|
+
return oklabToSrgb(oklchToOklab([lightness, Math.max(chroma, 0), hue]));
|
|
214
|
+
}
|
|
215
|
+
/** The channels of `color(<space> c1 c2 c3 [/ alpha])`, where 100% of a channel is 1. */
|
|
216
|
+
function readColorFunction(body) {
|
|
217
|
+
let trimmed = body.trim();
|
|
218
|
+
let spaceEnd = trimmed.search(/\s/);
|
|
219
|
+
if (spaceEnd === -1)
|
|
220
|
+
return "color() needs a color space and 3 channels";
|
|
221
|
+
let spaceName = trimmed.slice(0, spaceEnd);
|
|
222
|
+
let space = RGB_SPACES[spaceName];
|
|
223
|
+
if (space === undefined)
|
|
224
|
+
return `${spaceName} is not a supported color() space`;
|
|
225
|
+
let args = splitArguments(trimmed.slice(spaceEnd), false);
|
|
226
|
+
if (typeof args === "string")
|
|
227
|
+
return args;
|
|
228
|
+
let [x, y, z] = args.channels.map((token) => readScaled(token, 1));
|
|
229
|
+
if (x == null || y == null || z == null)
|
|
230
|
+
return "color() channels are numbers or percentages";
|
|
231
|
+
let alpha = readAlpha(args.alpha);
|
|
232
|
+
if (alpha === null)
|
|
233
|
+
return "alpha must be a number or a percentage";
|
|
234
|
+
let [r, g, b] = rgbSpaceToSrgb(space, [x, y, z]);
|
|
235
|
+
return { r, g, b, alpha };
|
|
236
|
+
}
|
|
237
|
+
/** Alpha as 0–1: a number or a percentage, clamped, and 1 when the color omits it. */
|
|
238
|
+
function readAlpha(token) {
|
|
239
|
+
if (token === null)
|
|
240
|
+
return 1;
|
|
241
|
+
let value = readScaled(token, 1);
|
|
242
|
+
return value === null ? null : clamp(value, 0, 1);
|
|
243
|
+
}
|
|
244
|
+
/** A hue in degrees from a bare number or any CSS angle unit; a percentage is no hue. */
|
|
245
|
+
function readHue(token) {
|
|
246
|
+
let degreesPerUnit = DEGREES_PER_UNIT[token.unit];
|
|
247
|
+
return degreesPerUnit === undefined ? null : token.value * degreesPerUnit;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* A number or percentage where 100% equals `scale` and a bare number is taken as is; any
|
|
251
|
+
* other unit is invalid.
|
|
252
|
+
*/
|
|
253
|
+
function readScaled(token, scale) {
|
|
254
|
+
if (token.unit === "%")
|
|
255
|
+
return (token.value / 100) * scale;
|
|
256
|
+
if (token.unit === "")
|
|
257
|
+
return token.value;
|
|
258
|
+
return null;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* An HSL or HWB percentage as 0–1. The modern syntax also accepts a bare number on the same
|
|
262
|
+
* 0–100 scale; the legacy comma syntax requires the `%`.
|
|
263
|
+
*/
|
|
264
|
+
function readPercentLike(token, legacy) {
|
|
265
|
+
if (token.unit === "%" || (token.unit === "" && !legacy))
|
|
266
|
+
return token.value / 100;
|
|
267
|
+
return null;
|
|
268
|
+
}
|
|
269
|
+
/** `value` limited to `[min, max]`. */
|
|
270
|
+
function clamp(value, min, max) {
|
|
271
|
+
return Math.min(Math.max(value, min), max);
|
|
272
|
+
}
|
package/dist/spaces.d.ts
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The color-space math every `Color` operation stands on: sRGB transfer, Oklab and OKLCH,
|
|
3
|
+
* HSL and HWB, CIE Lab and LCH, and the matrices that bring each `color()` space to linear
|
|
4
|
+
* sRGB. Every function is pure and keeps values outside 0–1, so wide-gamut input survives.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/** Three channels of one color space, in that space's channel order. */
|
|
10
|
+
export type Vector = [number, number, number];
|
|
11
|
+
/** A 3×3 matrix stored row by row. */
|
|
12
|
+
type Matrix = [Vector, Vector, Vector];
|
|
13
|
+
/**
|
|
14
|
+
* The chroma below which an OKLCH color counts as achromatic, so its hue is powerless:
|
|
15
|
+
* floating-point conversions leave grays a few millionths of chroma, never exactly zero.
|
|
16
|
+
*/
|
|
17
|
+
export declare const ACHROMATIC_CHROMA = 0.000004;
|
|
18
|
+
/** How one `color()` space reaches linear sRGB: its transfer function, then a matrix. */
|
|
19
|
+
export interface RgbSpace {
|
|
20
|
+
toLinear(channel: number): number;
|
|
21
|
+
matrix: Matrix | null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Every predefined space `color()` accepts, keyed by its CSS name. `xyz` is CSS's alias of
|
|
25
|
+
* `xyz-d65`, and a `null` matrix means the space is linear sRGB already.
|
|
26
|
+
*/
|
|
27
|
+
export declare const RGB_SPACES: Readonly<Record<string, RgbSpace>>;
|
|
28
|
+
/**
|
|
29
|
+
* Decodes one gamma-encoded sRGB channel to linear light. The curve is mirrored around
|
|
30
|
+
* zero, as CSS extends it, so a channel below 0 or above 1 keeps its meaning.
|
|
31
|
+
*/
|
|
32
|
+
export declare function srgbToLinear(channel: number): number;
|
|
33
|
+
/** Encodes one linear-light channel as gamma-encoded sRGB, mirrored around zero. */
|
|
34
|
+
export declare function linearToSrgb(channel: number): number;
|
|
35
|
+
/** Converts a `color()` space's channels to gamma-encoded sRGB. */
|
|
36
|
+
export declare function rgbSpaceToSrgb(space: RgbSpace, channels: Vector): Vector;
|
|
37
|
+
/** Converts gamma-encoded sRGB to Oklab `[L, a, b]`. */
|
|
38
|
+
export declare function srgbToOklab(rgb: Vector): Vector;
|
|
39
|
+
/** Converts Oklab `[L, a, b]` to gamma-encoded sRGB, unclamped. */
|
|
40
|
+
export declare function oklabToSrgb(lab: Vector): Vector;
|
|
41
|
+
/** Wraps any angle into `[0, 360)`. */
|
|
42
|
+
export declare function normalizeHue(degrees: number): number;
|
|
43
|
+
/** Converts Oklab to OKLCH `[L, C, h]`, with a hue of 0 for an achromatic color. */
|
|
44
|
+
export declare function oklabToOklch(lab: Vector): Vector;
|
|
45
|
+
/** Converts OKLCH `[L, C, h]` to Oklab. */
|
|
46
|
+
export declare function oklchToOklab(lch: Vector): Vector;
|
|
47
|
+
/**
|
|
48
|
+
* Converts HSL to gamma-encoded sRGB. Hue is in degrees; saturation and lightness are 0–1.
|
|
49
|
+
*/
|
|
50
|
+
export declare function hslToSrgb(hue: number, saturation: number, lightness: number): Vector;
|
|
51
|
+
/**
|
|
52
|
+
* Converts gamma-encoded sRGB to HSL `[h, s, l]`, with hue in degrees and a hue of 0 for
|
|
53
|
+
* a gray. The input is expected inside the sRGB gamut, which HSL cannot leave.
|
|
54
|
+
*/
|
|
55
|
+
export declare function srgbToHsl(rgb: Vector): Vector;
|
|
56
|
+
/**
|
|
57
|
+
* Converts HWB to gamma-encoded sRGB. Whiteness and blackness are 0–1; when they add up to
|
|
58
|
+
* 1 or more the color is the gray their ratio names.
|
|
59
|
+
*/
|
|
60
|
+
export declare function hwbToSrgb(hue: number, whiteness: number, blackness: number): Vector;
|
|
61
|
+
/** Converts CIE Lab (D50, L in 0–100) to gamma-encoded sRGB, unclamped. */
|
|
62
|
+
export declare function labToSrgb(lab: Vector): Vector;
|
|
63
|
+
/** Converts CIE LCH `[L, C, h]` to CIE Lab. */
|
|
64
|
+
export declare function lchToLab(lch: Vector): Vector;
|
|
65
|
+
export {};
|