@usalyt/registry 0.0.1 → 0.0.2

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,1023 @@
1
+ export type ThemeBaseId = "graphite" | "linear" | "zinc" | "slate" | "stone";
2
+ export type ThemeAccentId =
3
+ | "signal-orange"
4
+ | "lavender"
5
+ | "cobalt"
6
+ | "emerald"
7
+ | "amber"
8
+ | "crimson"
9
+ | "monochrome";
10
+ export type ThemeRadiusId = "none" | "sm" | "md" | "lg" | "xl";
11
+ export type ThemeDensityId = "compact" | "default" | "comfortable";
12
+ export type ThemeFontId = "inter" | "system" | "geist" | "jakarta";
13
+ export type ThemePolarity = "dark" | "light";
14
+ export type ThemeSemanticKey = "success" | "warning" | "danger" | "info";
15
+
16
+ export interface ThemeSemanticOverrides {
17
+ success?: string;
18
+ warning?: string;
19
+ danger?: string;
20
+ info?: string;
21
+ }
22
+
23
+ export interface ThemePresetConfig {
24
+ base: ThemeBaseId;
25
+ /** Either a built-in accent ID or a 6-digit hex color such as "#c83e0b". */
26
+ accent: ThemeAccentId | string;
27
+ radius: ThemeRadiusId;
28
+ density: ThemeDensityId;
29
+ font: ThemeFontId;
30
+ defaultMode: ThemePolarity;
31
+ /** Optional custom hex overrides for semantic telemetry colors. */
32
+ semantic?: ThemeSemanticOverrides;
33
+ }
34
+
35
+ export interface NamedThemePreset {
36
+ id: string;
37
+ label: string;
38
+ description: string;
39
+ config: ThemePresetConfig;
40
+ }
41
+
42
+ interface SurfacePalette {
43
+ canvas: string;
44
+ surface1: string;
45
+ surface2: string;
46
+ surface3: string;
47
+ surface4: string;
48
+ hairline: string;
49
+ hairlineStrong: string;
50
+ hairlineTertiary: string;
51
+ ink: string;
52
+ inkMuted: string;
53
+ inkSubtle: string;
54
+ inkTertiary: string;
55
+ field: string;
56
+ }
57
+
58
+ export interface BaseSurfaceSpec {
59
+ id: ThemeBaseId;
60
+ label: string;
61
+ dark: SurfacePalette;
62
+ light: SurfacePalette;
63
+ }
64
+
65
+ export interface AccentSpec {
66
+ id: ThemeAccentId;
67
+ label: string;
68
+ swatch: string;
69
+ dark: {
70
+ primary: string;
71
+ primaryForeground: string;
72
+ primaryHover: string;
73
+ primaryFocus: string;
74
+ ring: string;
75
+ };
76
+ light: {
77
+ primary: string;
78
+ primaryForeground: string;
79
+ primaryHover: string;
80
+ primaryFocus: string;
81
+ ring: string;
82
+ };
83
+ }
84
+
85
+ export interface RadiusSpec {
86
+ id: ThemeRadiusId;
87
+ label: string;
88
+ basePx: number;
89
+ scale: {
90
+ xs: string;
91
+ sm: string;
92
+ md: string;
93
+ lg: string;
94
+ xl: string;
95
+ xxl: string;
96
+ };
97
+ }
98
+
99
+ export interface DensitySpec {
100
+ id: ThemeDensityId;
101
+ label: string;
102
+ percent: number;
103
+ spacing: string;
104
+ }
105
+
106
+ export interface FontSpec {
107
+ id: ThemeFontId;
108
+ label: string;
109
+ stack: string;
110
+ }
111
+
112
+ export const DEFAULT_SEMANTIC_COLORS: Record<
113
+ ThemePolarity,
114
+ Record<ThemeSemanticKey, string>
115
+ > = {
116
+ light: {
117
+ success: "#1e8e3e",
118
+ warning: "#d97706",
119
+ danger: "#b91c1c",
120
+ info: "#027a9e",
121
+ },
122
+ dark: {
123
+ success: "#1e8e3e",
124
+ warning: "#d97706",
125
+ danger: "#dc2626",
126
+ info: "#027a9e",
127
+ },
128
+ };
129
+
130
+ export const DEFAULT_THEME_CONFIG: ThemePresetConfig = {
131
+ base: "graphite",
132
+ accent: "signal-orange",
133
+ radius: "md",
134
+ density: "default",
135
+ font: "inter",
136
+ defaultMode: "light",
137
+ };
138
+
139
+ export const THEME_BASES: Record<ThemeBaseId, BaseSurfaceSpec> = {
140
+ graphite: {
141
+ id: "graphite",
142
+ label: "Graphite",
143
+ dark: {
144
+ canvas: "#0e0e10",
145
+ surface1: "#161618",
146
+ surface2: "#1e1e21",
147
+ surface3: "#26262a",
148
+ surface4: "#2b2b30",
149
+ hairline: "#28282c",
150
+ hairlineStrong: "#3a3a40",
151
+ hairlineTertiary: "#4e4e56",
152
+ ink: "#f2f1ed",
153
+ inkMuted: "#cfcbc2",
154
+ inkSubtle: "#96948e",
155
+ inkTertiary: "#6e6c66",
156
+ field: "rgb(255 255 255 / 0.03)",
157
+ },
158
+ light: {
159
+ canvas: "#f4f3ef",
160
+ surface1: "#ffffff",
161
+ surface2: "#eae8e2",
162
+ surface3: "#e1dfd8",
163
+ surface4: "#ffffff",
164
+ hairline: "#d8d5cd",
165
+ hairlineStrong: "#bfbbb1",
166
+ hairlineTertiary: "#a39f94",
167
+ ink: "#121213",
168
+ inkMuted: "#343330",
169
+ inkSubtle: "#52514e",
170
+ inkTertiary: "#6e6c67",
171
+ field: "rgb(18 18 19 / 0.03)",
172
+ },
173
+ },
174
+ linear: {
175
+ id: "linear",
176
+ label: "Linear",
177
+ dark: {
178
+ canvas: "#010102",
179
+ surface1: "#0f1011",
180
+ surface2: "#141516",
181
+ surface3: "#18191a",
182
+ surface4: "#191a1b",
183
+ hairline: "#23252a",
184
+ hairlineStrong: "#34343a",
185
+ hairlineTertiary: "#3e3e44",
186
+ ink: "#f7f8f8",
187
+ inkMuted: "#d0d6e0",
188
+ inkSubtle: "#8a8f98",
189
+ inkTertiary: "#62666d",
190
+ field: "rgb(255 255 255 / 0.025)",
191
+ },
192
+ light: {
193
+ canvas: "#ffffff",
194
+ surface1: "#ffffff",
195
+ surface2: "#f5f6f6",
196
+ surface3: "#eceef0",
197
+ surface4: "#f6f7f7",
198
+ hairline: "#e4e5e7",
199
+ hairlineStrong: "#d2d4d8",
200
+ hairlineTertiary: "#c2c5cb",
201
+ ink: "#010102",
202
+ inkMuted: "#26282e",
203
+ inkSubtle: "#5c6068",
204
+ inkTertiary: "#62666d",
205
+ field: "rgb(1 1 2 / 0.025)",
206
+ },
207
+ },
208
+ zinc: {
209
+ id: "zinc",
210
+ label: "Zinc",
211
+ dark: {
212
+ canvas: "#09090b",
213
+ surface1: "#111114",
214
+ surface2: "#18181b",
215
+ surface3: "#202024",
216
+ surface4: "#27272a",
217
+ hairline: "#27272a",
218
+ hairlineStrong: "#3f3f46",
219
+ hairlineTertiary: "#52525b",
220
+ ink: "#fafafa",
221
+ inkMuted: "#d4d4d8",
222
+ inkSubtle: "#a1a1aa",
223
+ inkTertiary: "#71717a",
224
+ field: "rgb(255 255 255 / 0.03)",
225
+ },
226
+ light: {
227
+ canvas: "#ffffff",
228
+ surface1: "#ffffff",
229
+ surface2: "#f4f4f5",
230
+ surface3: "#e4e4e7",
231
+ surface4: "#f4f4f5",
232
+ hairline: "#e4e4e7",
233
+ hairlineStrong: "#d4d4d8",
234
+ hairlineTertiary: "#a1a1aa",
235
+ ink: "#09090b",
236
+ inkMuted: "#27272a",
237
+ inkSubtle: "#52525b",
238
+ inkTertiary: "#71717a",
239
+ field: "rgb(9 9 11 / 0.025)",
240
+ },
241
+ },
242
+ slate: {
243
+ id: "slate",
244
+ label: "Slate",
245
+ dark: {
246
+ canvas: "#080d17",
247
+ surface1: "#0f172a",
248
+ surface2: "#152036",
249
+ surface3: "#1e293b",
250
+ surface4: "#253349",
251
+ hairline: "#243247",
252
+ hairlineStrong: "#334155",
253
+ hairlineTertiary: "#475569",
254
+ ink: "#f8fafc",
255
+ inkMuted: "#cbd5e1",
256
+ inkSubtle: "#94a3b8",
257
+ inkTertiary: "#64748b",
258
+ field: "rgb(255 255 255 / 0.03)",
259
+ },
260
+ light: {
261
+ canvas: "#fafbfc",
262
+ surface1: "#ffffff",
263
+ surface2: "#f1f5f9",
264
+ surface3: "#e2e8f0",
265
+ surface4: "#f1f5f9",
266
+ hairline: "#dce3ec",
267
+ hairlineStrong: "#cbd5e1",
268
+ hairlineTertiary: "#94a3b8",
269
+ ink: "#0b1220",
270
+ inkMuted: "#1e293b",
271
+ inkSubtle: "#475569",
272
+ inkTertiary: "#64748b",
273
+ field: "rgb(15 23 42 / 0.025)",
274
+ },
275
+ },
276
+ stone: {
277
+ id: "stone",
278
+ label: "Stone",
279
+ dark: {
280
+ canvas: "#0c0a09",
281
+ surface1: "#161311",
282
+ surface2: "#1c1917",
283
+ surface3: "#26221f",
284
+ surface4: "#2e2925",
285
+ hairline: "#2e2925",
286
+ hairlineStrong: "#44403c",
287
+ hairlineTertiary: "#57534e",
288
+ ink: "#fafaf9",
289
+ inkMuted: "#d6d3d1",
290
+ inkSubtle: "#a8a29e",
291
+ inkTertiary: "#78716c",
292
+ field: "rgb(255 255 255 / 0.03)",
293
+ },
294
+ light: {
295
+ canvas: "#fafaf9",
296
+ surface1: "#ffffff",
297
+ surface2: "#f5f5f4",
298
+ surface3: "#e7e5e4",
299
+ surface4: "#f5f5f4",
300
+ hairline: "#e7e5e4",
301
+ hairlineStrong: "#d6d3d1",
302
+ hairlineTertiary: "#a8a29e",
303
+ ink: "#0c0a09",
304
+ inkMuted: "#292524",
305
+ inkSubtle: "#57534e",
306
+ inkTertiary: "#78716c",
307
+ field: "rgb(12 10 9 / 0.025)",
308
+ },
309
+ },
310
+ };
311
+
312
+ export const THEME_ACCENTS: Record<ThemeAccentId, AccentSpec> = {
313
+ "signal-orange": {
314
+ id: "signal-orange",
315
+ label: "Signal Orange",
316
+ swatch: "#f2541b",
317
+ dark: {
318
+ primary: "#f2541b",
319
+ primaryForeground: "#0e0e10",
320
+ primaryHover: "#ff6b35",
321
+ primaryFocus: "#f2541b",
322
+ ring: "#f2541b",
323
+ },
324
+ light: {
325
+ primary: "#c83e0b",
326
+ primaryForeground: "#ffffff",
327
+ primaryHover: "#b03508",
328
+ primaryFocus: "#ea4b14",
329
+ ring: "#ea4b14",
330
+ },
331
+ },
332
+ lavender: {
333
+ id: "lavender",
334
+ label: "Lavender",
335
+ swatch: "#5e6ad2",
336
+ dark: {
337
+ primary: "#5e6ad2",
338
+ primaryForeground: "#ffffff",
339
+ primaryHover: "#828fff",
340
+ primaryFocus: "#5e69d1",
341
+ ring: "#5e69d1",
342
+ },
343
+ light: {
344
+ primary: "#5e6ad2",
345
+ primaryForeground: "#ffffff",
346
+ primaryHover: "#4f5bc4",
347
+ primaryFocus: "#5e69d1",
348
+ ring: "#5e69d1",
349
+ },
350
+ },
351
+ cobalt: {
352
+ id: "cobalt",
353
+ label: "Cobalt",
354
+ swatch: "#2563eb",
355
+ dark: {
356
+ primary: "#2563eb",
357
+ primaryForeground: "#ffffff",
358
+ primaryHover: "#3b82f6",
359
+ primaryFocus: "#1d4ed8",
360
+ ring: "#60a5fa",
361
+ },
362
+ light: {
363
+ primary: "#1d4ed8",
364
+ primaryForeground: "#ffffff",
365
+ primaryHover: "#1e40af",
366
+ primaryFocus: "#1d4ed8",
367
+ ring: "#1d4ed8",
368
+ },
369
+ },
370
+ emerald: {
371
+ id: "emerald",
372
+ label: "Emerald",
373
+ swatch: "#15803d",
374
+ dark: {
375
+ primary: "#15803d",
376
+ primaryForeground: "#ffffff",
377
+ primaryHover: "#16a34a",
378
+ primaryFocus: "#166534",
379
+ ring: "#4ade80",
380
+ },
381
+ light: {
382
+ primary: "#15803d",
383
+ primaryForeground: "#ffffff",
384
+ primaryHover: "#166534",
385
+ primaryFocus: "#15803d",
386
+ ring: "#15803d",
387
+ },
388
+ },
389
+ amber: {
390
+ id: "amber",
391
+ label: "Amber",
392
+ swatch: "#f59e0b",
393
+ dark: {
394
+ primary: "#f59e0b",
395
+ primaryForeground: "#010102",
396
+ primaryHover: "#fbbf24",
397
+ primaryFocus: "#d97706",
398
+ ring: "#fbbf24",
399
+ },
400
+ light: {
401
+ primary: "#b45309",
402
+ primaryForeground: "#ffffff",
403
+ primaryHover: "#92400e",
404
+ primaryFocus: "#b45309",
405
+ ring: "#b45309",
406
+ },
407
+ },
408
+ crimson: {
409
+ id: "crimson",
410
+ label: "Crimson",
411
+ swatch: "#dc2626",
412
+ dark: {
413
+ primary: "#dc2626",
414
+ primaryForeground: "#ffffff",
415
+ primaryHover: "#ef4444",
416
+ primaryFocus: "#b91c1c",
417
+ ring: "#f87171",
418
+ },
419
+ light: {
420
+ primary: "#b91c1c",
421
+ primaryForeground: "#ffffff",
422
+ primaryHover: "#991b1b",
423
+ primaryFocus: "#b91c1c",
424
+ ring: "#b91c1c",
425
+ },
426
+ },
427
+ monochrome: {
428
+ id: "monochrome",
429
+ label: "Monochrome",
430
+ swatch: "#f7f8f8",
431
+ dark: {
432
+ primary: "#f7f8f8",
433
+ primaryForeground: "#010102",
434
+ primaryHover: "#d0d6e0",
435
+ primaryFocus: "#ffffff",
436
+ ring: "#d0d6e0",
437
+ },
438
+ light: {
439
+ primary: "#111113",
440
+ primaryForeground: "#ffffff",
441
+ primaryHover: "#27272a",
442
+ primaryFocus: "#010102",
443
+ ring: "#27272a",
444
+ },
445
+ },
446
+ };
447
+
448
+ export const THEME_RADII: Record<ThemeRadiusId, RadiusSpec> = {
449
+ none: {
450
+ id: "none",
451
+ label: "Sharp (0px)",
452
+ basePx: 0,
453
+ scale: { xs: "0px", sm: "0px", md: "0px", lg: "0px", xl: "0px", xxl: "0px" },
454
+ },
455
+ sm: {
456
+ id: "sm",
457
+ label: "Crisp (4px)",
458
+ basePx: 4,
459
+ scale: { xs: "2px", sm: "3px", md: "4px", lg: "6px", xl: "8px", xxl: "12px" },
460
+ },
461
+ md: {
462
+ id: "md",
463
+ label: "Machined (6px)",
464
+ basePx: 6,
465
+ scale: { xs: "3px", sm: "4.5px", md: "6px", lg: "9px", xl: "12px", xxl: "18px" },
466
+ },
467
+ lg: {
468
+ id: "lg",
469
+ label: "Soft (12px)",
470
+ basePx: 12,
471
+ scale: { xs: "6px", sm: "8px", md: "12px", lg: "16px", xl: "20px", xxl: "28px" },
472
+ },
473
+ xl: {
474
+ id: "xl",
475
+ label: "Rounded (16px)",
476
+ basePx: 16,
477
+ scale: { xs: "8px", sm: "10px", md: "16px", lg: "20px", xl: "24px", xxl: "32px" },
478
+ },
479
+ };
480
+
481
+ export const THEME_DENSITIES: Record<ThemeDensityId, DensitySpec> = {
482
+ compact: {
483
+ id: "compact",
484
+ label: "Compact (90%)",
485
+ percent: 90,
486
+ spacing: "0.225rem",
487
+ },
488
+ default: {
489
+ id: "default",
490
+ label: "Default (100%)",
491
+ percent: 100,
492
+ spacing: "0.25rem",
493
+ },
494
+ comfortable: {
495
+ id: "comfortable",
496
+ label: "Spacious (110%)",
497
+ percent: 110,
498
+ spacing: "0.275rem",
499
+ },
500
+ };
501
+
502
+ export const THEME_FONTS: Record<ThemeFontId, FontSpec> = {
503
+ inter: {
504
+ id: "inter",
505
+ label: "Inter Variable",
506
+ stack:
507
+ '"Inter Variable", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
508
+ },
509
+ system: {
510
+ id: "system",
511
+ label: "System UI",
512
+ stack:
513
+ 'ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif',
514
+ },
515
+ geist: {
516
+ id: "geist",
517
+ label: "Geist Sans",
518
+ stack:
519
+ '"Geist", "Inter Variable", ui-sans-serif, system-ui, -apple-system, sans-serif',
520
+ },
521
+ jakarta: {
522
+ id: "jakarta",
523
+ label: "Plus Jakarta Sans",
524
+ stack:
525
+ '"Plus Jakarta Sans", "Inter Variable", ui-sans-serif, system-ui, -apple-system, sans-serif',
526
+ },
527
+ };
528
+
529
+ export const NAMED_THEME_PRESETS: NamedThemePreset[] = [
530
+ {
531
+ id: "graphite",
532
+ label: "Graphite",
533
+ description:
534
+ "Dual-polarity anodized chalk light and matte graphite dark with machined 6px corners and Signal Orange.",
535
+ config: { ...DEFAULT_THEME_CONFIG },
536
+ },
537
+ {
538
+ id: "linear",
539
+ label: "Linear Craft",
540
+ description:
541
+ "Preserved Linear near-black canvas, four-step charcoal ladder, and lavender-blue accent.",
542
+ config: {
543
+ base: "linear",
544
+ accent: "lavender",
545
+ radius: "lg",
546
+ density: "default",
547
+ font: "inter",
548
+ defaultMode: "dark",
549
+ },
550
+ },
551
+ {
552
+ id: "minimal-light",
553
+ label: "Minimal Light",
554
+ description: "High-contrast monochrome gallery canvas with crisp 4px corners.",
555
+ config: {
556
+ base: "zinc",
557
+ accent: "monochrome",
558
+ radius: "sm",
559
+ density: "default",
560
+ font: "inter",
561
+ defaultMode: "light",
562
+ },
563
+ },
564
+ {
565
+ id: "console-dense",
566
+ label: "Console Dense",
567
+ description: "Compact 90% spacing for data-heavy dashboards and audit workflows.",
568
+ config: {
569
+ base: "slate",
570
+ accent: "cobalt",
571
+ radius: "sm",
572
+ density: "compact",
573
+ font: "inter",
574
+ defaultMode: "dark",
575
+ },
576
+ },
577
+ {
578
+ id: "editorial-stone",
579
+ label: "Editorial Stone",
580
+ description: "Warm stone neutrals, emerald accent, and spacious 110% rhythm.",
581
+ config: {
582
+ base: "stone",
583
+ accent: "emerald",
584
+ radius: "lg",
585
+ density: "comfortable",
586
+ font: "jakarta",
587
+ defaultMode: "dark",
588
+ },
589
+ },
590
+ ];
591
+
592
+ const HEX6_RE = /^#([0-9a-fA-F]{6})$/;
593
+
594
+ export function isValidHexColor(value: string): boolean {
595
+ return HEX6_RE.test(value.trim());
596
+ }
597
+
598
+ function hexToRgbTuple(hex: string): [number, number, number] {
599
+ const clean = hex.trim().replace(/^#/, "");
600
+ return [
601
+ parseInt(clean.slice(0, 2), 16),
602
+ parseInt(clean.slice(2, 4), 16),
603
+ parseInt(clean.slice(4, 6), 16),
604
+ ];
605
+ }
606
+
607
+ function rgbTupleToHex(r: number, g: number, b: number): string {
608
+ const clamp = (n: number) => Math.min(255, Math.max(0, Math.round(n)));
609
+ return (
610
+ "#" +
611
+ [clamp(r), clamp(g), clamp(b)]
612
+ .map((n) => n.toString(16).padStart(2, "0"))
613
+ .join("")
614
+ );
615
+ }
616
+
617
+ function relativeLuminance(hex: string): number {
618
+ const [r, g, b] = hexToRgbTuple(hex).map((channel) => {
619
+ const s = channel / 255;
620
+ return s <= 0.03928 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
621
+ });
622
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
623
+ }
624
+
625
+ export function contrastRatioHex(hexA: string, hexB: string): number {
626
+ const l1 = relativeLuminance(hexA);
627
+ const l2 = relativeLuminance(hexB);
628
+ const lighter = Math.max(l1, l2);
629
+ const darker = Math.min(l1, l2);
630
+ return (lighter + 0.05) / (darker + 0.05);
631
+ }
632
+
633
+ function shiftHex(hex: string, targetHex: string, amount: number): string {
634
+ const [r1, g1, b1] = hexToRgbTuple(hex);
635
+ const [r2, g2, b2] = hexToRgbTuple(targetHex);
636
+ return rgbTupleToHex(
637
+ r1 + (r2 - r1) * amount,
638
+ g1 + (g2 - g1) * amount,
639
+ b1 + (b2 - b1) * amount
640
+ );
641
+ }
642
+
643
+ export function resolveAccentTokens(
644
+ accent: ThemeAccentId | string,
645
+ mode: ThemePolarity
646
+ ): {
647
+ primary: string;
648
+ primaryForeground: string;
649
+ primaryHover: string;
650
+ primaryFocus: string;
651
+ ring: string;
652
+ contrast: number;
653
+ } {
654
+ if (accent in THEME_ACCENTS) {
655
+ const spec = THEME_ACCENTS[accent as ThemeAccentId][mode];
656
+ return {
657
+ ...spec,
658
+ contrast: contrastRatioHex(spec.primaryForeground, spec.primary),
659
+ };
660
+ }
661
+
662
+ const normalized = isValidHexColor(accent) ? accent.trim().toLowerCase() : "#c83e0b";
663
+ const whiteRatio = contrastRatioHex("#ffffff", normalized);
664
+ const darkRatio = contrastRatioHex("#0e0e10", normalized);
665
+ const primaryForeground = whiteRatio >= darkRatio ? "#ffffff" : "#0e0e10";
666
+ const contrast = Math.max(whiteRatio, darkRatio);
667
+
668
+ return {
669
+ primary: normalized,
670
+ primaryForeground,
671
+ primaryHover:
672
+ mode === "dark"
673
+ ? shiftHex(normalized, "#ffffff", 0.14)
674
+ : shiftHex(normalized, "#010102", 0.14),
675
+ primaryFocus: shiftHex(normalized, "#010102", 0.08),
676
+ ring: normalized,
677
+ contrast,
678
+ };
679
+ }
680
+
681
+ function calibrateSemanticInk(hueHex: string, canvasHex: string, mode: ThemePolarity): string {
682
+ const target = mode === "dark" ? "#ffffff" : "#0e0e10";
683
+ let candidate = hueHex;
684
+ for (let step = 0; step <= 10; step += 1) {
685
+ const t = step * 0.08;
686
+ candidate = shiftHex(hueHex, target, t);
687
+ if (contrastRatioHex(candidate, canvasHex) >= 4.7) {
688
+ return candidate;
689
+ }
690
+ }
691
+ return target;
692
+ }
693
+
694
+ export function resolveSemanticTokens(
695
+ overrides: ThemeSemanticOverrides | undefined,
696
+ mode: ThemePolarity,
697
+ canvasHex: string
698
+ ): {
699
+ success: string;
700
+ successForeground: string;
701
+ warning: string;
702
+ warningForeground: string;
703
+ danger: string;
704
+ dangerForeground: string;
705
+ dangerOnSolid: string;
706
+ info: string;
707
+ infoForeground: string;
708
+ } {
709
+ const defaults = DEFAULT_SEMANTIC_COLORS[mode];
710
+ const pick = (key: ThemeSemanticKey) => {
711
+ const raw = overrides?.[key];
712
+ return raw && isValidHexColor(raw) ? raw.trim().toLowerCase() : defaults[key];
713
+ };
714
+
715
+ const success = pick("success");
716
+ const warning = pick("warning");
717
+ const danger = pick("danger");
718
+ const info = pick("info");
719
+
720
+ const whiteOnDanger = contrastRatioHex("#ffffff", danger);
721
+ const darkOnDanger = contrastRatioHex("#0e0e10", danger);
722
+
723
+ return {
724
+ success,
725
+ successForeground: calibrateSemanticInk(success, canvasHex, mode),
726
+ warning,
727
+ warningForeground: calibrateSemanticInk(warning, canvasHex, mode),
728
+ danger,
729
+ dangerForeground: calibrateSemanticInk(danger, canvasHex, mode),
730
+ dangerOnSolid: whiteOnDanger >= darkOnDanger ? "#ffffff" : "#0e0e10",
731
+ info,
732
+ infoForeground: calibrateSemanticInk(info, canvasHex, mode),
733
+ };
734
+ }
735
+
736
+ function hasCustomSemantics(semantic?: ThemeSemanticOverrides): boolean {
737
+ if (!semantic) return false;
738
+ return Boolean(
739
+ (semantic.success && isValidHexColor(semantic.success)) ||
740
+ (semantic.warning && isValidHexColor(semantic.warning)) ||
741
+ (semantic.danger && isValidHexColor(semantic.danger)) ||
742
+ (semantic.info && isValidHexColor(semantic.info))
743
+ );
744
+ }
745
+
746
+ export function buildScopedThemeVars(
747
+ config: ThemePresetConfig,
748
+ activeMode: ThemePolarity
749
+ ): Record<string, string> {
750
+ const baseSpec = THEME_BASES[config.base] ?? THEME_BASES.graphite;
751
+ const surface = baseSpec[activeMode];
752
+ const accent = resolveAccentTokens(config.accent, activeMode);
753
+ const semantic = resolveSemanticTokens(config.semantic, activeMode, surface.canvas);
754
+ const radius = THEME_RADII[config.radius] ?? THEME_RADII.md;
755
+ const density = THEME_DENSITIES[config.density] ?? THEME_DENSITIES.default;
756
+ const font = THEME_FONTS[config.font] ?? THEME_FONTS.inter;
757
+
758
+ return {
759
+ "--spacing": density.spacing,
760
+ "--font-sans": font.stack,
761
+ "--radius": `${radius.basePx}px`,
762
+ "--radius-xs": radius.scale.xs,
763
+ "--radius-sm": radius.scale.sm,
764
+ "--radius-md": radius.scale.md,
765
+ "--radius-lg": radius.scale.lg,
766
+ "--radius-xl": radius.scale.xl,
767
+ "--radius-xxl": radius.scale.xxl,
768
+
769
+ "--color-background": surface.canvas,
770
+ "--color-foreground": surface.ink,
771
+ "--color-card": surface.surface1,
772
+ "--color-card-foreground": surface.ink,
773
+ "--color-popover": activeMode === "dark" ? surface.surface3 : surface.surface1,
774
+ "--color-popover-foreground": surface.ink,
775
+ "--color-secondary": surface.surface2,
776
+ "--color-secondary-foreground":
777
+ activeMode === "dark" ? surface.inkMuted : surface.ink,
778
+ "--color-muted": surface.surface2,
779
+ "--color-muted-foreground": surface.inkSubtle,
780
+ "--color-accent": surface.surface3,
781
+ "--color-accent-foreground": surface.ink,
782
+ "--color-border": surface.hairline,
783
+ "--color-input": surface.hairlineStrong,
784
+ "--color-hairline": surface.hairline,
785
+ "--color-hairline-strong": surface.hairlineStrong,
786
+ "--color-hairline-tertiary": surface.hairlineTertiary,
787
+ "--color-fog": surface.inkSubtle,
788
+ "--color-field": surface.field,
789
+
790
+ "--color-surface-base": surface.canvas,
791
+ "--color-surface-raised": surface.surface1,
792
+ "--color-surface-overlay":
793
+ activeMode === "dark" ? surface.surface3 : surface.surface1,
794
+ "--color-surface-inset": surface.surface2,
795
+
796
+ "--color-sidebar": activeMode === "dark" ? surface.surface1 : surface.surface2,
797
+ "--color-sidebar-foreground": surface.ink,
798
+ "--color-sidebar-border": surface.hairline,
799
+ "--color-sidebar-ring": accent.ring,
800
+ "--color-sidebar-accent": surface.surface3,
801
+ "--color-sidebar-accent-foreground": surface.ink,
802
+
803
+ "--color-primary": accent.primary,
804
+ "--color-primary-foreground": accent.primaryForeground,
805
+ "--color-primary-hover": accent.primaryHover,
806
+ "--color-primary-focus": accent.primaryFocus,
807
+ "--color-ring": accent.ring,
808
+
809
+ "--color-pulse-green": semantic.success,
810
+ "--color-semantic-success": semantic.success,
811
+ "--color-pulse-green-foreground": semantic.successForeground,
812
+ "--color-amber-signal": semantic.warning,
813
+ "--color-amber-signal-foreground": semantic.warningForeground,
814
+ "--color-coral-red": semantic.danger,
815
+ "--color-coral-red-foreground": semantic.dangerForeground,
816
+ "--color-destructive": semantic.danger,
817
+ "--color-destructive-foreground": semantic.dangerOnSolid,
818
+ "--color-signal-teal": semantic.info,
819
+ "--color-signal-teal-foreground": semantic.infoForeground,
820
+ };
821
+ }
822
+
823
+ export function isDefaultThemeConfig(config: ThemePresetConfig): boolean {
824
+ return (
825
+ config.base === DEFAULT_THEME_CONFIG.base &&
826
+ config.accent === DEFAULT_THEME_CONFIG.accent &&
827
+ config.radius === DEFAULT_THEME_CONFIG.radius &&
828
+ config.density === DEFAULT_THEME_CONFIG.density &&
829
+ config.font === DEFAULT_THEME_CONFIG.font &&
830
+ config.defaultMode === DEFAULT_THEME_CONFIG.defaultMode &&
831
+ !hasCustomSemantics(config.semantic)
832
+ );
833
+ }
834
+
835
+ export function encodeThemePreset(config: ThemePresetConfig): string {
836
+ const customSem = hasCustomSemantics(config.semantic);
837
+ if (!customSem) {
838
+ const matched = NAMED_THEME_PRESETS.find(
839
+ (preset) =>
840
+ preset.config.base === config.base &&
841
+ preset.config.accent === config.accent &&
842
+ preset.config.radius === config.radius &&
843
+ preset.config.density === config.density &&
844
+ preset.config.font === config.font &&
845
+ preset.config.defaultMode === config.defaultMode
846
+ );
847
+ if (matched) return matched.id;
848
+ }
849
+
850
+ const params = new URLSearchParams();
851
+ if (config.base !== DEFAULT_THEME_CONFIG.base) params.set("base", config.base);
852
+ if (config.accent !== DEFAULT_THEME_CONFIG.accent) params.set("accent", config.accent);
853
+ if (config.radius !== DEFAULT_THEME_CONFIG.radius) params.set("radius", config.radius);
854
+ if (config.density !== DEFAULT_THEME_CONFIG.density) params.set("density", config.density);
855
+ if (config.font !== DEFAULT_THEME_CONFIG.font) params.set("font", config.font);
856
+ if (config.defaultMode !== DEFAULT_THEME_CONFIG.defaultMode) {
857
+ params.set("mode", config.defaultMode);
858
+ }
859
+ if (config.semantic) {
860
+ for (const key of ["success", "warning", "danger", "info"] as const) {
861
+ const value = config.semantic[key];
862
+ if (value && isValidHexColor(value)) {
863
+ params.set(key, value.toLowerCase());
864
+ }
865
+ }
866
+ }
867
+ return params.toString() || "graphite";
868
+ }
869
+
870
+ export function decodeThemePreset(input: string): ThemePresetConfig {
871
+ const trimmed = input.trim();
872
+ if (!trimmed) return { ...DEFAULT_THEME_CONFIG };
873
+
874
+ const lower = trimmed.toLowerCase();
875
+ if (lower === "rams") return { ...DEFAULT_THEME_CONFIG };
876
+ if (lower === "rams-dark") {
877
+ return { ...DEFAULT_THEME_CONFIG, defaultMode: "dark" };
878
+ }
879
+
880
+ const named = NAMED_THEME_PRESETS.find(
881
+ (preset) => preset.id.toLowerCase() === lower
882
+ );
883
+ if (named) return { ...named.config };
884
+
885
+ let queryString = trimmed;
886
+ if (trimmed.includes("?")) {
887
+ queryString = trimmed.slice(trimmed.indexOf("?") + 1);
888
+ }
889
+ const params = new URLSearchParams(queryString);
890
+ const presetParam = params.get("preset")?.toLowerCase();
891
+ const baseFromPreset =
892
+ presetParam === "rams"
893
+ ? DEFAULT_THEME_CONFIG
894
+ : presetParam === "rams-dark"
895
+ ? { ...DEFAULT_THEME_CONFIG, defaultMode: "dark" as const }
896
+ : presetParam
897
+ ? NAMED_THEME_PRESETS.find((p) => p.id.toLowerCase() === presetParam)?.config
898
+ : undefined;
899
+ const resolved: ThemePresetConfig = {
900
+ ...(baseFromPreset ?? DEFAULT_THEME_CONFIG),
901
+ };
902
+
903
+ const base = params.get("base")?.toLowerCase();
904
+ if (base === "rams") {
905
+ resolved.base = "graphite";
906
+ } else if (base && base in THEME_BASES) {
907
+ resolved.base = base as ThemeBaseId;
908
+ }
909
+
910
+ const accent = params.get("accent");
911
+ if (accent) {
912
+ if (accent in THEME_ACCENTS) {
913
+ resolved.accent = accent as ThemeAccentId;
914
+ } else if (isValidHexColor(accent)) {
915
+ resolved.accent = accent.toLowerCase();
916
+ } else if (isValidHexColor(`#${accent}`)) {
917
+ resolved.accent = `#${accent.toLowerCase()}`;
918
+ }
919
+ }
920
+
921
+ const radius = params.get("radius");
922
+ if (radius && radius in THEME_RADII) {
923
+ resolved.radius = radius as ThemeRadiusId;
924
+ }
925
+
926
+ const density = params.get("density");
927
+ if (density && density in THEME_DENSITIES) {
928
+ resolved.density = density as ThemeDensityId;
929
+ }
930
+
931
+ const font = params.get("font");
932
+ if (font && font in THEME_FONTS) {
933
+ resolved.font = font as ThemeFontId;
934
+ }
935
+
936
+ const mode = params.get("mode");
937
+ if (mode === "dark" || mode === "light") {
938
+ resolved.defaultMode = mode;
939
+ }
940
+
941
+ const semantic: ThemeSemanticOverrides = {};
942
+ for (const key of ["success", "warning", "danger", "info"] as const) {
943
+ const val = params.get(key);
944
+ if (!val) continue;
945
+ if (isValidHexColor(val)) {
946
+ semantic[key] = val.toLowerCase();
947
+ } else if (isValidHexColor(`#${val}`)) {
948
+ semantic[key] = `#${val.toLowerCase()}`;
949
+ }
950
+ }
951
+ if (Object.keys(semantic).length > 0) {
952
+ resolved.semantic = semantic;
953
+ }
954
+
955
+ return resolved;
956
+ }
957
+
958
+ const PRESET_MARKER_START = "/* usalyt:theme-preset:start */";
959
+ const PRESET_MARKER_END = "/* usalyt:theme-preset:end */";
960
+
961
+ function formatCssDecls(vars: Record<string, string>, indent = " "): string {
962
+ return Object.entries(vars)
963
+ .map(([key, value]) => `${indent}${key}: ${value};`)
964
+ .join("\n");
965
+ }
966
+
967
+ /**
968
+ * Render a standalone CSS override block for the selected theme preset.
969
+ * Safe to paste into a consumer stylesheet or append to `usalyt-theme.css`.
970
+ */
971
+ export function renderThemeOverrideCss(config: ThemePresetConfig): string {
972
+ const darkVars = buildScopedThemeVars(config, "dark");
973
+ const lightVars = buildScopedThemeVars(config, "light");
974
+ const defaultIsLight = config.defaultMode === "light";
975
+
976
+ const rootSelector = defaultIsLight
977
+ ? ':root,\n html[data-theme="light"]:not(.dark)'
978
+ : ':root,\n .dark,\n html[data-theme="dark"]';
979
+ const altSelector = defaultIsLight
980
+ ? '.dark,\n html[data-theme="dark"]'
981
+ : 'html[data-theme="light"]:not(.dark)';
982
+
983
+ const rootVars = defaultIsLight ? lightVars : darkVars;
984
+ const altVars = defaultIsLight ? darkVars : lightVars;
985
+ const presetCode = encodeThemePreset(config);
986
+
987
+ return [
988
+ PRESET_MARKER_START,
989
+ `/* Usalyt Theme Preset: ${presetCode} */`,
990
+ "@layer base {",
991
+ ` ${rootSelector} {`,
992
+ formatCssDecls(rootVars),
993
+ " }",
994
+ "",
995
+ ` ${altSelector} {`,
996
+ formatCssDecls(altVars),
997
+ " }",
998
+ "}",
999
+ PRESET_MARKER_END,
1000
+ ].join("\n");
1001
+ }
1002
+
1003
+ /**
1004
+ * Combine the base `theme.css` from the registry with a calibrated preset block.
1005
+ * Idempotent: re-applying replaces any prior `usalyt:theme-preset` block.
1006
+ */
1007
+ export function applyThemePresetToCss(
1008
+ baseThemeCss: string,
1009
+ config: ThemePresetConfig
1010
+ ): string {
1011
+ const stripped = baseThemeCss
1012
+ .replace(
1013
+ /\n*\/\* usalyt:theme-preset:start \*\/[\s\S]*?\/\* usalyt:theme-preset:end \*\/\n*/g,
1014
+ "\n"
1015
+ )
1016
+ .trimEnd();
1017
+
1018
+ if (isDefaultThemeConfig(config)) {
1019
+ return `${stripped}\n`;
1020
+ }
1021
+
1022
+ return `${stripped}\n\n${renderThemeOverrideCss(config)}\n`;
1023
+ }