@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,102 @@
1
+ import type { Context, ResolvedToken } from '../dtcg/engine.js';
2
+ export interface MatColor {
3
+ kind: 'color';
4
+ hex: string;
5
+ alpha: number;
6
+ /** display-p3 components when the colour is outside sRGB. */
7
+ p3?: [number, number, number];
8
+ /** sRGB components 0..1. */
9
+ rgb: [number, number, number];
10
+ }
11
+ export interface MatDimension {
12
+ kind: 'dimension';
13
+ /** Value in px (rem x 16). */
14
+ px: number;
15
+ css: string;
16
+ }
17
+ export interface MatNumber {
18
+ kind: 'number';
19
+ value: number;
20
+ unit?: string;
21
+ }
22
+ export interface MatDuration {
23
+ kind: 'duration';
24
+ ms: number;
25
+ }
26
+ export interface MatEasing {
27
+ kind: 'easing';
28
+ bezier: [number, number, number, number];
29
+ /** Spring preset name when the easing is a spring (expressive character). */
30
+ spring?: string;
31
+ }
32
+ export interface MatFontFamily {
33
+ kind: 'fontFamily';
34
+ families: string[];
35
+ }
36
+ export interface MatFontWeight {
37
+ kind: 'fontWeight';
38
+ value: number;
39
+ }
40
+ export interface MatShadowLayer {
41
+ color: MatColor;
42
+ x: number;
43
+ y: number;
44
+ blur: number;
45
+ spread: number;
46
+ inset: boolean;
47
+ }
48
+ export interface MatShadow {
49
+ kind: 'shadow';
50
+ layers: MatShadowLayer[];
51
+ }
52
+ export interface MatTypography {
53
+ kind: 'typography';
54
+ families: string[];
55
+ /** Max (desktop) size in px. */
56
+ sizePx: number;
57
+ /** Min (mobile) size in px; equals sizePx when not fluid. */
58
+ minSizePx: number;
59
+ fluid: boolean;
60
+ sizeEm?: number;
61
+ weight: number;
62
+ letterSpacingEm: number;
63
+ lineHeight: number;
64
+ lineHeightIndic: number;
65
+ ios?: {
66
+ textStyle: string;
67
+ size: number;
68
+ weight: string;
69
+ };
70
+ android?: {
71
+ textStyle: string;
72
+ size: number;
73
+ lineHeight: number;
74
+ };
75
+ }
76
+ export interface MatSpring {
77
+ kind: 'spring';
78
+ response: number;
79
+ dampingRatio: number;
80
+ mass: number;
81
+ stiffness: number;
82
+ damping: number;
83
+ bounce: number;
84
+ overshoot: number;
85
+ settleMs: number;
86
+ linear: string;
87
+ bezier: [number, number, number, number];
88
+ }
89
+ export type MatValue = MatColor | MatDimension | MatNumber | MatDuration | MatEasing | MatFontFamily | MatFontWeight | MatShadow | MatTypography | MatSpring;
90
+ /** Follow an alias chain to the token that holds the literal value. */
91
+ export declare function literalToken(ctx: Context, path: string): ResolvedToken;
92
+ export interface MaterializeOptions {
93
+ /** Apply radius.factor and tempo (default true). */
94
+ themed?: boolean;
95
+ }
96
+ export declare function contextKnobs(ctx: Context): {
97
+ radiusFactor: number;
98
+ tempo: number;
99
+ };
100
+ export declare function materialize(ctx: Context, path: string): MatValue;
101
+ /** All tokens of a context, materialized, in path order. */
102
+ export declare function materializeAll(ctx: Context): Map<string, MatValue>;
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Turns a resolved context into platform-neutral values that every non-CSS
3
+ * format (JS, Swift, Kotlin, Dart, React Native) renders. The values match
4
+ * what the CSS computes for the same context: radius x radius.factor,
5
+ * durations and springs x the clamped theme tempo (platform factor excluded).
6
+ */
7
+ import { round } from '../color/convert.js';
8
+ import { undExt } from '../dtcg/types.js';
9
+ const WEIGHTS = {
10
+ thin: 100,
11
+ hairline: 100,
12
+ 'extra-light': 200,
13
+ 'ultra-light': 200,
14
+ light: 300,
15
+ normal: 400,
16
+ regular: 400,
17
+ book: 400,
18
+ medium: 500,
19
+ 'semi-bold': 600,
20
+ 'demi-bold': 600,
21
+ bold: 700,
22
+ 'extra-bold': 800,
23
+ 'ultra-bold': 800,
24
+ black: 900,
25
+ heavy: 900,
26
+ 'extra-black': 950,
27
+ 'ultra-black': 950,
28
+ };
29
+ function hexRgb(hex) {
30
+ const n = Number.parseInt(hex.slice(1), 16);
31
+ return [
32
+ round(((n >> 16) & 255) / 255, 4),
33
+ round(((n >> 8) & 255) / 255, 4),
34
+ round((n & 255) / 255, 4),
35
+ ];
36
+ }
37
+ /** Follow an alias chain to the token that holds the literal value. */
38
+ export function literalToken(ctx, path) {
39
+ let t = ctx.tokens.get(path);
40
+ const seen = new Set();
41
+ while (t?.alias && !seen.has(t.path)) {
42
+ seen.add(t.path);
43
+ t = ctx.tokens.get(t.alias);
44
+ }
45
+ if (!t)
46
+ throw new Error(`Missing token ${path}`);
47
+ return t;
48
+ }
49
+ function matColor(raw, ext) {
50
+ const alpha = raw.alpha ?? 1;
51
+ const p3 = raw.colorSpace === 'oklch' &&
52
+ ext.p3 &&
53
+ ext.srgbChroma !== undefined &&
54
+ raw.components[1] > ext.srgbChroma
55
+ ? ext.p3
56
+ : undefined;
57
+ return { kind: 'color', hex: raw.hex, alpha, rgb: hexRgb(raw.hex), ...(p3 ? { p3 } : {}) };
58
+ }
59
+ function px(d) {
60
+ return d.unit === 'rem' ? d.value * 16 : d.value;
61
+ }
62
+ export function contextKnobs(ctx) {
63
+ const num = (p, d) => ctx.tokens.get(p)?.resolved ?? d;
64
+ const tempo = Math.min(num('tempo.max', 1.5), Math.max(num('tempo.min', 0.75), num('motion.tempo', 1)));
65
+ return { radiusFactor: num('radius.factor', 1), tempo };
66
+ }
67
+ export function materialize(ctx, path) {
68
+ const token = ctx.tokens.get(path);
69
+ if (!token)
70
+ throw new Error(`Missing token ${path}`);
71
+ const lit = literalToken(ctx, path);
72
+ const ext = undExt(lit);
73
+ const v = token.resolved;
74
+ const { radiusFactor, tempo } = contextKnobs(ctx);
75
+ switch (token.type) {
76
+ case 'color':
77
+ return matColor(v, ext);
78
+ case 'dimension': {
79
+ const d = v;
80
+ let value = px(d);
81
+ if (ext.radiusScale)
82
+ value = round(value * radiusFactor, 2);
83
+ const css = ext.radiusScale
84
+ ? `${value}px`
85
+ : d.value === 0
86
+ ? '0'
87
+ : `${round(d.value, 4)}${d.unit}`;
88
+ return { kind: 'dimension', px: round(value, 3), css };
89
+ }
90
+ case 'number':
91
+ return { kind: 'number', value: v, ...(ext.unit ? { unit: ext.unit } : {}) };
92
+ case 'duration': {
93
+ const d = v;
94
+ const ms = d.unit === 's' ? d.value * 1000 : d.value;
95
+ return { kind: 'duration', ms: ext.tempo === false ? ms : Math.round(ms * tempo) };
96
+ }
97
+ case 'cubicBezier':
98
+ return {
99
+ kind: 'easing',
100
+ bezier: v,
101
+ ...(typeof ext.spring === 'string' ? { spring: ext.spring.replace(/^spring\./, '') } : {}),
102
+ };
103
+ case 'fontFamily':
104
+ return { kind: 'fontFamily', families: Array.isArray(v) ? v : [v] };
105
+ case 'fontWeight':
106
+ return { kind: 'fontWeight', value: typeof v === 'number' ? v : WEIGHTS[v] };
107
+ case 'shadow': {
108
+ const layers = (Array.isArray(v) ? v : [v]);
109
+ return {
110
+ kind: 'shadow',
111
+ layers: layers
112
+ .filter((l) => (l.color.alpha ?? 1) > 0)
113
+ .map((l) => ({
114
+ color: matColor(l.color, {}),
115
+ x: px(l.offsetX),
116
+ y: px(l.offsetY),
117
+ blur: px(l.blur),
118
+ spread: px(l.spread),
119
+ inset: l.inset === true,
120
+ })),
121
+ };
122
+ }
123
+ case 'typography': {
124
+ const t = v;
125
+ const sizePx = px(t.fontSize);
126
+ return {
127
+ kind: 'typography',
128
+ families: Array.isArray(t.fontFamily) ? t.fontFamily : [t.fontFamily],
129
+ sizePx,
130
+ minSizePx: ext.fluid ? ext.fluid.minPx : sizePx,
131
+ fluid: Boolean(ext.fluid),
132
+ ...(ext.fontSizeEm !== undefined ? { sizeEm: ext.fontSizeEm } : {}),
133
+ weight: typeof t.fontWeight === 'number' ? t.fontWeight : WEIGHTS[t.fontWeight],
134
+ letterSpacingEm: ext.letterSpacingEm ?? 0,
135
+ lineHeight: t.lineHeight,
136
+ lineHeightIndic: ext.lineHeightIndic ?? t.lineHeight,
137
+ ...(ext.ios ? { ios: ext.ios } : {}),
138
+ ...(ext.android ? { android: ext.android } : {}),
139
+ };
140
+ }
141
+ case 'transition': {
142
+ if (ext.kind !== 'spring')
143
+ throw new Error(`Unsupported transition ${path}`);
144
+ const t = v;
145
+ // Tempo scales response: stiffness / tempo^2, damping / tempo, settle x tempo.
146
+ return {
147
+ kind: 'spring',
148
+ response: round(ext.response * tempo, 4),
149
+ dampingRatio: ext.dampingRatio,
150
+ mass: 1,
151
+ stiffness: round(ext.stiffness / tempo ** 2, 1),
152
+ damping: round(ext.damping / tempo, 2),
153
+ bounce: ext.bounce,
154
+ overshoot: ext.overshoot,
155
+ settleMs: Math.round(ext.settleMs * tempo),
156
+ linear: ext.linear,
157
+ bezier: t.timingFunction,
158
+ };
159
+ }
160
+ default:
161
+ throw new Error(`Cannot materialize ${token.type} (${path})`);
162
+ }
163
+ }
164
+ /** All tokens of a context, materialized, in path order. */
165
+ export function materializeAll(ctx) {
166
+ const out = new Map();
167
+ for (const path of [...ctx.tokens.keys()].sort())
168
+ out.set(path, materialize(ctx, path));
169
+ return out;
170
+ }
@@ -0,0 +1,46 @@
1
+ import type { TokenFiles } from '../dtcg/engine.js';
2
+ import type { ThemeDefinition } from '../themes/definitions.js';
3
+ import type { MatColor, MatEasing, MatShadow, MatSpring, MatTypography } from './materialize.js';
4
+ export type Variant = 'light' | 'dark' | 'lightMore' | 'darkMore';
5
+ export declare const VARIANTS: readonly Variant[];
6
+ /** `status.success.on-solid` -> `statusSuccessOnSolid`; `space.0_5` -> `0p5`. */
7
+ export declare function camel(path: string): string;
8
+ /** Identifier-safe key for scale steps: `2xl` -> `xxl`, `3xl` -> `xxxl`, `0_5` -> `s0p5`. */
9
+ export declare function stepName(key: string, numericPrefix?: string): string;
10
+ export interface NativeTheme {
11
+ def: ThemeDefinition;
12
+ colors: Record<Variant, Map<string, MatColor>>;
13
+ shadows: Record<'light' | 'dark', Map<string, MatShadow>>;
14
+ radius: Map<string, number>;
15
+ durations: Map<string, number>;
16
+ springs: Map<string, MatSpring>;
17
+ easings: Map<string, MatEasing>;
18
+ typeface: Record<'body' | 'heading' | 'display' | 'code', string[]>;
19
+ tempo: number;
20
+ }
21
+ export interface NativeModel {
22
+ version: string;
23
+ /** Semantic colour roles: camel name -> token path. */
24
+ colorRoles: [string, string][];
25
+ themes: NativeTheme[];
26
+ spacing: Map<string, number>;
27
+ control: Record<'comfortable' | 'compact' | 'touch', Map<string, number>>;
28
+ iconSize: Map<string, number>;
29
+ borderWidth: Map<string, number>;
30
+ breakpoint: Map<string, number>;
31
+ zIndex: Map<string, number>;
32
+ typography: Map<string, MatTypography>;
33
+ reducedDurations: Map<string, number>;
34
+ focus: {
35
+ width: number;
36
+ offset: number;
37
+ widthMore: number;
38
+ };
39
+ target: {
40
+ fine: number;
41
+ touch: number;
42
+ android: number;
43
+ };
44
+ }
45
+ export declare function buildNativeModel(files: TokenFiles, version: string): NativeModel;
46
+ export type { MatColor, MatShadow, MatSpring, MatTypography, MatEasing };
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Platform-neutral model for native code generation. Native wrappers expose
3
+ * semantic roles only (report 1.4/8.2): no hue names, no primitive scales.
4
+ */
5
+ import { resolveContext } from '../dtcg/engine.js';
6
+ import { isBridge } from '../dtcg/types.js';
7
+ import { THEMES } from '../themes/definitions.js';
8
+ import { materialize } from './materialize.js';
9
+ export const VARIANTS = ['light', 'dark', 'lightMore', 'darkMore'];
10
+ /** `status.success.on-solid` -> `statusSuccessOnSolid`; `space.0_5` -> `0p5`. */
11
+ export function camel(path) {
12
+ const parts = path.split(/[.-]/).map((p) => p.replace(/_/g, 'p'));
13
+ return parts
14
+ .map((p, i) => (i === 0 ? p : p.charAt(0).toUpperCase() + p.slice(1)))
15
+ .join('')
16
+ .replace(/^(\d)/, 'n$1');
17
+ }
18
+ /** Identifier-safe key for scale steps: `2xl` -> `xxl`, `3xl` -> `xxxl`, `0_5` -> `s0p5`. */
19
+ export function stepName(key, numericPrefix = 's') {
20
+ if (key === '2xl')
21
+ return 'xxl';
22
+ if (key === '3xl')
23
+ return 'xxxl';
24
+ if (/^\d/.test(key))
25
+ return `${numericPrefix}${key.replace(/_/g, 'p')}`;
26
+ return camel(key);
27
+ }
28
+ const COLOR_ROOTS = [
29
+ 'bg',
30
+ 'fg',
31
+ 'border',
32
+ 'accent',
33
+ 'focus',
34
+ 'status',
35
+ 'link',
36
+ 'dataviz',
37
+ 'selection',
38
+ 'material',
39
+ ];
40
+ function colorRoles(ctx) {
41
+ return [...ctx.tokens.values()]
42
+ .filter((t) => t.type === 'color' && !isBridge(t) && COLOR_ROOTS.includes(t.path.split('.')[0]))
43
+ .map((t) => [camel(t.path), t.path])
44
+ .sort((a, b) => a[1].localeCompare(b[1]));
45
+ }
46
+ function group(ctx, prefix, map) {
47
+ const out = new Map();
48
+ for (const t of ctx.tokens.values()) {
49
+ const key = t.path.slice(prefix.length + 1);
50
+ if (t.path.startsWith(`${prefix}.`) &&
51
+ !key.includes('.') &&
52
+ !key.startsWith('$') &&
53
+ !isBridge(t)) {
54
+ out.set(t.path.slice(prefix.length + 1), map(t.path));
55
+ }
56
+ }
57
+ return out;
58
+ }
59
+ const pxOf = (ctx) => (path) => {
60
+ const m = materialize(ctx, path);
61
+ if (m.kind === 'dimension')
62
+ return m.px;
63
+ if (m.kind === 'number')
64
+ return m.value;
65
+ throw new Error(`${path} is not a dimension`);
66
+ };
67
+ const msOf = (ctx) => (path) => {
68
+ const m = materialize(ctx, path);
69
+ if (m.kind !== 'duration')
70
+ throw new Error(`${path} is not a duration`);
71
+ return m.ms;
72
+ };
73
+ export function buildNativeModel(files, version) {
74
+ const base = resolveContext(files);
75
+ const roles = colorRoles(base);
76
+ const themes = THEMES.map((def) => {
77
+ const ctxFor = (v) => {
78
+ // Dark-only themes (Raat) use their single mode for the light variants too.
79
+ const mode = def.modes.length === 1 ? def.modes[0] : v.startsWith('dark') ? 'dark' : 'light';
80
+ return resolveContext(files, {
81
+ theme: def.name,
82
+ mode,
83
+ contrast: v.endsWith('More') ? 'more' : 'standard',
84
+ });
85
+ };
86
+ const ctxs = Object.fromEntries(VARIANTS.map((v) => [v, ctxFor(v)]));
87
+ const colors = Object.fromEntries(VARIANTS.map((v) => [
88
+ v,
89
+ new Map(roles.map(([name, path]) => [name, materialize(ctxs[v], path)])),
90
+ ]));
91
+ const shadowMap = (c) => {
92
+ const m = new Map();
93
+ for (const t of c.tokens.values())
94
+ if (t.path.startsWith('shadow.'))
95
+ m.set(t.path.slice(7), materialize(c, t.path));
96
+ return m;
97
+ };
98
+ const c = ctxs.light;
99
+ const durations = new Map();
100
+ for (const t of c.tokens.values()) {
101
+ if (t.type === 'duration' &&
102
+ !isBridge(t) &&
103
+ (t.path.startsWith('motion.') || t.path.startsWith('duration.')))
104
+ durations.set(t.path, msOf(c)(t.path));
105
+ }
106
+ const springs = new Map();
107
+ for (const t of c.tokens.values())
108
+ if (t.path.startsWith('spring.'))
109
+ springs.set(t.path.slice(7), materialize(c, t.path));
110
+ const easings = new Map();
111
+ for (const t of c.tokens.values())
112
+ if (t.type === 'cubicBezier')
113
+ easings.set(t.path, materialize(c, t.path));
114
+ // Metric-matched "* Fallback" faces are web-only @font-face rules (local() + size-adjust).
115
+ const fam = (p) => materialize(c, p).families.filter((f) => !f.endsWith(' Fallback'));
116
+ return {
117
+ def,
118
+ colors,
119
+ shadows: {
120
+ light: shadowMap(resolveContext(files, { theme: def.name, mode: def.modes[0] })),
121
+ dark: shadowMap(resolveContext(files, { theme: def.name, mode: def.modes.at(-1) })),
122
+ },
123
+ radius: group(c, 'radius', pxOf(c)),
124
+ durations,
125
+ springs,
126
+ easings,
127
+ typeface: {
128
+ body: fam('typeface.body'),
129
+ heading: fam('typeface.heading'),
130
+ display: fam('typeface.display'),
131
+ code: fam('typeface.code'),
132
+ },
133
+ tempo: def.tempo,
134
+ };
135
+ });
136
+ const ctl = (density) => {
137
+ const c = resolveContext(files, { density });
138
+ const m = group(c, 'control.height', pxOf(c));
139
+ m.set('paddingX', pxOf(c)('control.padding-x'));
140
+ m.set('gap', pxOf(c)('control.gap'));
141
+ m.set('targetMin', pxOf(c)('target.min'));
142
+ return m;
143
+ };
144
+ const typography = new Map();
145
+ for (const t of base.tokens.values())
146
+ if (t.type === 'typography')
147
+ typography.set(t.path.slice(5), materialize(base, t.path));
148
+ const reduced = resolveContext(files, { motion: 'reduced' });
149
+ const reducedDurations = new Map();
150
+ for (const t of reduced.tokens.values())
151
+ if (t.type === 'duration' && t.path.startsWith('motion.'))
152
+ reducedDurations.set(t.path, msOf(reduced)(t.path));
153
+ const more = resolveContext(files, { contrast: 'more' });
154
+ return {
155
+ version,
156
+ colorRoles: roles,
157
+ themes,
158
+ spacing: group(base, 'space', pxOf(base)),
159
+ control: { comfortable: ctl('comfortable'), compact: ctl('compact'), touch: ctl('touch') },
160
+ iconSize: group(base, 'size.icon', pxOf(base)),
161
+ borderWidth: group(base, 'border-width', pxOf(base)),
162
+ breakpoint: group(base, 'breakpoint', pxOf(base)),
163
+ zIndex: group(base, 'z', pxOf(base)),
164
+ typography,
165
+ reducedDurations,
166
+ focus: {
167
+ width: pxOf(base)('focus.width'),
168
+ offset: pxOf(base)('focus.offset'),
169
+ widthMore: pxOf(more)('focus.width'),
170
+ },
171
+ target: {
172
+ fine: pxOf(base)('size.target.fine'),
173
+ touch: pxOf(base)('size.target.touch'),
174
+ android: pxOf(base)('size.target.android'),
175
+ },
176
+ };
177
+ }
@@ -0,0 +1,24 @@
1
+ /** WCAG 2.x relative luminance of a 6-digit sRGB hex colour. */
2
+ export declare function relativeLuminance(hex: string): number;
3
+ /** WCAG 2.x contrast ratio between two sRGB hex colours (order-independent). */
4
+ export declare function wcagContrast(a: string, b: string): number;
5
+ /** Composite a translucent colour over an opaque backdrop (sRGB, gamma-space, like browsers). */
6
+ export declare function compositeOver(fgHex: string, alpha: number, bgHex: string): string;
7
+ /** APCA Lc (0.0.98G) for text colour on background colour. Positive = dark on light. */
8
+ export declare function apcaContrast(textHex: string, bgHex: string): number;
9
+ /** WCAG 2.2 AA thresholds used by the contrast matrix. */
10
+ export declare const WCAG_AA: {
11
+ /** 1.4.3 normal text. */
12
+ readonly text: 4.5;
13
+ /** 1.4.3 large text (>= 24px, or >= 18.66px bold). */
14
+ readonly largeText: 3;
15
+ /** 1.4.11 non-text UI components, graphical objects and focus indicators. */
16
+ readonly ui: 3;
17
+ };
18
+ /** APCA advisory targets (report section 1.3). */
19
+ export declare const APCA_TARGETS: {
20
+ readonly body: 75;
21
+ readonly secondary: 60;
22
+ readonly large: 45;
23
+ readonly nonText: 30;
24
+ };
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Contrast maths.
3
+ *
4
+ * - WCAG 2.x contrast ratio (blocking in CI): (L1 + 0.05) / (L2 + 0.05) with
5
+ * sRGB relative luminance.
6
+ * - APCA 0.0.98G (advisory only, see report section 1.3): implemented from the
7
+ * published constants. APCA is not part of WCAG 2.2 and is never used to fail
8
+ * the build.
9
+ */
10
+ import { hexToRgb } from './convert.js';
11
+ function linearize(channel) {
12
+ return channel <= 0.04045 ? channel / 12.92 : ((channel + 0.055) / 1.055) ** 2.4;
13
+ }
14
+ /** WCAG 2.x relative luminance of a 6-digit sRGB hex colour. */
15
+ export function relativeLuminance(hex) {
16
+ const [r, g, b] = hexToRgb(hex);
17
+ return 0.2126 * linearize(r) + 0.7152 * linearize(g) + 0.0722 * linearize(b);
18
+ }
19
+ /** WCAG 2.x contrast ratio between two sRGB hex colours (order-independent). */
20
+ export function wcagContrast(a, b) {
21
+ const la = relativeLuminance(a);
22
+ const lb = relativeLuminance(b);
23
+ const [hi, lo] = la > lb ? [la, lb] : [lb, la];
24
+ return (hi + 0.05) / (lo + 0.05);
25
+ }
26
+ /** Composite a translucent colour over an opaque backdrop (sRGB, gamma-space, like browsers). */
27
+ export function compositeOver(fgHex, alpha, bgHex) {
28
+ const f = hexToRgb(fgHex);
29
+ const b = hexToRgb(bgHex);
30
+ const mix = (i) => alpha * f[i] + (1 - alpha) * b[i];
31
+ const to = (v) => Math.round(Math.min(1, Math.max(0, v)) * 255)
32
+ .toString(16)
33
+ .padStart(2, '0');
34
+ return `#${to(mix(0))}${to(mix(1))}${to(mix(2))}`;
35
+ }
36
+ // APCA 0.0.98G constants (https://github.com/Myndex/SAPC-APCA, documentation of the formula).
37
+ const APCA = {
38
+ mainTRC: 2.4,
39
+ sRco: 0.2126729,
40
+ sGco: 0.7151522,
41
+ sBco: 0.072175,
42
+ normBG: 0.56,
43
+ normTXT: 0.57,
44
+ revTXT: 0.62,
45
+ revBG: 0.65,
46
+ blkThrs: 0.022,
47
+ blkClmp: 1.414,
48
+ scaleBoW: 1.14,
49
+ scaleWoB: 1.14,
50
+ loBoWoffset: 0.027,
51
+ loWoBoffset: 0.027,
52
+ deltaYmin: 0.0005,
53
+ loClip: 0.1,
54
+ };
55
+ function apcaY(hex) {
56
+ const [r, g, b] = hexToRgb(hex);
57
+ return (APCA.sRco * r ** APCA.mainTRC + APCA.sGco * g ** APCA.mainTRC + APCA.sBco * b ** APCA.mainTRC);
58
+ }
59
+ /** APCA Lc (0.0.98G) for text colour on background colour. Positive = dark on light. */
60
+ export function apcaContrast(textHex, bgHex) {
61
+ let yTxt = apcaY(textHex);
62
+ let yBg = apcaY(bgHex);
63
+ if (yTxt < APCA.blkThrs)
64
+ yTxt += (APCA.blkThrs - yTxt) ** APCA.blkClmp;
65
+ if (yBg < APCA.blkThrs)
66
+ yBg += (APCA.blkThrs - yBg) ** APCA.blkClmp;
67
+ if (Math.abs(yBg - yTxt) < APCA.deltaYmin)
68
+ return 0;
69
+ let out;
70
+ if (yBg > yTxt) {
71
+ const sapc = (yBg ** APCA.normBG - yTxt ** APCA.normTXT) * APCA.scaleBoW;
72
+ out = sapc < APCA.loClip ? 0 : sapc - APCA.loBoWoffset;
73
+ }
74
+ else {
75
+ const sapc = (yBg ** APCA.revBG - yTxt ** APCA.revTXT) * APCA.scaleWoB;
76
+ out = sapc > -APCA.loClip ? 0 : sapc + APCA.loWoBoffset;
77
+ }
78
+ return out * 100;
79
+ }
80
+ /** WCAG 2.2 AA thresholds used by the contrast matrix. */
81
+ export const WCAG_AA = {
82
+ /** 1.4.3 normal text. */
83
+ text: 4.5,
84
+ /** 1.4.3 large text (>= 24px, or >= 18.66px bold). */
85
+ largeText: 3,
86
+ /** 1.4.11 non-text UI components, graphical objects and focus indicators. */
87
+ ui: 3,
88
+ };
89
+ /** APCA advisory targets (report section 1.3). */
90
+ export const APCA_TARGETS = {
91
+ body: 75,
92
+ secondary: 60,
93
+ large: 45,
94
+ nonText: 30,
95
+ };
@@ -0,0 +1,39 @@
1
+ import type { Oklch, P3, Rgb } from 'culori';
2
+ export interface OklchTriple {
3
+ /** Lightness, 0..1 (DTCG oklch convention). */
4
+ l: number;
5
+ /** Chroma, >= 0. */
6
+ c: number;
7
+ /** Hue in degrees, 0..360. */
8
+ h: number;
9
+ }
10
+ /** Rounds to a fixed number of decimals, avoiding `-0`. */
11
+ export declare function round(value: number, decimals: number): number;
12
+ export interface FittedColor {
13
+ /** The OKLCH value with P3-fitted chroma (rounded to 3 decimals). */
14
+ p3: OklchTriple;
15
+ /** The OKLCH value with sRGB-fitted chroma (rounded to 3 decimals). */
16
+ srgb: OklchTriple;
17
+ /** 6-digit sRGB hex of the sRGB-fitted colour. */
18
+ hex: string;
19
+ /** display-p3 components (0..1) of the P3-fitted colour, rounded to 4 decimals. */
20
+ p3Components: [number, number, number];
21
+ /** sRGB components (0..1) of the sRGB-fitted colour, rounded to 4 decimals. */
22
+ srgbComponents: [number, number, number];
23
+ }
24
+ /**
25
+ * Fit an OKLCH colour into Display P3 and then sRGB.
26
+ * Rounds L to 3 decimals, C to 3 decimals and h to 1 decimal *before*
27
+ * fitting, so the published token values are the exact inputs to conversion.
28
+ */
29
+ export declare function fitColor(input: OklchTriple): FittedColor;
30
+ /** Parse any CSS colour string into an OKLCH triple. */
31
+ export declare function parseOklch(css: string): OklchTriple;
32
+ /** sRGB hex (6-digit) to OKLCH. */
33
+ export declare function hexToOklch(hex: string): OklchTriple;
34
+ /** OKLCH to sRGB hex, clipping out-of-gamut values. */
35
+ export declare function oklchToHex(color: OklchTriple): string;
36
+ /** Hex string to sRGB components in 0..1. */
37
+ export declare function hexToRgb(hex: string): [number, number, number];
38
+ export declare function rgbToHex([r, g, b]: [number, number, number]): string;
39
+ export type { Oklch, P3, Rgb };