@und-ui/tokens 0.0.0-stage → 0.2.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.
Files changed (172) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +345 -2
  3. package/dist/android/values/und_colors.xml +91 -0
  4. package/dist/android/values/und_dimens.xml +93 -0
  5. package/dist/android/values-night/und_colors.xml +91 -0
  6. package/dist/css/base.css +2335 -0
  7. package/dist/css/motion.css +223 -0
  8. package/dist/css/themes/kesar.css +68 -0
  9. package/dist/css/themes/mitti.css +84 -0
  10. package/dist/css/themes/neem.css +84 -0
  11. package/dist/css/themes/raat.css +507 -0
  12. package/dist/css/themes/sutra.css +109 -0
  13. package/dist/css/themes/ujala.css +67 -0
  14. package/dist/css/tokens.css +3167 -0
  15. package/dist/dart/und_tokens.dart +3223 -0
  16. package/dist/js/contexts.d.ts +2 -0
  17. package/dist/js/contexts.js +3 -0
  18. package/dist/js/index.d.ts +1883 -0
  19. package/dist/js/index.js +2230 -0
  20. package/dist/json/flat/kesar.dark.json +737 -0
  21. package/dist/json/flat/kesar.darkMore.json +737 -0
  22. package/dist/json/flat/kesar.light.json +737 -0
  23. package/dist/json/flat/kesar.lightMore.json +737 -0
  24. package/dist/json/flat/mitti.dark.json +737 -0
  25. package/dist/json/flat/mitti.darkMore.json +737 -0
  26. package/dist/json/flat/mitti.light.json +737 -0
  27. package/dist/json/flat/mitti.lightMore.json +737 -0
  28. package/dist/json/flat/neem.dark.json +737 -0
  29. package/dist/json/flat/neem.darkMore.json +737 -0
  30. package/dist/json/flat/neem.light.json +737 -0
  31. package/dist/json/flat/neem.lightMore.json +737 -0
  32. package/dist/json/flat/raat.dark.json +737 -0
  33. package/dist/json/flat/raat.darkMore.json +737 -0
  34. package/dist/json/flat/sutra.dark.json +738 -0
  35. package/dist/json/flat/sutra.darkMore.json +738 -0
  36. package/dist/json/flat/sutra.light.json +738 -0
  37. package/dist/json/flat/sutra.lightMore.json +738 -0
  38. package/dist/json/flat/ujala.dark.json +737 -0
  39. package/dist/json/flat/ujala.darkMore.json +737 -0
  40. package/dist/json/flat/ujala.light.json +737 -0
  41. package/dist/json/flat/ujala.lightMore.json +737 -0
  42. package/dist/json/nested/kesar.dark.json +885 -0
  43. package/dist/json/nested/kesar.darkMore.json +885 -0
  44. package/dist/json/nested/kesar.light.json +885 -0
  45. package/dist/json/nested/kesar.lightMore.json +885 -0
  46. package/dist/json/nested/mitti.dark.json +885 -0
  47. package/dist/json/nested/mitti.darkMore.json +885 -0
  48. package/dist/json/nested/mitti.light.json +885 -0
  49. package/dist/json/nested/mitti.lightMore.json +885 -0
  50. package/dist/json/nested/neem.dark.json +885 -0
  51. package/dist/json/nested/neem.darkMore.json +885 -0
  52. package/dist/json/nested/neem.light.json +885 -0
  53. package/dist/json/nested/neem.lightMore.json +885 -0
  54. package/dist/json/nested/raat.dark.json +885 -0
  55. package/dist/json/nested/raat.darkMore.json +885 -0
  56. package/dist/json/nested/sutra.dark.json +886 -0
  57. package/dist/json/nested/sutra.darkMore.json +886 -0
  58. package/dist/json/nested/sutra.light.json +886 -0
  59. package/dist/json/nested/sutra.lightMore.json +886 -0
  60. package/dist/json/nested/ujala.dark.json +885 -0
  61. package/dist/json/nested/ujala.darkMore.json +885 -0
  62. package/dist/json/nested/ujala.light.json +885 -0
  63. package/dist/json/nested/ujala.lightMore.json +885 -0
  64. package/dist/kotlin/UndTokens.kt +2870 -0
  65. package/dist/lib/build/contexts.d.ts +13 -0
  66. package/dist/lib/build/contexts.js +20 -0
  67. package/dist/lib/build/css-values.d.ts +31 -0
  68. package/dist/lib/build/css-values.js +191 -0
  69. package/dist/lib/build/formats/css.d.ts +43 -0
  70. package/dist/lib/build/formats/css.js +380 -0
  71. package/dist/lib/build/formats/dart.d.ts +9 -0
  72. package/dist/lib/build/formats/dart.js +342 -0
  73. package/dist/lib/build/formats/js.d.ts +29 -0
  74. package/dist/lib/build/formats/js.js +297 -0
  75. package/dist/lib/build/formats/kotlin.d.ts +8 -0
  76. package/dist/lib/build/formats/kotlin.js +232 -0
  77. package/dist/lib/build/formats/react-native.d.ts +2 -0
  78. package/dist/lib/build/formats/react-native.js +208 -0
  79. package/dist/lib/build/formats/style-dictionary.d.ts +8 -0
  80. package/dist/lib/build/formats/style-dictionary.js +163 -0
  81. package/dist/lib/build/formats/swift.d.ts +8 -0
  82. package/dist/lib/build/formats/swift.js +395 -0
  83. package/dist/lib/build/formats/tailwind.d.ts +9 -0
  84. package/dist/lib/build/formats/tailwind.js +105 -0
  85. package/dist/lib/build/index.d.ts +12 -0
  86. package/dist/lib/build/index.js +133 -0
  87. package/dist/lib/build/materialize.d.ts +102 -0
  88. package/dist/lib/build/materialize.js +170 -0
  89. package/dist/lib/build/native-model.d.ts +46 -0
  90. package/dist/lib/build/native-model.js +177 -0
  91. package/dist/lib/color/contrast.d.ts +24 -0
  92. package/dist/lib/color/contrast.js +95 -0
  93. package/dist/lib/color/convert.d.ts +39 -0
  94. package/dist/lib/color/convert.js +91 -0
  95. package/dist/lib/color/pairs.d.ts +29 -0
  96. package/dist/lib/color/pairs.js +90 -0
  97. package/dist/lib/color/palette.d.ts +118 -0
  98. package/dist/lib/color/palette.js +199 -0
  99. package/dist/lib/color/scales.d.ts +18 -0
  100. package/dist/lib/color/scales.js +115 -0
  101. package/dist/lib/dtcg/engine.d.ts +75 -0
  102. package/dist/lib/dtcg/engine.js +395 -0
  103. package/dist/lib/dtcg/format-json.d.ts +1 -0
  104. package/dist/lib/dtcg/format-json.js +59 -0
  105. package/dist/lib/dtcg/types.d.ts +124 -0
  106. package/dist/lib/dtcg/types.js +54 -0
  107. package/dist/lib/index.d.ts +20 -0
  108. package/dist/lib/index.js +14 -0
  109. package/dist/lib/motion/spring.d.ts +40 -0
  110. package/dist/lib/motion/spring.js +129 -0
  111. package/dist/lib/source/generate.d.ts +28 -0
  112. package/dist/lib/source/generate.js +468 -0
  113. package/dist/lib/themes/definitions.d.ts +43 -0
  114. package/dist/lib/themes/definitions.js +140 -0
  115. package/dist/react-native/index.d.ts +130 -0
  116. package/dist/react-native/index.js +15 -0
  117. package/dist/react-native/tokens.d.ts +2 -0
  118. package/dist/react-native/tokens.js +4413 -0
  119. package/dist/scss/_variables.scss +738 -0
  120. package/dist/scss/_vars.scss +857 -0
  121. package/dist/scss/kesar.dark.scss +738 -0
  122. package/dist/scss/kesar.darkMore.scss +738 -0
  123. package/dist/scss/kesar.light.scss +738 -0
  124. package/dist/scss/kesar.lightMore.scss +738 -0
  125. package/dist/scss/mitti.dark.scss +738 -0
  126. package/dist/scss/mitti.darkMore.scss +738 -0
  127. package/dist/scss/mitti.light.scss +738 -0
  128. package/dist/scss/mitti.lightMore.scss +738 -0
  129. package/dist/scss/neem.dark.scss +738 -0
  130. package/dist/scss/neem.darkMore.scss +738 -0
  131. package/dist/scss/neem.light.scss +738 -0
  132. package/dist/scss/neem.lightMore.scss +738 -0
  133. package/dist/scss/raat.dark.scss +738 -0
  134. package/dist/scss/raat.darkMore.scss +738 -0
  135. package/dist/scss/sutra.dark.scss +739 -0
  136. package/dist/scss/sutra.darkMore.scss +739 -0
  137. package/dist/scss/sutra.light.scss +739 -0
  138. package/dist/scss/sutra.lightMore.scss +739 -0
  139. package/dist/scss/ujala.dark.scss +738 -0
  140. package/dist/scss/ujala.darkMore.scss +738 -0
  141. package/dist/scss/ujala.light.scss +738 -0
  142. package/dist/scss/ujala.lightMore.scss +738 -0
  143. package/dist/swift/UndTokens.swift +4117 -0
  144. package/dist/tailwind/und.css +586 -0
  145. package/package.json +84 -3
  146. package/tokens/contrast/more.tokens.json +46 -0
  147. package/tokens/density/comfortable.tokens.json +64 -0
  148. package/tokens/density/compact.tokens.json +68 -0
  149. package/tokens/density/touch.tokens.json +64 -0
  150. package/tokens/modes/dark.tokens.json +197 -0
  151. package/tokens/modes/light.tokens.json +166 -0
  152. package/tokens/motion/reduced.tokens.json +119 -0
  153. package/tokens/primitives/color.dark.tokens.json +2001 -0
  154. package/tokens/primitives/color.light.tokens.json +2077 -0
  155. package/tokens/primitives/dimension.tokens.json +181 -0
  156. package/tokens/primitives/motion.tokens.json +41 -0
  157. package/tokens/primitives/spring.tokens.json +138 -0
  158. package/tokens/primitives/typography.tokens.json +107 -0
  159. package/tokens/semantic/bridge.tokens.json +52 -0
  160. package/tokens/semantic/color.tokens.json +129 -0
  161. package/tokens/semantic/layout.tokens.json +84 -0
  162. package/tokens/semantic/motion.tokens.json +111 -0
  163. package/tokens/semantic/scale.tokens.json +66 -0
  164. package/tokens/semantic/typography.tokens.json +281 -0
  165. package/tokens/themes/kesar.tokens.json +105 -0
  166. package/tokens/themes/mitti.tokens.json +123 -0
  167. package/tokens/themes/neem.tokens.json +123 -0
  168. package/tokens/themes/raat.tokens.json +104 -0
  169. package/tokens/themes/sutra.tokens.json +126 -0
  170. package/tokens/themes/ujala.tokens.json +96 -0
  171. package/tokens/transparency/reduced.tokens.json +30 -0
  172. package/tokens/und.resolver.json +83 -0
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Colour-space helpers built on culori (MIT).
3
+ *
4
+ * All palette maths happens in OKLCH. Gamut fitting reduces chroma at constant
5
+ * lightness and hue (culori `clampChroma`, a binary search on chroma), first to
6
+ * Display P3 and then to sRGB, exactly as section 1.4 of the design-language
7
+ * report describes.
8
+ */
9
+ import { clampChroma, converter, formatHex, inGamut, parse } from 'culori';
10
+ const toOklch = converter('oklch');
11
+ const toRgb = converter('rgb');
12
+ const toP3 = converter('p3');
13
+ const inSrgb = inGamut('rgb');
14
+ const inP3 = inGamut('p3');
15
+ /** Rounds to a fixed number of decimals, avoiding `-0`. */
16
+ export function round(value, decimals) {
17
+ const f = 10 ** decimals;
18
+ const r = Math.round(value * f) / f;
19
+ return Object.is(r, -0) ? 0 : r;
20
+ }
21
+ function oklch({ l, c, h }) {
22
+ return { mode: 'oklch', l, c, h };
23
+ }
24
+ /** Largest chroma (<= c) that fits the gamut, at the same L and h. */
25
+ function fitChroma(color, gamut) {
26
+ const fits = gamut === 'rgb' ? inSrgb : inP3;
27
+ const candidate = oklch(color);
28
+ if (fits(candidate))
29
+ return color.c;
30
+ const clamped = clampChroma(candidate, 'oklch', gamut);
31
+ return clamped.c ?? 0;
32
+ }
33
+ /**
34
+ * Fit an OKLCH colour into Display P3 and then sRGB.
35
+ * Rounds L to 3 decimals, C to 3 decimals and h to 1 decimal *before*
36
+ * fitting, so the published token values are the exact inputs to conversion.
37
+ */
38
+ export function fitColor(input) {
39
+ const base = { l: round(input.l, 3), c: round(input.c, 3), h: round(input.h, 1) };
40
+ // Round the fitted chroma *down* so the rounded value is still in gamut.
41
+ const p3c = Math.floor(fitChroma(base, 'p3') * 1000) / 1000;
42
+ const p3 = { ...base, c: p3c };
43
+ const srgbC = Math.floor(fitChroma(p3, 'rgb') * 1000) / 1000;
44
+ const srgb = { ...base, c: srgbC };
45
+ const rgb = toRgb(oklch(srgb));
46
+ const p3rgb = toP3(oklch(p3));
47
+ return {
48
+ p3,
49
+ srgb,
50
+ hex: formatHex(clampRgb(rgb)),
51
+ p3Components: [
52
+ round(clamp01(p3rgb.r), 4),
53
+ round(clamp01(p3rgb.g), 4),
54
+ round(clamp01(p3rgb.b), 4),
55
+ ],
56
+ srgbComponents: [round(clamp01(rgb.r), 4), round(clamp01(rgb.g), 4), round(clamp01(rgb.b), 4)],
57
+ };
58
+ }
59
+ function clamp01(v) {
60
+ return Math.min(1, Math.max(0, v));
61
+ }
62
+ function clampRgb(c) {
63
+ return { mode: 'rgb', r: clamp01(c.r), g: clamp01(c.g), b: clamp01(c.b) };
64
+ }
65
+ /** Parse any CSS colour string into an OKLCH triple. */
66
+ export function parseOklch(css) {
67
+ const parsed = parse(css);
68
+ if (!parsed)
69
+ throw new Error(`Cannot parse colour "${css}"`);
70
+ const o = toOklch(parsed);
71
+ return { l: o.l, c: o.c, h: o.h ?? 0 };
72
+ }
73
+ /** sRGB hex (6-digit) to OKLCH. */
74
+ export function hexToOklch(hex) {
75
+ return parseOklch(hex);
76
+ }
77
+ /** OKLCH to sRGB hex, clipping out-of-gamut values. */
78
+ export function oklchToHex(color) {
79
+ return formatHex(clampRgb(toRgb(oklch(color))));
80
+ }
81
+ /** Hex string to sRGB components in 0..1. */
82
+ export function hexToRgb(hex) {
83
+ const p = parse(hex);
84
+ if (!p)
85
+ throw new Error(`Bad hex ${hex}`);
86
+ const c = toRgb(p);
87
+ return [c.r, c.g, c.b];
88
+ }
89
+ export function rgbToHex([r, g, b]) {
90
+ return formatHex({ mode: 'rgb', r: clamp01(r), g: clamp01(g), b: clamp01(b) });
91
+ }
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Documented foreground/background pairs (report sections 1.3, 1.7, 3.6, 6.1, 6.2)
3
+ * and the contrast matrix evaluated over every theme x mode x contrast context.
4
+ * WCAG 2.2 AA is blocking; APCA is advisory and reported only.
5
+ */
6
+ import type { Context } from '../dtcg/engine.js';
7
+ export type PairKind = 'text' | 'large' | 'ui';
8
+ export interface Pair {
9
+ fg: string;
10
+ bg: string;
11
+ kind: PairKind;
12
+ /** Composite a translucent bg over these backdrops (glass). */
13
+ backdrops?: string[];
14
+ note?: string;
15
+ }
16
+ export declare const PAIRS: readonly Pair[];
17
+ export interface PairResult extends Pair {
18
+ fgHex: string;
19
+ bgHex: string;
20
+ ratio: number;
21
+ apca: number;
22
+ required: number;
23
+ pass: boolean;
24
+ apcaTarget: number;
25
+ apcaPass: boolean;
26
+ }
27
+ export declare function evaluatePair(ctx: Context, pair: Pair): PairResult[];
28
+ export declare function evaluateContext(ctx: Context): PairResult[];
29
+ export declare function contextLabel(ctx: Context): string;
@@ -0,0 +1,90 @@
1
+ import { APCA_TARGETS, WCAG_AA, apcaContrast, compositeOver, wcagContrast } from './contrast.js';
2
+ const SURFACES = ['bg.canvas', 'bg.surface', 'bg.subtle'];
3
+ /** `color.neutral` is the neutral scale group, so its bridge token is `color.neutral.$root`. */
4
+ const bridgeTone = (tone) => tone === 'neutral' ? 'color.neutral.$root' : `color.${tone}`;
5
+ const STATUSES = ['success', 'warning', 'danger', 'info'];
6
+ function on(fg, bgs, kind, note) {
7
+ return bgs.map((bg) => ({ fg, bg, kind, ...(note ? { note } : {}) }));
8
+ }
9
+ export const PAIRS = [
10
+ // Text (1.4.3, 4.5:1)
11
+ ...on('fg.default', [...SURFACES, 'bg.raised', 'bg.muted', 'bg.muted-hover', 'bg.muted-active'], 'text'),
12
+ ...on('fg.muted', [...SURFACES, 'bg.raised', 'bg.muted'], 'text', 'secondary text'),
13
+ ...on('fg.on-accent', ['accent.solid', 'accent.solid-hover'], 'text', 'button label'),
14
+ ...on('fg.inverse', ['bg.inverse'], 'text', 'tooltip'),
15
+ // The on-solid scale alias must read on the theme's accent solid band, whatever a
16
+ // theme repoints (Sutra uses neutral-12 solids).
17
+ ...on('scale.accent.contrast', ['scale.accent.9', 'scale.accent.10', 'accent.solid', 'accent.solid-hover'], 'text', 'on-solid scale alias'),
18
+ ...on('accent.fg', [...SURFACES, 'bg.raised', 'accent.bg'], 'text', 'links, active nav, selected row'),
19
+ ...on('accent.fg-strong', ['accent.bg', 'accent.bg-hover', 'accent.bg-active'], 'text'),
20
+ ...on('link.default', [...SURFACES], 'text'),
21
+ ...on('link.visited', [...SURFACES], 'text'),
22
+ ...STATUSES.flatMap((s) => [
23
+ ...on(`status.${s}.fg`, [...SURFACES, `status.${s}.bg`], 'text'),
24
+ ...on(`status.${s}.fg-strong`, [`status.${s}.bg`, `status.${s}.bg-hover`], 'text'),
25
+ ...on(`status.${s}.on-solid`, [`status.${s}.solid`, `status.${s}.solid-hover`], 'text', 'badge/button label'),
26
+ ]),
27
+ ...['thin', 'regular', 'thick', 'chrome'].map((m) => ({
28
+ fg: 'fg.default',
29
+ bg: `material.${m}.bg`,
30
+ kind: 'text',
31
+ backdrops: ['#000000', '#ffffff'],
32
+ note: 'glass over worst-case backdrops',
33
+ })),
34
+ // v0 component bridge: the button tone is both a fill and text on the page.
35
+ ...['accent', 'neutral', 'success', 'warning', 'danger'].flatMap((tone) => [
36
+ ...on(`color.on-${tone}`, [bridgeTone(tone)], 'text', 'v0 button solid'),
37
+ ...on(bridgeTone(tone), [...SURFACES], 'text', 'v0 button outline/ghost/link text'),
38
+ ]),
39
+ // Large text only (3:1)
40
+ ...on('fg.subtle', ['bg.canvas', 'bg.surface'], 'large', 'placeholder/meta, large or non-essential only'),
41
+ // Non-text UI (1.4.11 and focus, 3:1)
42
+ ...on('border.control', [...SURFACES, 'bg.raised', 'bg.muted'], 'ui', 'input/checkbox boundary'),
43
+ ...on('focus.ring', [...SURFACES, 'bg.raised', 'bg.muted'], 'ui', 'focus indicator'),
44
+ ...on('status.danger.solid', ['bg.canvas', 'bg.surface'], 'ui', 'invalid input border'),
45
+ ...on('fg.on-accent', ['accent.solid'], 'ui', 'checkmark on checked box'),
46
+ ];
47
+ function color(ctx, path) {
48
+ const t = ctx.tokens.get(path);
49
+ if (!t)
50
+ throw new Error(`Pair token ${path} missing`);
51
+ if (t.type !== 'color')
52
+ throw new Error(`Pair token ${path} is ${t.type}, not color`);
53
+ return t.resolved;
54
+ }
55
+ export function evaluatePair(ctx, pair) {
56
+ const fg = color(ctx, pair.fg);
57
+ const bg = color(ctx, pair.bg);
58
+ const required = pair.kind === 'text' ? WCAG_AA.text : pair.kind === 'large' ? WCAG_AA.largeText : WCAG_AA.ui;
59
+ const apcaTarget = pair.kind === 'text'
60
+ ? APCA_TARGETS.secondary
61
+ : pair.kind === 'large'
62
+ ? APCA_TARGETS.large
63
+ : APCA_TARGETS.nonText;
64
+ const canvas = color(ctx, 'bg.canvas').hex;
65
+ const backdrops = (bg.alpha ?? 1) < 1 ? (pair.backdrops ?? [canvas]) : [null];
66
+ return backdrops.map((backdrop) => {
67
+ const bgHex = backdrop ? compositeOver(bg.hex, bg.alpha ?? 1, backdrop) : bg.hex;
68
+ const fgHex = (fg.alpha ?? 1) < 1 ? compositeOver(fg.hex, fg.alpha, bgHex) : fg.hex;
69
+ const ratio = wcagContrast(fgHex, bgHex);
70
+ const apca = Math.abs(apcaContrast(fgHex, bgHex));
71
+ return {
72
+ ...pair,
73
+ ...(backdrop ? { note: `${pair.note ?? ''} over ${backdrop}`.trim() } : {}),
74
+ fgHex,
75
+ bgHex,
76
+ ratio,
77
+ apca,
78
+ required,
79
+ pass: ratio >= required,
80
+ apcaTarget,
81
+ apcaPass: apca >= apcaTarget,
82
+ };
83
+ });
84
+ }
85
+ export function evaluateContext(ctx) {
86
+ return PAIRS.flatMap((p) => evaluatePair(ctx, p));
87
+ }
88
+ export function contextLabel(ctx) {
89
+ return `${ctx.input.theme}/${ctx.input.mode}${ctx.input.contrast === 'more' ? '/more' : ''}`;
90
+ }
@@ -0,0 +1,118 @@
1
+ /**
2
+ * OKLCH 12-step palette generator (port of design-language report section 1.4).
3
+ *
4
+ * Every scale comes from three inputs: hue, peak chroma (Cmax) and the step 9
5
+ * lightness. Steps follow the Radix role contract:
6
+ * 1-2 backgrounds, 3-5 component backgrounds, 6-8 borders (8 = strong),
7
+ * 9-10 solids, 11-12 text.
8
+ * Values are gamut-fitted to Display P3 (the `oklch()` value) and then to sRGB
9
+ * (the hex fallback) by reducing chroma at constant L and h.
10
+ */
11
+ import { compositeOver } from './contrast.js';
12
+ import { oklchToHex } from './convert.js';
13
+ import type { FittedColor } from './convert.js';
14
+ export type Mode = 'light' | 'dark';
15
+ export declare const MODES: readonly Mode[];
16
+ export declare const STEPS: readonly [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
17
+ export type Step = (typeof STEPS)[number];
18
+ /** Lightness targets per step (report section 1.4). `null` = derived from the seed. */
19
+ export declare const LIGHTNESS: Record<Mode, readonly (number | null)[]>;
20
+ /** Step 10 offset from step 9 (light: seed - 0.04, dark: seed + 0.045). */
21
+ export declare const STEP10_DELTA: Record<Mode, number>;
22
+ /** Chroma multipliers (x Cmax) for chromatic scales. */
23
+ export declare const CHROMA_FACTORS: Record<Mode, readonly number[]>;
24
+ /**
25
+ * Chroma multipliers for neutral scales, derived from the Slate Dusk table
26
+ * (C 0.002..0.016 for Cmax 0.016), identical in both modes.
27
+ */
28
+ export declare const NEUTRAL_CHROMA_FACTORS: readonly number[];
29
+ export interface ScaleSpec {
30
+ /** Token name of the scale (`color.<name>.<step>`). */
31
+ name: string;
32
+ /** Human name used in docs (for example "UND Indigo"). */
33
+ label: string;
34
+ kind: 'chromatic' | 'neutral';
35
+ hue: number;
36
+ /** Peak chroma. */
37
+ cmax: number;
38
+ /**
39
+ * Step 9 lightness. For chromatic scales this is the seed and is the same in
40
+ * both modes. For neutral scales it is solved per mode to give a 3:1
41
+ * control border against steps 1-3 (and white in light mode); a number here
42
+ * overrides the solver.
43
+ */
44
+ seedL?: number;
45
+ /** Optional per-mode lightness overrides (12 entries, `null` keeps the default). */
46
+ lightness?: Partial<Record<Mode, readonly (number | null)[]>>;
47
+ /** Optional chroma factor override (12 entries, both modes). */
48
+ chromaFactors?: readonly number[];
49
+ /** Exact OKLCH values for individual steps (for example Mitti's cream canvas). */
50
+ stepOverrides?: Partial<Record<Mode, Partial<Record<number, {
51
+ l: number;
52
+ c: number;
53
+ h: number;
54
+ }>>>>;
55
+ description?: string;
56
+ }
57
+ export interface AlphaColor {
58
+ /** Opaque sRGB colour (6-digit hex). */
59
+ hex: string;
60
+ components: [number, number, number];
61
+ alpha: number;
62
+ /** 8-digit hex (#rrggbbaa) for CSS. */
63
+ hex8: string;
64
+ }
65
+ export interface OnSolid {
66
+ kind: 'white' | 'dark';
67
+ hex: string;
68
+ fitted: FittedColor;
69
+ }
70
+ export interface GeneratedStep {
71
+ step: Step;
72
+ light: FittedColor;
73
+ dark: FittedColor;
74
+ }
75
+ export interface GeneratedScale {
76
+ spec: ScaleSpec;
77
+ steps: GeneratedStep[];
78
+ alpha: Record<Mode, AlphaColor[]>;
79
+ /** Text colour for step 9/10 solids. Same in both modes. */
80
+ onSolid: OnSolid;
81
+ /**
82
+ * The `color.<scale>.contrast` token per mode. Chromatic scales use `onSolid` in both
83
+ * modes (step 9 is shared). Neutral scales solve step 9 per mode, so the readable text
84
+ * differs: dark text on the light-mode step 9, white on the dark-mode step 9.
85
+ */
86
+ onSolidByMode: Record<Mode, OnSolid>;
87
+ /** Notes about solver decisions (for docs and the report). */
88
+ notes: string[];
89
+ }
90
+ /**
91
+ * Radix-style per-channel alpha solve: find the smallest alpha and an opaque
92
+ * colour F so that F at that alpha over `bg` reproduces `target` exactly
93
+ * (sRGB, gamma space, as browsers composite).
94
+ */
95
+ export declare function solveAlpha(targetHex: string, bgHex: string): AlphaColor;
96
+ /** Backdrops the alpha twins are solved against: white (light) and black (dark). */
97
+ export declare const ALPHA_BACKDROP: Record<Mode, string>;
98
+ export declare function generateScale(spec: ScaleSpec): GeneratedScale;
99
+ /** Convenience: the sRGB hex of a step in a mode. */
100
+ export declare function hexOf(scale: GeneratedScale, step: Step, mode: Mode): string;
101
+ export interface CommunityAccentResult {
102
+ scale: GeneratedScale;
103
+ /** Step 9 lightness after auto-correction. */
104
+ seedL: number;
105
+ adjusted: boolean;
106
+ }
107
+ /**
108
+ * Build an accent scale from a community-supplied brand colour (report section 7):
109
+ * auto-pick the on-solid colour and auto-correct step 9 lightness until the
110
+ * on-solid text reaches 4.5:1. Throws with an explanation if that is impossible.
111
+ */
112
+ export declare function createCommunityAccent(name: string, hex: string, parse: (hex: string) => {
113
+ l: number;
114
+ c: number;
115
+ h: number;
116
+ }): CommunityAccentResult;
117
+ /** Re-exported for tests and docs. */
118
+ export { compositeOver, oklchToHex };
@@ -0,0 +1,199 @@
1
+ /**
2
+ * OKLCH 12-step palette generator (port of design-language report section 1.4).
3
+ *
4
+ * Every scale comes from three inputs: hue, peak chroma (Cmax) and the step 9
5
+ * lightness. Steps follow the Radix role contract:
6
+ * 1-2 backgrounds, 3-5 component backgrounds, 6-8 borders (8 = strong),
7
+ * 9-10 solids, 11-12 text.
8
+ * Values are gamut-fitted to Display P3 (the `oklch()` value) and then to sRGB
9
+ * (the hex fallback) by reducing chroma at constant L and h.
10
+ */
11
+ import { compositeOver, wcagContrast } from './contrast.js';
12
+ import { fitColor, hexToRgb, oklchToHex, rgbToHex, round } from './convert.js';
13
+ export const MODES = ['light', 'dark'];
14
+ export const STEPS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
15
+ /** Lightness targets per step (report section 1.4). `null` = derived from the seed. */
16
+ export const LIGHTNESS = {
17
+ light: [0.993, 0.983, 0.962, 0.938, 0.912, 0.882, 0.838, 0.772, null, null, 0.505, 0.265],
18
+ dark: [0.165, 0.198, 0.238, 0.268, 0.298, 0.336, 0.392, 0.47, null, null, 0.8, 0.945],
19
+ };
20
+ /** Step 10 offset from step 9 (light: seed - 0.04, dark: seed + 0.045). */
21
+ export const STEP10_DELTA = { light: -0.04, dark: 0.045 };
22
+ /** Chroma multipliers (x Cmax) for chromatic scales. */
23
+ export const CHROMA_FACTORS = {
24
+ light: [0.012, 0.03, 0.07, 0.11, 0.16, 0.22, 0.3, 0.45, 1, 1, 0.85, 0.45],
25
+ dark: [0.1, 0.13, 0.25, 0.33, 0.4, 0.46, 0.54, 0.68, 1, 1, 0.75, 0.25],
26
+ };
27
+ /**
28
+ * Chroma multipliers for neutral scales, derived from the Slate Dusk table
29
+ * (C 0.002..0.016 for Cmax 0.016), identical in both modes.
30
+ */
31
+ export const NEUTRAL_CHROMA_FACTORS = [
32
+ 0.125, 0.1875, 0.3125, 0.375, 0.4375, 0.5, 0.625, 0.8125, 1, 1, 1, 0.875,
33
+ ];
34
+ const WHITE_HEX = '#ffffff';
35
+ function solveNeutralStep9(spec, mode, backgrounds) {
36
+ // Search the step 9 lightness closest to the "mid" point that gives >= 3:1
37
+ // against every background. Light mode searches downwards, dark upwards.
38
+ const start = mode === 'light' ? 0.7 : 0.45;
39
+ const dir = mode === 'light' ? -1 : 1;
40
+ for (let i = 0; i <= 300; i++) {
41
+ const l = round(start + dir * i * 0.001, 3);
42
+ const c = spec.cmax * (spec.chromaFactors ?? NEUTRAL_CHROMA_FACTORS)[8];
43
+ const hex = fitColor({ l, c, h: spec.hue }).hex;
44
+ const min = Math.min(...backgrounds.map((bg) => wcagContrast(hex, bg)));
45
+ if (min >= 3.0)
46
+ return l;
47
+ }
48
+ throw new Error(`Cannot solve neutral step 9 for ${spec.name} (${mode})`);
49
+ }
50
+ function lightnessFor(spec, mode, index) {
51
+ const override = spec.lightness?.[mode]?.[index];
52
+ if (override !== undefined && override !== null)
53
+ return override;
54
+ return LIGHTNESS[mode][index] ?? null;
55
+ }
56
+ function chromaFor(spec, mode, index) {
57
+ const factors = spec.chromaFactors ?? (spec.kind === 'neutral' ? NEUTRAL_CHROMA_FACTORS : CHROMA_FACTORS[mode]);
58
+ return spec.cmax * factors[index];
59
+ }
60
+ /**
61
+ * Radix-style per-channel alpha solve: find the smallest alpha and an opaque
62
+ * colour F so that F at that alpha over `bg` reproduces `target` exactly
63
+ * (sRGB, gamma space, as browsers composite).
64
+ */
65
+ export function solveAlpha(targetHex, bgHex) {
66
+ const t = hexToRgb(targetHex);
67
+ const b = hexToRgb(bgHex);
68
+ let a = 0;
69
+ for (let i = 0; i < 3; i++) {
70
+ const tc = t[i];
71
+ const bc = b[i];
72
+ if (tc > bc)
73
+ a = Math.max(a, (tc - bc) / (1 - bc));
74
+ else if (tc < bc)
75
+ a = Math.max(a, (bc - tc) / bc);
76
+ }
77
+ a = Math.min(1, Math.ceil(a * 1000) / 1000);
78
+ if (a === 0)
79
+ a = 0.001;
80
+ const f = [0, 1, 2].map((i) => (t[i] - (1 - a) * b[i]) / a);
81
+ const hex = rgbToHex(f);
82
+ const comps = hexToRgb(hex).map((v) => round(v, 4));
83
+ const alphaHex = Math.round(a * 255)
84
+ .toString(16)
85
+ .padStart(2, '0');
86
+ return { hex, components: comps, alpha: a, hex8: `${hex}${alphaHex}` };
87
+ }
88
+ /** Backdrops the alpha twins are solved against: white (light) and black (dark). */
89
+ export const ALPHA_BACKDROP = { light: '#ffffff', dark: '#000000' };
90
+ export function generateScale(spec) {
91
+ const notes = [];
92
+ const perMode = {};
93
+ for (const mode of MODES) {
94
+ const lightness = [];
95
+ // Steps 1-8 and 11-12 from the table.
96
+ for (let i = 0; i < 12; i++)
97
+ lightness[i] = lightnessFor(spec, mode, i) ?? Number.NaN;
98
+ let l9;
99
+ if (spec.kind === 'neutral' &&
100
+ spec.seedL === undefined &&
101
+ spec.lightness?.[mode]?.[8] == null) {
102
+ const bgs = [0, 1, 2].map((i) => fitColor({ l: lightness[i], c: chromaFor(spec, mode, i), h: spec.hue }).hex);
103
+ if (mode === 'light')
104
+ bgs.push(WHITE_HEX);
105
+ l9 = solveNeutralStep9(spec, mode, bgs);
106
+ notes.push(`${mode} step 9 solved to L ${l9} for >= 3:1 against steps 1-3${mode === 'light' ? ' and white' : ''}`);
107
+ }
108
+ else {
109
+ l9 = spec.lightness?.[mode]?.[8] ?? spec.seedL ?? Number.NaN;
110
+ }
111
+ if (Number.isNaN(l9))
112
+ throw new Error(`Scale ${spec.name} needs seedL`);
113
+ lightness[8] = l9;
114
+ lightness[9] = spec.lightness?.[mode]?.[9] ?? round(l9 + STEP10_DELTA[mode], 3);
115
+ perMode[mode] = lightness.map((l, i) => {
116
+ const exact = spec.stepOverrides?.[mode]?.[i + 1];
117
+ return fitColor(exact ?? { l, c: chromaFor(spec, mode, i), h: spec.hue });
118
+ });
119
+ }
120
+ // On-solid text: white if it reaches 4.5:1 on step 9 (identical in both modes for
121
+ // chromatic scales), otherwise the light-mode step 12 (Radix's choice for amber).
122
+ const light9 = perMode.light[8].hex;
123
+ const whiteOk = wcagContrast(WHITE_HEX, light9) >= 4.5;
124
+ const darkText = perMode.light[11];
125
+ const white = { kind: 'white', hex: WHITE_HEX, fitted: fitColor({ l: 1, c: 0, h: 0 }) };
126
+ const dark = { kind: 'dark', hex: darkText.hex, fitted: darkText };
127
+ const onSolid = whiteOk ? white : dark;
128
+ // Step 10 (solid hover) must keep on-solid text at >= 4.5:1. If the recipe
129
+ // direction breaks that, the hover moves the other way (away from the text).
130
+ for (const mode of MODES) {
131
+ const s10 = perMode[mode][9];
132
+ if (wcagContrast(onSolid.hex, s10.hex) < 4.5 && spec.kind === 'chromatic') {
133
+ const flipped = round(perMode[mode][8].p3.l - STEP10_DELTA[mode], 3);
134
+ const refit = fitColor({ l: flipped, c: chromaFor(spec, mode, 9), h: spec.hue });
135
+ notes.push(`${mode} step 10 flipped from L ${s10.p3.l} to L ${flipped} so on-solid text keeps 4.5:1 (was ${wcagContrast(onSolid.hex, s10.hex).toFixed(2)}:1)`);
136
+ perMode[mode][9] = refit;
137
+ }
138
+ }
139
+ const steps = STEPS.map((step, i) => ({
140
+ step,
141
+ light: perMode.light[i],
142
+ dark: perMode.dark[i],
143
+ }));
144
+ const alpha = {
145
+ light: perMode.light.map((c) => solveAlpha(c.hex, ALPHA_BACKDROP.light)),
146
+ dark: perMode.dark.map((c) => solveAlpha(c.hex, ALPHA_BACKDROP.dark)),
147
+ };
148
+ const onSolidByMode = {};
149
+ for (const mode of MODES) {
150
+ if (spec.kind === 'chromatic')
151
+ onSolidByMode[mode] = onSolid;
152
+ else {
153
+ const s9 = perMode[mode][8].hex;
154
+ onSolidByMode[mode] =
155
+ wcagContrast(WHITE_HEX, s9) >= wcagContrast(dark.hex, s9) ? white : dark;
156
+ }
157
+ }
158
+ return { spec, steps, alpha, onSolid, onSolidByMode, notes };
159
+ }
160
+ /** Convenience: the sRGB hex of a step in a mode. */
161
+ export function hexOf(scale, step, mode) {
162
+ return scale.steps[step - 1][mode].hex;
163
+ }
164
+ /**
165
+ * Build an accent scale from a community-supplied brand colour (report section 7):
166
+ * auto-pick the on-solid colour and auto-correct step 9 lightness until the
167
+ * on-solid text reaches 4.5:1. Throws with an explanation if that is impossible.
168
+ */
169
+ export function createCommunityAccent(name, hex, parse) {
170
+ const seed = parse(hex);
171
+ const base = {
172
+ name,
173
+ label: name,
174
+ kind: 'chromatic',
175
+ hue: round(seed.h, 1),
176
+ cmax: Math.max(0.04, round(seed.c, 3)),
177
+ seedL: round(seed.l, 3),
178
+ };
179
+ const tryL = (l) => generateScale({ ...base, seedL: round(l, 3) });
180
+ let scale = tryL(base.seedL);
181
+ const passes = (s) => wcagContrast(s.onSolid.hex, hexOf(s, 9, 'light')) >= 4.5 &&
182
+ wcagContrast(s.onSolid.hex, hexOf(s, 10, 'light')) >= 4.5 &&
183
+ wcagContrast(s.onSolid.hex, hexOf(s, 10, 'dark')) >= 4.5;
184
+ if (passes(scale))
185
+ return { scale, seedL: base.seedL, adjusted: false };
186
+ // Move L away from the mid-tone in small steps, both directions, nearest first.
187
+ for (let d = 0.005; d <= 0.25; d += 0.005) {
188
+ for (const l of [base.seedL - d, base.seedL + d]) {
189
+ if (l < 0.3 || l > 0.9)
190
+ continue;
191
+ scale = tryL(l);
192
+ if (passes(scale))
193
+ return { scale, seedL: round(l, 3), adjusted: true };
194
+ }
195
+ }
196
+ throw new Error(`Accent ${hex} cannot reach 4.5:1 with white or dark on-solid text at any usable lightness; choose a different colour.`);
197
+ }
198
+ /** Re-exported for tests and docs. */
199
+ export { compositeOver, oklchToHex };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * The UND UI scale seeds (report sections 1.5 and 7).
3
+ *
4
+ * Ten core scales (neutral + nine hues) plus five theme neutrals used by the
5
+ * Kesar, Neem, Mitti, Raat and Sutra themes.
6
+ */
7
+ import type { ScaleSpec } from './palette.js';
8
+ export declare const CORE_SCALES: readonly ScaleSpec[];
9
+ /** Theme neutrals (report section 7). */
10
+ export declare const THEME_NEUTRALS: readonly ScaleSpec[];
11
+ export declare const ALL_SCALES: readonly ScaleSpec[];
12
+ /** Status mapping (report 1.5). */
13
+ export declare const STATUS_SCALES: {
14
+ readonly success: "green";
15
+ readonly warning: "amber";
16
+ readonly danger: "red";
17
+ readonly info: "blue";
18
+ };
@@ -0,0 +1,115 @@
1
+ export const CORE_SCALES = [
2
+ {
3
+ name: 'neutral',
4
+ label: 'Neutral "Slate Dusk"',
5
+ kind: 'neutral',
6
+ hue: 275,
7
+ cmax: 0.016,
8
+ description: 'Brand-tinted neutral (hue 275).',
9
+ },
10
+ {
11
+ name: 'indigo',
12
+ label: 'UND Indigo',
13
+ kind: 'chromatic',
14
+ hue: 275.6,
15
+ cmax: 0.24,
16
+ seedL: 0.531,
17
+ description: 'Brand accent, seed #504fec.',
18
+ },
19
+ {
20
+ name: 'saffron',
21
+ label: 'Saffron "Kesar"',
22
+ kind: 'chromatic',
23
+ hue: 55,
24
+ cmax: 0.19,
25
+ seedL: 0.72,
26
+ },
27
+ {
28
+ name: 'green',
29
+ label: 'Green "Neem"',
30
+ kind: 'chromatic',
31
+ hue: 152,
32
+ cmax: 0.16,
33
+ seedL: 0.53,
34
+ description: 'Success.',
35
+ },
36
+ {
37
+ name: 'amber',
38
+ label: 'Amber "Haldi"',
39
+ kind: 'chromatic',
40
+ hue: 80,
41
+ cmax: 0.17,
42
+ seedL: 0.81,
43
+ description: 'Warning.',
44
+ },
45
+ {
46
+ name: 'red',
47
+ label: 'Red "Sindoor"',
48
+ kind: 'chromatic',
49
+ hue: 25,
50
+ cmax: 0.22,
51
+ seedL: 0.575,
52
+ description: 'Danger.',
53
+ },
54
+ {
55
+ name: 'blue',
56
+ label: 'Blue "Neel"',
57
+ kind: 'chromatic',
58
+ hue: 252,
59
+ cmax: 0.19,
60
+ seedL: 0.55,
61
+ description: 'Info.',
62
+ },
63
+ { name: 'teal', label: 'Teal "Monsoon"', kind: 'chromatic', hue: 195, cmax: 0.13, seedL: 0.53 },
64
+ {
65
+ name: 'terracotta',
66
+ label: 'Terracotta "Mitti"',
67
+ kind: 'chromatic',
68
+ hue: 38,
69
+ cmax: 0.15,
70
+ seedL: 0.56,
71
+ },
72
+ { name: 'pink', label: 'Pink "Gulabi"', kind: 'chromatic', hue: 350, cmax: 0.2, seedL: 0.58 },
73
+ ];
74
+ /** Theme neutrals (report section 7). */
75
+ export const THEME_NEUTRALS = [
76
+ { name: 'warm', label: 'Warm neutral (Kesar)', kind: 'neutral', hue: 60, cmax: 0.01 },
77
+ { name: 'sage', label: 'Sage neutral (Neem)', kind: 'neutral', hue: 150, cmax: 0.01 },
78
+ {
79
+ name: 'sand',
80
+ label: 'Sand neutral (Mitti)',
81
+ kind: 'neutral',
82
+ hue: 70,
83
+ cmax: 0.014,
84
+ // Mitti's canvas is slightly cream in light mode (report section 7).
85
+ stepOverrides: { light: { 1: { l: 0.985, c: 0.008, h: 80 } } },
86
+ },
87
+ {
88
+ name: 'midnight',
89
+ label: 'Deep midnight (Raat)',
90
+ kind: 'neutral',
91
+ hue: 270,
92
+ cmax: 0.02,
93
+ // Raat is dark-only: step 1 sits at L 0.13 with C 0.02 (report section 7).
94
+ lightness: {
95
+ dark: [0.13, 0.165, 0.205, 0.235, 0.265, 0.302, 0.36, 0.44, null, null, 0.8, 0.945],
96
+ },
97
+ chromaFactors: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0.8, 0.6],
98
+ },
99
+ {
100
+ name: 'gray',
101
+ label: 'Pure neutral (Sutra)',
102
+ kind: 'neutral',
103
+ hue: 275,
104
+ cmax: 0.002,
105
+ chromaFactors: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
106
+ },
107
+ ];
108
+ export const ALL_SCALES = [...CORE_SCALES, ...THEME_NEUTRALS];
109
+ /** Status mapping (report 1.5). */
110
+ export const STATUS_SCALES = {
111
+ success: 'green',
112
+ warning: 'amber',
113
+ danger: 'red',
114
+ info: 'blue',
115
+ };