@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/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sergio Xalambrí
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# @sdxc/color
|
|
2
|
+
|
|
3
|
+
Immutable CSS colors: parse every CSS color syntax, convert through OKLCH, gamut-map, mix, build scales and measure WCAG contrast.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm add @sdxc/color @sdxc/result
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
`Color.parse` and `ensureContrast` answer a [`@sdxc/result`](https://www.npmjs.com/package/@sdxc/result) `Result`. The package has no other dependency and runs in Workers, browsers and Node.
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
### Parse and print
|
|
16
|
+
|
|
17
|
+
`Color.parse` reads any CSS color, including what `getComputedStyle` returns, and never throws. `toString()` prints `rgb()` inside sRGB and `oklch()` outside it, so `${color}` is always valid CSS.
|
|
18
|
+
|
|
19
|
+
```typescript
|
|
20
|
+
import { Color } from "@sdxc/color";
|
|
21
|
+
import { isSuccess } from "@sdxc/result";
|
|
22
|
+
|
|
23
|
+
let result = Color.parse("oklch(0.62 0.19 260)");
|
|
24
|
+
if (isSuccess(result)) {
|
|
25
|
+
let brand = result.data;
|
|
26
|
+
brand.toString(); // "rgb(57.75 128.67 246.1)"
|
|
27
|
+
brand.toString("hex"); // "#3a81f6"
|
|
28
|
+
}
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
### Adjust in OKLCH
|
|
32
|
+
|
|
33
|
+
Every operation answers a new `Color`. `darken(0.1)` is 10% darker in OKLCH lightness, keeping hue and chroma.
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
let hover = brand.darken(0.1);
|
|
37
|
+
let muted = brand.desaturate(0.5).withAlpha(0.6);
|
|
38
|
+
let accent = brand.with({ hue: 30 });
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Check contrast
|
|
42
|
+
|
|
43
|
+
`contrast` paints a translucent foreground over the background (and a translucent background over white) before measuring with WCAG 2.1.
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
let text = Color.rgb(0.47, 0.47, 0.47);
|
|
47
|
+
let page = Color.rgb(1, 1, 1);
|
|
48
|
+
|
|
49
|
+
text.contrast(page).ratio; // 4.42
|
|
50
|
+
text.contrast(page).aa.text; // false
|
|
51
|
+
|
|
52
|
+
let fixed = text.ensureContrast(page, 4.5); // Success: rgb(118.66 118.66 118.66)
|
|
53
|
+
page.contrastingText(); // black
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Mix and build scales
|
|
57
|
+
|
|
58
|
+
Interpolation follows CSS `color-mix()`: Oklab by default, premultiplied alpha, and a hue arc in OKLCH.
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
let red = Color.rgb(1, 0, 0);
|
|
62
|
+
let blue = Color.rgb(0, 0, 1);
|
|
63
|
+
|
|
64
|
+
red.mix(blue); // rgb(140.36 83.03 162.31), as color-mix(in oklab, red, blue)
|
|
65
|
+
red.mix(blue, 0.25, { space: "oklch", hue: "longer" });
|
|
66
|
+
|
|
67
|
+
let heat = Color.scale([blue, Color.rgb(1, 1, 0), red]);
|
|
68
|
+
heat(0.5); // yellow
|
|
69
|
+
Color.steps([red, blue], 5); // 5 colors, both ends included
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## API
|
|
73
|
+
|
|
74
|
+
### `Color`
|
|
75
|
+
|
|
76
|
+
A frozen value storing sRGB as unclamped floats (extended sRGB) plus alpha, so a wide-gamut color from `oklch()` or `color(display-p3 …)` survives losslessly. An authored hue on a gray and `none` components do not survive: a gray reads back with hue 0 and `none` reads as 0.
|
|
77
|
+
|
|
78
|
+
#### Creating
|
|
79
|
+
|
|
80
|
+
- `Color.parse(input)`: hex, named colors, `transparent`, `rgb()`, `hsl()`, `hwb()`, `lab()`, `lch()`, `oklab()`, `oklch()` and `color()` in `srgb`, `srgb-linear`, `display-p3`, `a98-rgb`, `rec2020`, `prophoto-rgb` and `xyz`/`xyz-d50`/`xyz-d65`; a `ColorParseError` otherwise (including `currentcolor` and relative colors)
|
|
81
|
+
- `Color.rgb(r, g, b, alpha?)`: sRGB channels 0–1, unclamped
|
|
82
|
+
- `Color.hsl(h, s, l, alpha?)` and `Color.hwb(h, w, b, alpha?)`: hue in degrees, the rest 0–1
|
|
83
|
+
- `Color.oklab(l, a, b, alpha?)` and `Color.oklch(l, c, h, alpha?)`: lightness 0–1, hue in degrees
|
|
84
|
+
|
|
85
|
+
Alpha defaults to 1 and is clamped to 0–1; a non-finite number reads as 0.
|
|
86
|
+
|
|
87
|
+
#### Reading
|
|
88
|
+
|
|
89
|
+
- `toString(format?)`: `"hex"`, `"rgb"` and `"hsl"` gamut-map first; `"srgb"` (`color(srgb …)`), `"oklch"` and `"oklab"` are lossless
|
|
90
|
+
- `toRgb()`, `toHsl()`, `toOklab()`, `toOklch()`: plain objects with `alpha`; `toHsl()` is gamut-mapped
|
|
91
|
+
- `alpha`, `equals(other, epsilon = 1e-6)`, `inGamut()`
|
|
92
|
+
- `toGamut()`: CSS Color 4 gamut mapping, lowering OKLCH chroma until the color fits sRGB
|
|
93
|
+
|
|
94
|
+
#### Operations
|
|
95
|
+
|
|
96
|
+
- `over(background)`: source-over compositing in sRGB, as browsers paint
|
|
97
|
+
- `mix(other, amount = 0.5, { space, hue })`: `amount` is the share of `other`; `space` is `"oklab"` (default), `"oklch"` or `"srgb"`; `hue` is `"shorter"` (default), `"longer"`, `"increasing"` or `"decreasing"`
|
|
98
|
+
- `withAlpha(alpha)`, `fade(factor)`
|
|
99
|
+
- `lighten(n)`, `darken(n)`: OKLCH lightness × (1 ± n), clamped to 0–1
|
|
100
|
+
- `saturate(n)`, `desaturate(n)`: OKLCH chroma × (1 ± n)
|
|
101
|
+
- `rotate(degrees)`, `complement()`, `grayscale()`, `invert()`
|
|
102
|
+
- `with({ lightness, chroma, hue, alpha })`: exact OKLCH values
|
|
103
|
+
|
|
104
|
+
Operations do not gamut-map, so a result may leave sRGB; call `toGamut()` when it must fit.
|
|
105
|
+
|
|
106
|
+
#### Contrast
|
|
107
|
+
|
|
108
|
+
- `contrast(background, algorithm = "WCAG21")` and `Color.contrast(foreground, background)`: a `WcagContrast` with `ratio`, `aa.text` (4.5), `aa.largeText` (3), `aa.nonText` (3), `aaa.text` (7) and `aaa.largeText` (4.5). Both colors are gamut-mapped before measuring
|
|
109
|
+
- `ensureContrast(background, ratio)`: the closest color reaching `ratio` by changing only OKLCH lightness, or a `ContrastUnreachableError` whose `best` is the most contrasting lightness extreme
|
|
110
|
+
- `contrastingText()`: black or white, whichever contrasts more
|
|
111
|
+
|
|
112
|
+
#### Scales
|
|
113
|
+
|
|
114
|
+
- `Color.scale(colors, options?)`: a function from `t` in 0–1 to a color, evenly spaced across `colors`
|
|
115
|
+
- `Color.steps(colors, count, options?)`: `count` colors along that scale, both ends included
|
|
116
|
+
|
|
117
|
+
### Errors
|
|
118
|
+
|
|
119
|
+
- `ColorParseError`: `input` holds the rejected text
|
|
120
|
+
- `ContrastUnreachableError`: `target`, the best `ratio` reached and the `best` color
|
|
121
|
+
|
|
122
|
+
## Pattern: Theming A Page From A Chosen Color
|
|
123
|
+
|
|
124
|
+
A Worker serves a stylesheet built from the accent a visitor picked. An unreadable value falls back to a default, links are darkened until they pass AA on white, and text on the accent is whichever of black or white reads better.
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { Color } from "@sdxc/color";
|
|
128
|
+
import { unwrap } from "@sdxc/result";
|
|
129
|
+
|
|
130
|
+
function themeStylesheet(accentText: string | null): string {
|
|
131
|
+
let accent = unwrap(Color.parse(accentText ?? ""), () => Color.oklch(0.55, 0.15, 260)).toGamut();
|
|
132
|
+
let page = Color.rgb(1, 1, 1);
|
|
133
|
+
let link = accent.ensureContrast(page, 4.5);
|
|
134
|
+
|
|
135
|
+
return `:root {
|
|
136
|
+
--accent: ${accent.toString()};
|
|
137
|
+
--accent-hover: ${accent.darken(0.1).toGamut().toString()};
|
|
138
|
+
--on-accent: ${accent.contrastingText().toString()};
|
|
139
|
+
--link: ${(link.status === "success" ? link.data : link.error.best).toString()};
|
|
140
|
+
}`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export default {
|
|
144
|
+
async fetch(request: Request): Promise<Response> {
|
|
145
|
+
let accent = new URL(request.url).searchParams.get("accent");
|
|
146
|
+
return new Response(themeStylesheet(accent), { headers: { "Content-Type": "text/css" } });
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Pattern: Validating A Submitted Color
|
|
152
|
+
|
|
153
|
+
A settings form accepts any CSS color, rejects text that is not one, requires the color to stand out on white as a UI part (WCAG 1.4.11), and stores it as hex.
|
|
154
|
+
|
|
155
|
+
```typescript
|
|
156
|
+
import { Color } from "@sdxc/color";
|
|
157
|
+
import { isFailure } from "@sdxc/result";
|
|
158
|
+
|
|
159
|
+
async function saveBrandColor(request: Request): Promise<Response> {
|
|
160
|
+
let form = await request.formData();
|
|
161
|
+
let parsed = Color.parse(String(form.get("color") ?? ""));
|
|
162
|
+
if (isFailure(parsed)) return Response.json({ error: parsed.error.message }, { status: 422 });
|
|
163
|
+
|
|
164
|
+
let color = parsed.data.toGamut();
|
|
165
|
+
if (!color.contrast(Color.rgb(1, 1, 1)).aa.nonText) {
|
|
166
|
+
return Response.json({ error: "Pick a color that stands out on white" }, { status: 422 });
|
|
167
|
+
}
|
|
168
|
+
return Response.json({ color: color.toString("hex") });
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Pattern: Finding Low-Contrast Text On A Rendered Page
|
|
173
|
+
|
|
174
|
+
In the browser, `getComputedStyle` answers every color in a syntax `Color.parse` reads, `color-mix()` results included. A transparent background shows its ancestors through, so the backdrop is every translucent layer up to the first opaque one, painted over the white canvas.
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import { Color } from "@sdxc/color";
|
|
178
|
+
import { isSuccess } from "@sdxc/result";
|
|
179
|
+
|
|
180
|
+
function backdropOf(element: Element): Color {
|
|
181
|
+
let layers: Color[] = [];
|
|
182
|
+
for (let node: Element | null = element; node !== null; node = node.parentElement) {
|
|
183
|
+
let parsed = Color.parse(getComputedStyle(node).backgroundColor);
|
|
184
|
+
if (!isSuccess(parsed) || parsed.data.alpha === 0) continue;
|
|
185
|
+
layers.push(parsed.data);
|
|
186
|
+
if (parsed.data.alpha === 1) break;
|
|
187
|
+
}
|
|
188
|
+
return layers.reduceRight((below, layer) => layer.over(below), Color.rgb(1, 1, 1));
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function lowContrastText(root: ParentNode): Element[] {
|
|
192
|
+
return [...root.querySelectorAll("p, a, li, label, button")].filter((element) => {
|
|
193
|
+
let text = Color.parse(getComputedStyle(element).color);
|
|
194
|
+
return isSuccess(text) && !text.data.contrast(backdropOf(element)).aa.text;
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Versioning
|
|
200
|
+
|
|
201
|
+
Releases are dated rather than semantic. A version is the UTC date it was published, written `YYYY.M.D`, so `2026.9.4` is the release from 4 September 2026. At most one release goes out per day.
|
|
202
|
+
|
|
203
|
+
Those numbers say when, not what: a later date means a later release and carries no compatibility promise. Any release may change or remove an export.
|
|
204
|
+
|
|
205
|
+
Depend on one exact date, and move it when you are ready to take the change:
|
|
206
|
+
|
|
207
|
+
```json
|
|
208
|
+
{
|
|
209
|
+
"dependencies": {
|
|
210
|
+
"@sdxc/color": "2026.9.4"
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
A caret or tilde range reads the date as major, minor and patch, so it accepts every later release in the same year. An exact version keeps the upgrade yours to schedule.
|
|
216
|
+
|
|
217
|
+
## License
|
|
218
|
+
|
|
219
|
+
MIT
|
|
220
|
+
|
|
221
|
+
## Author
|
|
222
|
+
|
|
223
|
+
[Sergio Xalambrí](https://sergiodxa.com)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The failure `Color.parse` reports for text that is not a CSS color it can read. Colors
|
|
3
|
+
* arrive from stylesheets, settings and user input, so the rejected text stays on the
|
|
4
|
+
* error for a log line or a form message.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/** Text `Color.parse` could not read as a CSS color, delivered inside a `Failure`. */
|
|
10
|
+
export declare class ColorParseError extends Error {
|
|
11
|
+
/** The rejected text, verbatim. */
|
|
12
|
+
readonly input: string;
|
|
13
|
+
/**
|
|
14
|
+
* @param input - The text that failed to parse.
|
|
15
|
+
* @param reason - Why it failed, appended to the message.
|
|
16
|
+
*/
|
|
17
|
+
constructor(input: string, reason: string);
|
|
18
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The failure `Color.parse` reports for text that is not a CSS color it can read. Colors
|
|
3
|
+
* arrive from stylesheets, settings and user input, so the rejected text stays on the
|
|
4
|
+
* error for a log line or a form message.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
/** Text `Color.parse` could not read as a CSS color, delivered inside a `Failure`. */
|
|
10
|
+
export class ColorParseError extends Error {
|
|
11
|
+
/** The rejected text, verbatim. */
|
|
12
|
+
input;
|
|
13
|
+
/**
|
|
14
|
+
* @param input - The text that failed to parse.
|
|
15
|
+
* @param reason - Why it failed, appended to the message.
|
|
16
|
+
*/
|
|
17
|
+
constructor(input, reason) {
|
|
18
|
+
super(`Invalid color ${JSON.stringify(input)}: ${reason}`);
|
|
19
|
+
this.name = "ColorParseError";
|
|
20
|
+
this.input = input;
|
|
21
|
+
}
|
|
22
|
+
}
|
package/dist/color.d.ts
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The immutable `Color` value. It stores extended sRGB as unclamped floats plus alpha, so a
|
|
3
|
+
* wide-gamut color from `oklch()` or `color(display-p3)` survives losslessly, and converts to
|
|
4
|
+
* Oklab or OKLCH on demand for every perceptual operation, contrast check and scale.
|
|
5
|
+
*
|
|
6
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
7
|
+
* @copyright Sergio Xalambrí 2026
|
|
8
|
+
*/
|
|
9
|
+
import type { Result } from "@sdxc/result";
|
|
10
|
+
import type { ColorParseError } from "./color-parse-error.js";
|
|
11
|
+
import type { ContrastAlgorithm, WcagContrast } from "./contrast.js";
|
|
12
|
+
import type { InterpolationOptions } from "./interpolate.js";
|
|
13
|
+
import { ContrastUnreachableError } from "./contrast-unreachable-error.js";
|
|
14
|
+
/** sRGB channels, nominally 0–1 but unclamped for wide-gamut colors, and alpha 0–1. */
|
|
15
|
+
export interface Rgb {
|
|
16
|
+
r: number;
|
|
17
|
+
g: number;
|
|
18
|
+
b: number;
|
|
19
|
+
alpha: number;
|
|
20
|
+
}
|
|
21
|
+
/** HSL with hue in degrees `[0, 360)` and saturation, lightness and alpha 0–1. */
|
|
22
|
+
export interface Hsl {
|
|
23
|
+
h: number;
|
|
24
|
+
s: number;
|
|
25
|
+
l: number;
|
|
26
|
+
alpha: number;
|
|
27
|
+
}
|
|
28
|
+
/** Oklab with lightness 0–1 and a, b roughly ±0.4. */
|
|
29
|
+
export interface Oklab {
|
|
30
|
+
l: number;
|
|
31
|
+
a: number;
|
|
32
|
+
b: number;
|
|
33
|
+
alpha: number;
|
|
34
|
+
}
|
|
35
|
+
/** OKLCH with lightness 0–1, chroma from 0 (about 0.37 at sRGB's edge) and hue in degrees. */
|
|
36
|
+
export interface Oklch {
|
|
37
|
+
l: number;
|
|
38
|
+
c: number;
|
|
39
|
+
h: number;
|
|
40
|
+
alpha: number;
|
|
41
|
+
}
|
|
42
|
+
/** The exact OKLCH values `Color#with` sets; anything omitted keeps the color's own. */
|
|
43
|
+
export interface OklchChanges {
|
|
44
|
+
lightness?: number;
|
|
45
|
+
chroma?: number;
|
|
46
|
+
hue?: number;
|
|
47
|
+
alpha?: number;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* The CSS `Color#toString` prints. `"hex"`, `"rgb"` and `"hsl"` describe sRGB only, so they
|
|
51
|
+
* gamut-map first; `"srgb"` (`color(srgb …)`), `"oklch"` and `"oklab"` are lossless.
|
|
52
|
+
*/
|
|
53
|
+
export type ColorFormat = "hex" | "rgb" | "hsl" | "oklch" | "oklab" | "srgb";
|
|
54
|
+
/**
|
|
55
|
+
* An immutable CSS color. Build one with `Color.parse` or a numeric constructor; every
|
|
56
|
+
* operation answers a new `Color`. An authored hue on a gray and `none` components are not
|
|
57
|
+
* kept: a gray reads back with hue 0, and `none` reads as 0.
|
|
58
|
+
*/
|
|
59
|
+
export declare class Color {
|
|
60
|
+
#private;
|
|
61
|
+
/** Stores the channels, reading a non-finite one as 0 and clamping alpha to 0–1. */
|
|
62
|
+
private constructor();
|
|
63
|
+
/**
|
|
64
|
+
* Reads any CSS color: hex, named colors and `transparent`, `rgb()`, `hsl()`, `hwb()`,
|
|
65
|
+
* `lab()`, `lch()`, `oklab()`, `oklch()` and `color()` in its predefined spaces, both
|
|
66
|
+
* as authored and as `getComputedStyle` serializes them.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* Color.parse("oklch(0.24 0.008 70)"); // Success<Color>
|
|
70
|
+
* @example
|
|
71
|
+
* Color.parse("currentcolor"); // Failure<ColorParseError>
|
|
72
|
+
*/
|
|
73
|
+
static parse(input: string): Result<Color, ColorParseError>;
|
|
74
|
+
/** A color from sRGB channels 0–1, kept unclamped so values outside describe wide gamut. */
|
|
75
|
+
static rgb(r: number, g: number, b: number, alpha?: number): Color;
|
|
76
|
+
/** A color from HSL: hue in degrees, saturation and lightness 0–1. */
|
|
77
|
+
static hsl(hue: number, saturation: number, lightness: number, alpha?: number): Color;
|
|
78
|
+
/** A color from HWB: hue in degrees, whiteness and blackness 0–1. */
|
|
79
|
+
static hwb(hue: number, whiteness: number, blackness: number, alpha?: number): Color;
|
|
80
|
+
/** A color from Oklab: lightness 0–1 and a, b roughly ±0.4. */
|
|
81
|
+
static oklab(lightness: number, a: number, b: number, alpha?: number): Color;
|
|
82
|
+
/** A color from OKLCH: lightness 0–1, chroma from 0 and hue in degrees. */
|
|
83
|
+
static oklch(lightness: number, chroma: number, hue: number, alpha?: number): Color;
|
|
84
|
+
/** The contrast of `foreground` painted on `background`; see `Color#contrast`. */
|
|
85
|
+
static contrast(foreground: Color, background: Color, algorithm?: ContrastAlgorithm): WcagContrast;
|
|
86
|
+
/**
|
|
87
|
+
* A gradient through `colors`, evenly spaced, as a function from `t` in 0–1 to a color.
|
|
88
|
+
* `t` is clamped, and each segment interpolates as `mix` does.
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* let heat = Color.scale([blue, yellow, red]); heat(0.5); // yellow
|
|
92
|
+
*/
|
|
93
|
+
static scale(colors: readonly [Color, ...Color[]], options?: InterpolationOptions): (t: number) => Color;
|
|
94
|
+
/**
|
|
95
|
+
* `count` evenly spaced colors along `Color.scale(colors, options)`, both ends included.
|
|
96
|
+
* One step answers the first color, and fewer than one answers none.
|
|
97
|
+
*/
|
|
98
|
+
static steps(colors: readonly [Color, ...Color[]], count: number, options?: InterpolationOptions): Color[];
|
|
99
|
+
/** Opacity from 0 (transparent) to 1 (opaque). */
|
|
100
|
+
get alpha(): number;
|
|
101
|
+
/** The stored channels, unclamped, so a wide-gamut color reads below 0 or above 1. */
|
|
102
|
+
toRgb(): Rgb;
|
|
103
|
+
/** The color as HSL, gamut-mapped first because HSL cannot describe anything outside sRGB. */
|
|
104
|
+
toHsl(): Hsl;
|
|
105
|
+
/** The color in Oklab, unclamped. */
|
|
106
|
+
toOklab(): Oklab;
|
|
107
|
+
/** The color in OKLCH, unclamped; an achromatic color answers a hue of 0. */
|
|
108
|
+
toOklch(): Oklch;
|
|
109
|
+
/** Whether every sRGB channel and alpha differs from `other`'s by at most `epsilon`. */
|
|
110
|
+
equals(other: Color, epsilon?: number): boolean;
|
|
111
|
+
/** Whether the color fits in sRGB, so every display can show it as is. */
|
|
112
|
+
inGamut(): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* The color mapped into sRGB by the CSS Color 4 algorithm, which lowers OKLCH chroma and
|
|
115
|
+
* keeps lightness and hue. A color already inside answers itself.
|
|
116
|
+
*/
|
|
117
|
+
toGamut(): Color;
|
|
118
|
+
/**
|
|
119
|
+
* Valid CSS for the color, so `${color}` works anywhere CSS does. Without a format it is
|
|
120
|
+
* `rgb(r g b / a)` inside sRGB and `oklch()` outside, which no display clips.
|
|
121
|
+
*/
|
|
122
|
+
toString(format?: ColorFormat): string;
|
|
123
|
+
/**
|
|
124
|
+
* This color painted over `background` with source-over compositing in gamma-encoded
|
|
125
|
+
* sRGB, the way browsers paint a translucent layer.
|
|
126
|
+
*/
|
|
127
|
+
over(background: Color): Color;
|
|
128
|
+
/**
|
|
129
|
+
* This color mixed with `other` by CSS color-mix() rules, premultiplied alpha included.
|
|
130
|
+
* `amount` is the share of `other`: 0 answers this color and 1 answers `other`.
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* red.mix(blue, 0.25, { space: "oklch", hue: "longer" });
|
|
134
|
+
*/
|
|
135
|
+
mix(other: Color, amount?: number, options?: InterpolationOptions): Color;
|
|
136
|
+
/** The same color at opacity `alpha`, clamped to 0–1. */
|
|
137
|
+
withAlpha(alpha: number): Color;
|
|
138
|
+
/** The same color with its alpha multiplied by `factor`. */
|
|
139
|
+
fade(factor: number): Color;
|
|
140
|
+
/** OKLCH lightness multiplied by `1 + amount`, clamped to 1: `lighten(0.1)` is 10% lighter. */
|
|
141
|
+
lighten(amount: number): Color;
|
|
142
|
+
/** OKLCH lightness multiplied by `1 - amount`, clamped to 0: `darken(0.1)` is 10% darker. */
|
|
143
|
+
darken(amount: number): Color;
|
|
144
|
+
/** OKLCH chroma multiplied by `1 + amount`; the result may leave sRGB, see `toGamut`. */
|
|
145
|
+
saturate(amount: number): Color;
|
|
146
|
+
/** OKLCH chroma multiplied by `1 - amount`, never below 0. */
|
|
147
|
+
desaturate(amount: number): Color;
|
|
148
|
+
/** The OKLCH hue turned by `degrees`, either way round. */
|
|
149
|
+
rotate(degrees: number): Color;
|
|
150
|
+
/** The gray with the same OKLCH lightness. */
|
|
151
|
+
grayscale(): Color;
|
|
152
|
+
/** The color opposite on the OKLCH hue circle, at the same lightness and chroma. */
|
|
153
|
+
complement(): Color;
|
|
154
|
+
/** Each sRGB channel inverted (`1 - channel`), alpha kept. */
|
|
155
|
+
invert(): Color;
|
|
156
|
+
/**
|
|
157
|
+
* The color with exact OKLCH values set. A gray starts from hue 0, so giving it chroma
|
|
158
|
+
* without a hue turns it pink.
|
|
159
|
+
*/
|
|
160
|
+
with(changes: OklchChanges): Color;
|
|
161
|
+
/**
|
|
162
|
+
* The contrast of this color as text or a UI part on `background`. A translucent
|
|
163
|
+
* background is painted over white first and this color is painted over that, so the
|
|
164
|
+
* ratio is what a reader sees; both are gamut-mapped to sRGB before measuring.
|
|
165
|
+
* Order matters only for translucent colors; two opaque colors measure the same either way.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* Color.rgb(0, 0, 0).contrast(Color.rgb(1, 1, 1)).aa.text; // true, 21:1
|
|
169
|
+
*/
|
|
170
|
+
contrast(background: Color, _algorithm?: ContrastAlgorithm): WcagContrast;
|
|
171
|
+
/**
|
|
172
|
+
* The color closest to this one that reaches `ratio` against `background`, changing only
|
|
173
|
+
* OKLCH lightness, in whichever direction gets there with the smaller change. The answer
|
|
174
|
+
* is gamut-mapped, since that is the color contrast was measured on.
|
|
175
|
+
*
|
|
176
|
+
* @returns This color when it already passes, or a `ContrastUnreachableError` holding the
|
|
177
|
+
* best lightness extreme when neither black-ward nor white-ward reaches `ratio`.
|
|
178
|
+
*/
|
|
179
|
+
ensureContrast(background: Color, ratio: number): Result<Color, ContrastUnreachableError>;
|
|
180
|
+
/** Black or white, whichever contrasts more as text on this color; black wins a tie. */
|
|
181
|
+
contrastingText(): Color;
|
|
182
|
+
}
|