@tenphi/glaze 0.1.0

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,298 @@
1
+ //#region src/contrast-solver.d.ts
2
+ /**
3
+ * OKHSL Contrast Solver
4
+ *
5
+ * Finds the closest OKHSL lightness that satisfies a WCAG 2 contrast target
6
+ * against a base color. Used by glaze when resolving dependent colors
7
+ * with `ensureContrast`.
8
+ */
9
+ type ContrastPreset = 'AA' | 'AAA' | 'AA-large' | 'AAA-large';
10
+ type MinContrast$1 = number | ContrastPreset;
11
+ interface FindLightnessForContrastOptions {
12
+ /** Hue of the candidate color (0–360). */
13
+ hue: number;
14
+ /** Saturation of the candidate color (0–1). */
15
+ saturation: number;
16
+ /** Preferred lightness of the candidate (0–1). */
17
+ preferredLightness: number;
18
+ /** Base/reference color as linear sRGB (channels may be outside 0–1 before clamp). */
19
+ baseLinearRgb: [number, number, number];
20
+ /** Ensures the WCAG contrast ratio meets a target floor. */
21
+ ensureContrast: MinContrast$1;
22
+ /** Search bounds for lightness. Default: [0, 1]. */
23
+ lightnessRange?: [number, number];
24
+ /** Convergence threshold. Default: 1e-4. */
25
+ epsilon?: number;
26
+ /** Maximum binary-search iterations per branch. Default: 14. */
27
+ maxIterations?: number;
28
+ }
29
+ interface FindLightnessForContrastResult {
30
+ /** Chosen lightness in 0–1. */
31
+ lightness: number;
32
+ /** Achieved WCAG contrast ratio. */
33
+ contrast: number;
34
+ /** Whether the target was reached. */
35
+ met: boolean;
36
+ /** Which branch was selected. */
37
+ branch: 'lighter' | 'darker' | 'preferred';
38
+ }
39
+ declare function resolveMinContrast(value: MinContrast$1): number;
40
+ /**
41
+ * Find the OKHSL lightness that satisfies a WCAG 2 contrast target
42
+ * against a base color, staying as close to `preferredLightness` as possible.
43
+ */
44
+ declare function findLightnessForContrast(options: FindLightnessForContrastOptions): FindLightnessForContrastResult;
45
+ //#endregion
46
+ //#region src/types.d.ts
47
+ /** A value or [normal, high-contrast] pair. */
48
+ type HCPair<T> = T | [T, T];
49
+ type MinContrast = number | ContrastPreset;
50
+ type AdaptationMode = 'auto' | 'fixed' | 'static';
51
+ /** Color format for output. */
52
+ type GlazeColorFormat = 'okhsl' | 'rgb' | 'hsl' | 'oklch';
53
+ /**
54
+ * Controls which scheme variants are generated in the export.
55
+ * Light is always included (it's the default).
56
+ */
57
+ interface GlazeOutputModes {
58
+ /** Include dark scheme variants. Default: true. */
59
+ dark?: boolean;
60
+ /** Include high-contrast variants (both light-HC and dark-HC). Default: false. */
61
+ highContrast?: boolean;
62
+ }
63
+ interface ColorDef {
64
+ /** Lightness in the light scheme (0–100). Root color. */
65
+ l?: HCPair<number>;
66
+ /** Saturation factor applied to the seed saturation (0–1, default: 1). */
67
+ sat?: number;
68
+ /** Name of another color in the same theme (dependent color). */
69
+ base?: string;
70
+ /** Lightness delta from the base color. */
71
+ contrast?: HCPair<number>;
72
+ /** Ensures the WCAG contrast ratio meets a target floor against the base. */
73
+ ensureContrast?: HCPair<MinContrast>;
74
+ /** Adaptation mode. Default: 'auto'. */
75
+ mode?: AdaptationMode;
76
+ }
77
+ type ColorMap = Record<string, ColorDef>;
78
+ /** Resolved color for a single scheme variant. */
79
+ interface ResolvedColorVariant {
80
+ /** OKHSL hue (0–360). */
81
+ h: number;
82
+ /** OKHSL saturation (0–1). */
83
+ s: number;
84
+ /** OKHSL lightness (0–1). */
85
+ l: number;
86
+ }
87
+ /** Fully resolved color across all scheme variants. */
88
+ interface ResolvedColor {
89
+ name: string;
90
+ light: ResolvedColorVariant;
91
+ dark: ResolvedColorVariant;
92
+ lightContrast: ResolvedColorVariant;
93
+ darkContrast: ResolvedColorVariant;
94
+ mode: AdaptationMode;
95
+ }
96
+ interface GlazeConfig {
97
+ /** Dark scheme lightness window [lo, hi]. Default: [10, 90]. */
98
+ darkLightness?: [number, number];
99
+ /** Saturation reduction factor for dark scheme (0–1). Default: 0.1. */
100
+ darkDesaturation?: number;
101
+ /** State alias names for token export. */
102
+ states?: {
103
+ dark?: string;
104
+ highContrast?: string;
105
+ };
106
+ /** Which scheme variants to include in exports. Default: both true. */
107
+ modes?: GlazeOutputModes;
108
+ }
109
+ interface GlazeConfigResolved {
110
+ darkLightness: [number, number];
111
+ darkDesaturation: number;
112
+ states: {
113
+ dark: string;
114
+ highContrast: string;
115
+ };
116
+ modes: Required<GlazeOutputModes>;
117
+ }
118
+ /** Serialized theme configuration (no resolved values). */
119
+ interface GlazeThemeExport {
120
+ hue: number;
121
+ saturation: number;
122
+ colors: ColorMap;
123
+ }
124
+ /** Input for `glaze.color()` standalone factory. */
125
+ interface GlazeColorInput {
126
+ hue: number;
127
+ saturation: number;
128
+ l: HCPair<number>;
129
+ sat?: number;
130
+ mode?: AdaptationMode;
131
+ }
132
+ /** Return type for `glaze.color()`. */
133
+ interface GlazeColorToken {
134
+ /** Resolve the color across all scheme variants. */
135
+ resolve(): ResolvedColor;
136
+ /** Export as a flat token map (no color name key). */
137
+ token(options?: GlazeTokenOptions): Record<string, string>;
138
+ /** Export as a flat JSON map (no color name key). */
139
+ json(options?: GlazeJsonOptions): Record<string, string>;
140
+ }
141
+ interface GlazeTheme {
142
+ /** The hue seed (0–360). */
143
+ readonly hue: number;
144
+ /** The saturation seed (0–100). */
145
+ readonly saturation: number;
146
+ /** Add/replace colors (additive merge with existing definitions). */
147
+ colors(defs: ColorMap): void;
148
+ /** Get a color definition by name. */
149
+ color(name: string): ColorDef | undefined;
150
+ /** Set a single color definition. */
151
+ color(name: string, def: ColorDef): void;
152
+ /** Remove one or more color definitions. */
153
+ remove(names: string | string[]): void;
154
+ /** Check if a color is defined. */
155
+ has(name: string): boolean;
156
+ /** List all defined color names. */
157
+ list(): string[];
158
+ /** Clear all color definitions. */
159
+ reset(): void;
160
+ /** Export the theme configuration as a JSON-safe object. */
161
+ export(): GlazeThemeExport;
162
+ /** Create a child theme inheriting all color definitions. */
163
+ extend(options: GlazeExtendOptions): GlazeTheme;
164
+ /** Resolve all colors and return the result map. */
165
+ resolve(): Map<string, ResolvedColor>;
166
+ /** Export as token map. */
167
+ tokens(options?: GlazeTokenOptions): Record<string, Record<string, string>>;
168
+ /** Export as plain JSON. */
169
+ json(options?: GlazeJsonOptions): Record<string, Record<string, string>>;
170
+ }
171
+ interface GlazeExtendOptions {
172
+ hue?: number;
173
+ saturation?: number;
174
+ colors?: ColorMap;
175
+ }
176
+ interface GlazeTokenOptions {
177
+ /** Prefix mode. `true` uses "<themeName>-", or provide a custom map. */
178
+ prefix?: boolean | Record<string, string>;
179
+ /** Override state aliases for this export. */
180
+ states?: {
181
+ dark?: string;
182
+ highContrast?: string;
183
+ };
184
+ /** Override which scheme variants to include. */
185
+ modes?: GlazeOutputModes;
186
+ /** Output color format. Default: 'okhsl'. */
187
+ format?: GlazeColorFormat;
188
+ }
189
+ interface GlazeJsonOptions {
190
+ /** Override which scheme variants to include. */
191
+ modes?: GlazeOutputModes;
192
+ /** Output color format. Default: 'okhsl'. */
193
+ format?: GlazeColorFormat;
194
+ }
195
+ interface GlazePalette {
196
+ /** Export all themes as a combined token map. */
197
+ tokens(options?: GlazeTokenOptions): Record<string, Record<string, string>>;
198
+ /** Export all themes as plain JSON. */
199
+ json(options?: GlazeJsonOptions & {
200
+ prefix?: boolean | Record<string, string>;
201
+ }): Record<string, Record<string, Record<string, string>>>;
202
+ }
203
+ //#endregion
204
+ //#region src/glaze.d.ts
205
+ type PaletteInput = Record<string, GlazeTheme>;
206
+ /**
207
+ * Create a single-hue glaze theme.
208
+ *
209
+ * @example
210
+ * ```ts
211
+ * const primary = glaze({ hue: 280, saturation: 80 });
212
+ * // or shorthand:
213
+ * const primary = glaze(280, 80);
214
+ * ```
215
+ */
216
+ declare function glaze(hueOrOptions: number | {
217
+ hue: number;
218
+ saturation: number;
219
+ }, saturation?: number): GlazeTheme;
220
+ declare namespace glaze {
221
+ var configure: (config: GlazeConfig) => void;
222
+ var palette: (themes: PaletteInput) => {
223
+ tokens(options?: GlazeTokenOptions): Record<string, Record<string, string>>;
224
+ json(options?: GlazeJsonOptions & {
225
+ prefix?: boolean | Record<string, string>;
226
+ }): Record<string, Record<string, Record<string, string>>>;
227
+ };
228
+ var from: (data: GlazeThemeExport) => GlazeTheme;
229
+ var color: (input: GlazeColorInput) => GlazeColorToken;
230
+ var fromHex: (hex: string) => GlazeTheme;
231
+ var fromRgb: (r: number, g: number, b: number) => GlazeTheme;
232
+ var getConfig: () => GlazeConfigResolved;
233
+ var resetConfig: () => void;
234
+ }
235
+ //#endregion
236
+ //#region src/okhsl-color-math.d.ts
237
+ /**
238
+ * OKHSL color math primitives for the glaze theme generator.
239
+ *
240
+ * Provides bidirectional OKHSL ↔ sRGB conversion, relative luminance
241
+ * computation for WCAG 2 contrast calculations, and multi-format output
242
+ * (okhsl, rgb, hsl, oklch).
243
+ */
244
+ /**
245
+ * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to linear sRGB.
246
+ * Channels may exceed [0, 1] near gamut boundaries — caller must clamp if needed.
247
+ */
248
+ declare function okhslToLinearSrgb(h: number, s: number, l: number): [number, number, number];
249
+ /**
250
+ * Compute relative luminance Y from linear sRGB channels.
251
+ * Per WCAG 2: Y = 0.2126·R + 0.7152·G + 0.0722·B
252
+ */
253
+ declare function relativeLuminanceFromLinearRgb(rgb: [number, number, number]): number;
254
+ /**
255
+ * WCAG 2 contrast ratio from two luminance values.
256
+ */
257
+ declare function contrastRatioFromLuminance(yA: number, yB: number): number;
258
+ /**
259
+ * Convert OKHSL to gamma-encoded sRGB (clamped to 0–1).
260
+ */
261
+ declare function okhslToSrgb(h: number, s: number, l: number): [number, number, number];
262
+ /**
263
+ * Convert OKHSL (h: 0–360, s: 0–1, l: 0–1) to OKLab [L, a, b].
264
+ */
265
+ declare function okhslToOklab(h: number, s: number, l: number): [number, number, number];
266
+ /**
267
+ * Convert gamma-encoded sRGB (0–1 per channel) to OKHSL.
268
+ * Returns [h, s, l] where h: 0–360, s: 0–1, l: 0–1.
269
+ */
270
+ declare function srgbToOkhsl(rgb: [number, number, number]): [number, number, number];
271
+ /**
272
+ * Parse a hex color string (#rgb or #rrggbb) to sRGB [r, g, b] in 0–1 range.
273
+ * Returns null if the string is not a valid hex color.
274
+ */
275
+ declare function parseHex(hex: string): [number, number, number] | null;
276
+ /**
277
+ * Format OKHSL values as a CSS `okhsl(H S% L%)` string.
278
+ * h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
279
+ */
280
+ declare function formatOkhsl(h: number, s: number, l: number): string;
281
+ /**
282
+ * Format OKHSL values as a CSS `rgb(R, G, B)` string with fractional 0–255 values.
283
+ * h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
284
+ */
285
+ declare function formatRgb(h: number, s: number, l: number): string;
286
+ /**
287
+ * Format OKHSL values as a CSS `hsl(H, S%, L%)` string.
288
+ * h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
289
+ */
290
+ declare function formatHsl(h: number, s: number, l: number): string;
291
+ /**
292
+ * Format OKHSL values as a CSS `oklch(L% C H)` string.
293
+ * h: 0–360, s: 0–100, l: 0–100 (percentage scale for s and l).
294
+ */
295
+ declare function formatOklch(h: number, s: number, l: number): string;
296
+ //#endregion
297
+ export { type AdaptationMode, type ColorDef, type ColorMap, type ContrastPreset, type FindLightnessForContrastOptions, type FindLightnessForContrastResult, type GlazeColorFormat, type GlazeColorInput, type GlazeColorToken, type GlazeConfig, type GlazeExtendOptions, type GlazeJsonOptions, type GlazeOutputModes, type GlazePalette, type GlazeTheme, type GlazeThemeExport, type GlazeTokenOptions, type HCPair, type MinContrast, type ResolvedColor, type ResolvedColorVariant, contrastRatioFromLuminance, findLightnessForContrast, formatHsl, formatOkhsl, formatOklch, formatRgb, glaze, okhslToLinearSrgb, okhslToOklab, okhslToSrgb, parseHex, relativeLuminanceFromLinearRgb, resolveMinContrast, srgbToOkhsl };
298
+ //# sourceMappingURL=index.d.mts.map