@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/dist/spaces.js ADDED
@@ -0,0 +1,254 @@
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
+ /**
10
+ * The chroma below which an OKLCH color counts as achromatic, so its hue is powerless:
11
+ * floating-point conversions leave grays a few millionths of chroma, never exactly zero.
12
+ */
13
+ export const ACHROMATIC_CHROMA = 4e-6;
14
+ /** Linear sRGB to the LMS cone response Oklab is built on. */
15
+ const LINEAR_SRGB_TO_LMS = [
16
+ [0.4122214708, 0.5363325363, 0.0514459929],
17
+ [0.2119034982, 0.6806995451, 0.1073969566],
18
+ [0.0883024619, 0.2817188376, 0.6299787005],
19
+ ];
20
+ /** Cube-rooted LMS to Oklab. */
21
+ const LMS_TO_OKLAB = [
22
+ [0.2104542553, 0.793617785, -0.0040720468],
23
+ [1.9779984951, -2.428592205, 0.4505937099],
24
+ [0.0259040371, 0.7827717662, -0.808675766],
25
+ ];
26
+ /** Oklab to cube-rooted LMS, the exact inverse of `LMS_TO_OKLAB` so round trips stay tight. */
27
+ const OKLAB_TO_LMS = [
28
+ [0.9999999984505199, 0.39633779217376786, 0.2158037580607588],
29
+ [1.0000000088817607, -0.10556134232365634, -0.0638541747717059],
30
+ [1.000000054672411, -0.08948418209496577, -1.291485537864092],
31
+ ];
32
+ /** LMS cone response to linear sRGB, the exact inverse of `LINEAR_SRGB_TO_LMS`. */
33
+ const LMS_TO_LINEAR_SRGB = [
34
+ [4.076741661347994, -3.3077115904081933, 0.23096992872942784],
35
+ [-1.268438004092176, 2.6097574006633715, -0.34131939631021946],
36
+ [-0.004196086541837046, -0.7034186144594494, 1.7076147009309446],
37
+ ];
38
+ /** CIE XYZ relative to D65 to linear sRGB, derived from the sRGB primaries. */
39
+ const XYZ_D65_TO_LINEAR_SRGB = [
40
+ [3.2409699419045226, -1.537383177570094, -0.49861076029300344],
41
+ [-0.9692436362808796, 1.8759675015077204, 0.0415550574071756],
42
+ [0.05563007969699363, -0.2039769588889765, 1.0569715142428784],
43
+ ];
44
+ /** CIE XYZ relative to D50 to linear sRGB, through the Bradford adaptation CSS uses. */
45
+ const XYZ_D50_TO_LINEAR_SRGB = [
46
+ [3.134135852900119, -1.6173859980180436, -0.49066221791109765],
47
+ [-0.9787954765557777, 1.9162543773959888, 0.03344287339036693],
48
+ [0.07195539255794739, -0.228976759815182, 1.4053860351131182],
49
+ ];
50
+ /** Linear Display P3 to linear sRGB. */
51
+ const LINEAR_P3_TO_LINEAR_SRGB = [
52
+ [1.2249401762805596, -0.22494017628055984, 0],
53
+ [-0.042056954709688066, 1.042056954709688, 0],
54
+ [-0.019637554590334436, -0.07863604555063179, 1.0982736001409663],
55
+ ];
56
+ /** Linear Adobe RGB (1998) to linear sRGB. */
57
+ const LINEAR_A98_TO_LINEAR_SRGB = [
58
+ [1.3983557439607786, -0.39835574396077855, 0],
59
+ [0, 1, 0],
60
+ [0, -0.04292898929447317, 1.0429289892944729],
61
+ ];
62
+ /** Linear Rec. 2020 to linear sRGB. */
63
+ const LINEAR_REC2020_TO_LINEAR_SRGB = [
64
+ [1.6604910021084347, -0.5876411387885495, -0.07284986331988486],
65
+ [-0.12455047452159052, 1.1328998971259598, -0.008349422604369487],
66
+ [-0.01815076335490522, -0.10057889800800736, 1.1187296613629125],
67
+ ];
68
+ /** Linear ProPhoto RGB, a D50 space, to linear sRGB. */
69
+ const LINEAR_PROPHOTO_TO_LINEAR_SRGB = [
70
+ [2.0343808495169964, -0.7276357899341352, -0.3067450595828619],
71
+ [-0.22882573163305028, 1.2317425411901053, -0.002916809557054506],
72
+ [-0.008558828783917382, -0.1532667021380372, 1.1618255309219545],
73
+ ];
74
+ /** The D50 white point CIE Lab is relative to, as CSS defines it. */
75
+ const D50_WHITE = [0.3457 / 0.3585, 1, (1 - 0.3457 - 0.3585) / 0.3585];
76
+ /** CIE Lab's κ, the slope of the linear segment near black. */
77
+ const LAB_KAPPA = 24_389 / 27;
78
+ /** CIE Lab's ε, where the cube-root segment begins. */
79
+ const LAB_EPSILON = 216 / 24_389;
80
+ /** Rec. 2020's transfer constant α. */
81
+ const REC2020_ALPHA = 1.09929682680944;
82
+ /** Rec. 2020's transfer constant β. */
83
+ const REC2020_BETA = 0.018053968510807;
84
+ /** The value itself, for spaces whose channels are already linear. */
85
+ function identity(channel) {
86
+ return channel;
87
+ }
88
+ /** Adobe RGB (1998)'s pure gamma of 563/256, mirrored for negative values. */
89
+ function a98ToLinear(channel) {
90
+ return Math.sign(channel) * Math.abs(channel) ** (563 / 256);
91
+ }
92
+ /** Rec. 2020's piecewise transfer, mirrored for negative values. */
93
+ function rec2020ToLinear(channel) {
94
+ let magnitude = Math.abs(channel);
95
+ if (magnitude < REC2020_BETA * 4.5)
96
+ return channel / 4.5;
97
+ return Math.sign(channel) * ((magnitude + REC2020_ALPHA - 1) / REC2020_ALPHA) ** (1 / 0.45);
98
+ }
99
+ /** ProPhoto RGB's gamma of 1.8 with a linear toe, mirrored for negative values. */
100
+ function prophotoToLinear(channel) {
101
+ let magnitude = Math.abs(channel);
102
+ if (magnitude <= 16 / 512)
103
+ return channel / 16;
104
+ return Math.sign(channel) * magnitude ** 1.8;
105
+ }
106
+ /**
107
+ * Every predefined space `color()` accepts, keyed by its CSS name. `xyz` is CSS's alias of
108
+ * `xyz-d65`, and a `null` matrix means the space is linear sRGB already.
109
+ */
110
+ export const RGB_SPACES = {
111
+ srgb: { toLinear: srgbToLinear, matrix: null },
112
+ "srgb-linear": { toLinear: identity, matrix: null },
113
+ "display-p3": { toLinear: srgbToLinear, matrix: LINEAR_P3_TO_LINEAR_SRGB },
114
+ "a98-rgb": { toLinear: a98ToLinear, matrix: LINEAR_A98_TO_LINEAR_SRGB },
115
+ rec2020: { toLinear: rec2020ToLinear, matrix: LINEAR_REC2020_TO_LINEAR_SRGB },
116
+ "prophoto-rgb": { toLinear: prophotoToLinear, matrix: LINEAR_PROPHOTO_TO_LINEAR_SRGB },
117
+ xyz: { toLinear: identity, matrix: XYZ_D65_TO_LINEAR_SRGB },
118
+ "xyz-d65": { toLinear: identity, matrix: XYZ_D65_TO_LINEAR_SRGB },
119
+ "xyz-d50": { toLinear: identity, matrix: XYZ_D50_TO_LINEAR_SRGB },
120
+ };
121
+ /** `matrix × vector`. */
122
+ function multiply(matrix, vector) {
123
+ return [
124
+ matrix[0][0] * vector[0] + matrix[0][1] * vector[1] + matrix[0][2] * vector[2],
125
+ matrix[1][0] * vector[0] + matrix[1][1] * vector[1] + matrix[1][2] * vector[2],
126
+ matrix[2][0] * vector[0] + matrix[2][1] * vector[1] + matrix[2][2] * vector[2],
127
+ ];
128
+ }
129
+ /**
130
+ * Decodes one gamma-encoded sRGB channel to linear light. The curve is mirrored around
131
+ * zero, as CSS extends it, so a channel below 0 or above 1 keeps its meaning.
132
+ */
133
+ export function srgbToLinear(channel) {
134
+ let magnitude = Math.abs(channel);
135
+ if (magnitude <= 0.04045)
136
+ return channel / 12.92;
137
+ return Math.sign(channel) * ((magnitude + 0.055) / 1.055) ** 2.4;
138
+ }
139
+ /** Encodes one linear-light channel as gamma-encoded sRGB, mirrored around zero. */
140
+ export function linearToSrgb(channel) {
141
+ let magnitude = Math.abs(channel);
142
+ if (magnitude <= 0.0031308)
143
+ return channel * 12.92;
144
+ return Math.sign(channel) * (1.055 * magnitude ** (1 / 2.4) - 0.055);
145
+ }
146
+ /** Converts a `color()` space's channels to gamma-encoded sRGB. */
147
+ export function rgbSpaceToSrgb(space, channels) {
148
+ let linear = [
149
+ space.toLinear(channels[0]),
150
+ space.toLinear(channels[1]),
151
+ space.toLinear(channels[2]),
152
+ ];
153
+ let srgb = space.matrix === null ? linear : multiply(space.matrix, linear);
154
+ return [linearToSrgb(srgb[0]), linearToSrgb(srgb[1]), linearToSrgb(srgb[2])];
155
+ }
156
+ /** Converts gamma-encoded sRGB to Oklab `[L, a, b]`. */
157
+ export function srgbToOklab(rgb) {
158
+ let lms = multiply(LINEAR_SRGB_TO_LMS, [
159
+ srgbToLinear(rgb[0]),
160
+ srgbToLinear(rgb[1]),
161
+ srgbToLinear(rgb[2]),
162
+ ]);
163
+ return multiply(LMS_TO_OKLAB, [Math.cbrt(lms[0]), Math.cbrt(lms[1]), Math.cbrt(lms[2])]);
164
+ }
165
+ /** Converts Oklab `[L, a, b]` to gamma-encoded sRGB, unclamped. */
166
+ export function oklabToSrgb(lab) {
167
+ let root = multiply(OKLAB_TO_LMS, lab);
168
+ let linear = multiply(LMS_TO_LINEAR_SRGB, [root[0] ** 3, root[1] ** 3, root[2] ** 3]);
169
+ return [linearToSrgb(linear[0]), linearToSrgb(linear[1]), linearToSrgb(linear[2])];
170
+ }
171
+ /** Wraps any angle into `[0, 360)`. */
172
+ export function normalizeHue(degrees) {
173
+ let hue = degrees % 360;
174
+ return hue < 0 ? hue + 360 : hue;
175
+ }
176
+ /** Converts Oklab to OKLCH `[L, C, h]`, with a hue of 0 for an achromatic color. */
177
+ export function oklabToOklch(lab) {
178
+ let chroma = Math.hypot(lab[1], lab[2]);
179
+ if (chroma < ACHROMATIC_CHROMA)
180
+ return [lab[0], chroma, 0];
181
+ return [lab[0], chroma, normalizeHue((Math.atan2(lab[2], lab[1]) * 180) / Math.PI)];
182
+ }
183
+ /** Converts OKLCH `[L, C, h]` to Oklab. */
184
+ export function oklchToOklab(lch) {
185
+ let radians = (lch[2] * Math.PI) / 180;
186
+ return [lch[0], lch[1] * Math.cos(radians), lch[1] * Math.sin(radians)];
187
+ }
188
+ /**
189
+ * Converts HSL to gamma-encoded sRGB. Hue is in degrees; saturation and lightness are 0–1.
190
+ */
191
+ export function hslToSrgb(hue, saturation, lightness) {
192
+ let amount = saturation * Math.min(lightness, 1 - lightness);
193
+ let channel = (offset) => {
194
+ let k = (offset + normalizeHue(hue) / 30) % 12;
195
+ return lightness - amount * Math.max(-1, Math.min(k - 3, 9 - k, 1));
196
+ };
197
+ return [channel(0), channel(8), channel(4)];
198
+ }
199
+ /**
200
+ * Converts gamma-encoded sRGB to HSL `[h, s, l]`, with hue in degrees and a hue of 0 for
201
+ * a gray. The input is expected inside the sRGB gamut, which HSL cannot leave.
202
+ */
203
+ export function srgbToHsl(rgb) {
204
+ let max = Math.max(...rgb);
205
+ let min = Math.min(...rgb);
206
+ let lightness = (max + min) / 2;
207
+ let delta = max - min;
208
+ if (delta < 1e-12)
209
+ return [0, 0, lightness];
210
+ let saturation = lightness === 0 || lightness === 1 ? 0 : (max - lightness) / Math.min(lightness, 1 - lightness);
211
+ let hue = huePosition(rgb, max, delta) * 60;
212
+ return [normalizeHue(hue), saturation, lightness];
213
+ }
214
+ /** Where the hue of `rgb` falls on the six-sector color wheel, as a sector position. */
215
+ function huePosition(rgb, max, delta) {
216
+ let [r, g, b] = rgb;
217
+ if (max === r)
218
+ return (g - b) / delta + (g < b ? 6 : 0);
219
+ if (max === g)
220
+ return (b - r) / delta + 2;
221
+ return (r - g) / delta + 4;
222
+ }
223
+ /**
224
+ * Converts HWB to gamma-encoded sRGB. Whiteness and blackness are 0–1; when they add up to
225
+ * 1 or more the color is the gray their ratio names.
226
+ */
227
+ export function hwbToSrgb(hue, whiteness, blackness) {
228
+ if (whiteness + blackness >= 1) {
229
+ let gray = whiteness / (whiteness + blackness);
230
+ return [gray, gray, gray];
231
+ }
232
+ let pure = hslToSrgb(hue, 1, 0.5);
233
+ let scale = 1 - whiteness - blackness;
234
+ return [pure[0] * scale + whiteness, pure[1] * scale + whiteness, pure[2] * scale + whiteness];
235
+ }
236
+ /** Converts CIE Lab (D50, L in 0–100) to gamma-encoded sRGB, unclamped. */
237
+ export function labToSrgb(lab) {
238
+ let fy = (lab[0] + 16) / 116;
239
+ let fx = lab[1] / 500 + fy;
240
+ let fz = fy - lab[2] / 200;
241
+ let x = fx ** 3 > LAB_EPSILON ? fx ** 3 : (116 * fx - 16) / LAB_KAPPA;
242
+ let y = lab[0] > LAB_KAPPA * LAB_EPSILON ? fy ** 3 : lab[0] / LAB_KAPPA;
243
+ let z = fz ** 3 > LAB_EPSILON ? fz ** 3 : (116 * fz - 16) / LAB_KAPPA;
244
+ return rgbSpaceToSrgb({ toLinear: identity, matrix: XYZ_D50_TO_LINEAR_SRGB }, [
245
+ x * D50_WHITE[0],
246
+ y * D50_WHITE[1],
247
+ z * D50_WHITE[2],
248
+ ]);
249
+ }
250
+ /** Converts CIE LCH `[L, C, h]` to CIE Lab. */
251
+ export function lchToLab(lch) {
252
+ let radians = (lch[2] * Math.PI) / 180;
253
+ return [lch[0], lch[1] * Math.cos(radians), lch[1] * Math.sin(radians)];
254
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@sdxc/color",
3
+ "version": "0.0.0-pre.1",
4
+ "description": "Immutable CSS colors: parse every CSS color syntax, convert through OKLCH, gamut-map, mix, build scales and measure WCAG contrast",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": {
8
+ ".": "./dist/index.js"
9
+ },
10
+ "dependencies": {
11
+ "@sdxc/result": "2026.10.13"
12
+ },
13
+ "gitHead": "b3bc5350bdbcc7200a18558a33bf70e1de1858b6",
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/sergiodxa/monorepo.git",
20
+ "directory": "packages/color"
21
+ },
22
+ "homepage": "https://sdxc.dev/api/color",
23
+ "author": {
24
+ "name": "Sergio Xalambrí",
25
+ "url": "https://sergiodxa.com"
26
+ },
27
+ "funding": {
28
+ "type": "github",
29
+ "url": "https://github.com/sponsors/sergiodxa"
30
+ }
31
+ }