@ariaui/color 0.1.2 → 0.1.4

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.
@@ -0,0 +1,213 @@
1
+ /**
2
+ * A color in the OKLCH color space.
3
+ * - `l`: Lightness, 0–1 (0 = black, 1 = white)
4
+ * - `c`: Chroma, 0–~0.4 (0 = gray, higher = more saturated)
5
+ * - `h`: Hue, 0–360 degrees
6
+ * - `alpha`: Opacity, 0–1
7
+ */
8
+ interface OklchColor {
9
+ readonly l: number;
10
+ readonly c: number;
11
+ readonly h: number;
12
+ readonly alpha: number;
13
+ }
14
+ /**
15
+ * A color in the OKLab color space (intermediate for conversions).
16
+ */
17
+ interface OklabColor {
18
+ readonly L: number;
19
+ readonly a: number;
20
+ readonly b: number;
21
+ readonly alpha: number;
22
+ }
23
+ /**
24
+ * A color in the sRGB color space.
25
+ * - `r`, `g`, `b`: 0–1 range (not 0–255)
26
+ * - `alpha`: 0–1
27
+ */
28
+ interface SrgbColor {
29
+ readonly r: number;
30
+ readonly g: number;
31
+ readonly b: number;
32
+ readonly alpha: number;
33
+ }
34
+ /** A CSS hex color string. */
35
+ type HexString = `#${string}`;
36
+ /** Semantic color mode. */
37
+ type ColorMode = "light" | "dark";
38
+
39
+ /**
40
+ * Parse an OKLCH channel string as used by `@ariaui/tokens` primitives.
41
+ *
42
+ * Accepts formats:
43
+ * - `"55.2% 0.014 286"` (L with %)
44
+ * - `"0.552 0.014 286"` (L as decimal)
45
+ * - `"55.2% 0.014 286 / 0.5"` (with alpha)
46
+ *
47
+ * @throws if the string cannot be parsed.
48
+ */
49
+ declare function parseOklchChannels(channels: string): OklchColor;
50
+ /**
51
+ * Parse an `oklch(...)` CSS function string.
52
+ *
53
+ * Accepts: `oklch(55.2% 0.014 286)`, `oklch(55.2% 0.014 286 / 0.5)`
54
+ */
55
+ declare function parseOklchCss(css: string): OklchColor;
56
+ /**
57
+ * Parse a hex color string.
58
+ *
59
+ * Accepts: `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`
60
+ */
61
+ declare function parseHex(hex: string): SrgbColor;
62
+ /**
63
+ * Parse an `rgb(...)` or `rgba(...)` CSS function string.
64
+ *
65
+ * Accepts modern space-separated (`rgb(139 92 246)`, `rgb(139 92 246 / 0.5)`)
66
+ * and legacy comma-separated (`rgb(139, 92, 246)`, `rgba(139, 92, 246, 0.5)`).
67
+ */
68
+ declare function parseRgb(css: string): SrgbColor;
69
+
70
+ /** Convert OKLCH → OKLab */
71
+ declare function oklchToOklab(c: OklchColor): OklabColor;
72
+ /** Convert OKLab → OKLCH */
73
+ declare function oklabToOklch(lab: OklabColor): OklchColor;
74
+ /**
75
+ * Convert OKLCH → sRGB with CSS Color Level 4 gamut mapping.
76
+ * Out-of-gamut colors are mapped by reducing chroma while preserving
77
+ * lightness and hue, per the spec.
78
+ */
79
+ declare function oklchToSrgb(color: OklchColor): SrgbColor;
80
+ /**
81
+ * Convert sRGB → OKLCH.
82
+ */
83
+ declare function srgbToOklch(color: SrgbColor): OklchColor;
84
+ /**
85
+ * Convert sRGB → hex string.
86
+ * Values are clamped to [0, 255]. Alpha is included only if < 1.
87
+ */
88
+ declare function srgbToHex(color: SrgbColor): HexString;
89
+ /**
90
+ * Convert OKLCH → hex string (via sRGB with gamut mapping).
91
+ */
92
+ declare function oklchToHex(color: OklchColor): HexString;
93
+ /**
94
+ * Convert hex string → OKLCH.
95
+ */
96
+ declare function hexToOklch(hex: string): OklchColor;
97
+
98
+ /**
99
+ * Increase lightness by `amount` (0–1).
100
+ * `lighten(color, 0.1)` adds 0.1 to lightness (clamped to 1).
101
+ */
102
+ declare function lighten(color: OklchColor, amount: number): OklchColor;
103
+ /**
104
+ * Decrease lightness by `amount` (0–1).
105
+ * `darken(color, 0.1)` subtracts 0.1 from lightness (clamped to 0).
106
+ */
107
+ declare function darken(color: OklchColor, amount: number): OklchColor;
108
+ /**
109
+ * Increase chroma by `amount`.
110
+ * `saturate(color, 0.05)` adds 0.05 to chroma (clamped to 0).
111
+ */
112
+ declare function saturate(color: OklchColor, amount: number): OklchColor;
113
+ /**
114
+ * Decrease chroma by `amount`.
115
+ * `desaturate(color, 0.05)` subtracts 0.05 from chroma (clamped to 0).
116
+ */
117
+ declare function desaturate(color: OklchColor, amount: number): OklchColor;
118
+ /**
119
+ * Set the alpha channel.
120
+ */
121
+ declare function setAlpha(color: OklchColor, alpha: number): OklchColor;
122
+ /**
123
+ * Rotate the hue by `degrees`.
124
+ * `adjustHue(color, 30)` rotates hue by +30°.
125
+ */
126
+ declare function adjustHue(color: OklchColor, degrees: number): OklchColor;
127
+ /**
128
+ * Mix two OKLCH colors by linear interpolation.
129
+ *
130
+ * @param color1 - First color
131
+ * @param color2 - Second color
132
+ * @param ratio - 0 = 100% color1, 1 = 100% color2 (default 0.5)
133
+ *
134
+ * Hue interpolation uses the shorter arc.
135
+ */
136
+ declare function mix(color1: OklchColor, color2: OklchColor, ratio?: number): OklchColor;
137
+ /**
138
+ * Create a complement (hue + 180°).
139
+ */
140
+ declare function complement(color: OklchColor): OklchColor;
141
+ /**
142
+ * Invert a color (flip lightness).
143
+ */
144
+ declare function invert(color: OklchColor): OklchColor;
145
+ /**
146
+ * Convert to grayscale (set chroma to 0).
147
+ */
148
+ declare function grayscale(color: OklchColor): OklchColor;
149
+
150
+ /**
151
+ * Calculate WCAG 2.1 contrast ratio between two colors.
152
+ * Returns a value between 1 and 21.
153
+ *
154
+ * Accepts either OKLCH or sRGB colors (OKLCH are converted via gamut mapping).
155
+ */
156
+ declare function contrastRatio(fg: OklchColor | SrgbColor, bg: OklchColor | SrgbColor): number;
157
+ /**
158
+ * Check if a fg/bg pair meets WCAG 2.1 AA contrast requirements.
159
+ *
160
+ * - Normal text: ratio ≥ 4.5
161
+ * - Large text (≥18pt or ≥14pt bold): ratio ≥ 3
162
+ */
163
+ declare function meetsAA(fg: OklchColor | SrgbColor, bg: OklchColor | SrgbColor, size?: "normal" | "large"): boolean;
164
+ /**
165
+ * Check if a fg/bg pair meets WCAG 2.1 AAA contrast requirements.
166
+ *
167
+ * - Normal text: ratio ≥ 7
168
+ * - Large text: ratio ≥ 4.5
169
+ */
170
+ declare function meetsAAA(fg: OklchColor | SrgbColor, bg: OklchColor | SrgbColor, size?: "normal" | "large"): boolean;
171
+ /**
172
+ * Suggest an accessible foreground color by adjusting lightness to meet
173
+ * the target contrast level against the given background.
174
+ *
175
+ * Tries darkening first, then lightening. Returns the closest accessible
176
+ * OklchColor, or the original if it already passes.
177
+ *
178
+ * @param fg - Foreground color to adjust (OKLCH)
179
+ * @param bg - Background color (OKLCH or sRGB)
180
+ * @param target - "AA" (4.5:1) or "AAA" (7:1), default "AA"
181
+ * @param size - "normal" or "large" text
182
+ */
183
+ declare function suggestAccessible(fg: OklchColor, bg: OklchColor | SrgbColor, target?: "AA" | "AAA", size?: "normal" | "large"): OklchColor;
184
+
185
+ /**
186
+ * Format an OKLCH color as a CSS `oklch()` string.
187
+ *
188
+ * @example toOklchString({ l: 0.552, c: 0.014, h: 286, alpha: 1 })
189
+ * → "oklch(55.2% 0.014 286)"
190
+ */
191
+ declare function toOklchString(color: OklchColor): string;
192
+ /**
193
+ * Format an sRGB color as a CSS `rgb()` string (modern syntax).
194
+ *
195
+ * @example toRgbString({ r: 0.545, g: 0.361, b: 0.965, alpha: 1 })
196
+ * → "rgb(139 92 246)"
197
+ */
198
+ declare function toRgbString(color: SrgbColor): string;
199
+ /**
200
+ * Format a color as a hex string.
201
+ * Accepts OKLCH (converts via gamut mapping) or sRGB.
202
+ */
203
+ declare function toHexString(color: OklchColor | SrgbColor): HexString;
204
+ /**
205
+ * Format an OKLCH color back to the bare channel string format
206
+ * used by `@ariaui/tokens` primitives: `"L% C H"`.
207
+ *
208
+ * @example toCssChannels({ l: 0.552, c: 0.014, h: 286, alpha: 1 })
209
+ * → "55.2% 0.014 286"
210
+ */
211
+ declare function toCssChannels(color: OklchColor): string;
212
+
213
+ export { type ColorMode, type HexString, type OklabColor, type OklchColor, type SrgbColor, adjustHue, complement, contrastRatio, darken, desaturate, grayscale, hexToOklch, invert, lighten, meetsAA, meetsAAA, mix, oklabToOklch, oklchToHex, oklchToOklab, oklchToSrgb, parseHex, parseOklchChannels, parseOklchCss, parseRgb, saturate, setAlpha, srgbToHex, srgbToOklch, suggestAccessible, toCssChannels, toHexString, toOklchString, toRgbString };
package/dist/index.js ADDED
@@ -0,0 +1,62 @@
1
+ import {
2
+ adjustHue,
3
+ complement,
4
+ contrastRatio,
5
+ darken,
6
+ desaturate,
7
+ grayscale,
8
+ hexToOklch,
9
+ invert,
10
+ lighten,
11
+ meetsAA,
12
+ meetsAAA,
13
+ mix,
14
+ oklabToOklch,
15
+ oklchToHex,
16
+ oklchToOklab,
17
+ oklchToSrgb,
18
+ parseHex,
19
+ parseOklchChannels,
20
+ parseOklchCss,
21
+ parseRgb,
22
+ saturate,
23
+ setAlpha,
24
+ srgbToHex,
25
+ srgbToOklch,
26
+ suggestAccessible,
27
+ toCssChannels,
28
+ toHexString,
29
+ toOklchString,
30
+ toRgbString
31
+ } from "./chunk-3BGTZVEU.js";
32
+ export {
33
+ adjustHue,
34
+ complement,
35
+ contrastRatio,
36
+ darken,
37
+ desaturate,
38
+ grayscale,
39
+ hexToOklch,
40
+ invert,
41
+ lighten,
42
+ meetsAA,
43
+ meetsAAA,
44
+ mix,
45
+ oklabToOklch,
46
+ oklchToHex,
47
+ oklchToOklab,
48
+ oklchToSrgb,
49
+ parseHex,
50
+ parseOklchChannels,
51
+ parseOklchCss,
52
+ parseRgb,
53
+ saturate,
54
+ setAlpha,
55
+ srgbToHex,
56
+ srgbToOklch,
57
+ suggestAccessible,
58
+ toCssChannels,
59
+ toHexString,
60
+ toOklchString,
61
+ toRgbString
62
+ };