@atom63/styles 0.1.0-beta.1 → 0.1.0-beta.11

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 (119) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +22 -13
  3. package/generated/atom63.figma-sync.json +23277 -0
  4. package/generated/atom63.tokens.json +19650 -8403
  5. package/generated/z-layers.d.ts +33 -0
  6. package/generated/z-layers.js +31 -0
  7. package/package.json +29 -15
  8. package/src/compat/a63-from-shadcn.css +281 -0
  9. package/src/compat/shadcn.browser.test.ts +23 -0
  10. package/src/compat/shadcn.css +133 -17
  11. package/src/contracts/action.css +4 -3
  12. package/src/contracts/action.native.css +11 -0
  13. package/src/contracts/action.tokens.json +66 -0
  14. package/src/contracts/badge.css +128 -58
  15. package/src/contracts/badge.resolver.json +454 -0
  16. package/src/contracts/choice.css +5 -2
  17. package/src/contracts/choice.native.css +9 -0
  18. package/src/contracts/choice.tokens.json +66 -0
  19. package/src/contracts/control.css +54 -57
  20. package/src/contracts/control.native.css +13 -0
  21. package/src/contracts/control.tokens.json +294 -0
  22. package/src/contracts/environment.css +178 -60
  23. package/src/contracts/environment.native.css +23 -0
  24. package/src/contracts/environment.resolver.json +1210 -0
  25. package/src/contracts/field.css +4 -2
  26. package/src/contracts/field.tokens.json +40 -0
  27. package/src/contracts/marker.css +6 -4
  28. package/src/contracts/marker.tokens.json +15 -0
  29. package/src/contracts/menu.css +25 -61
  30. package/src/contracts/menu.native.css +10 -0
  31. package/src/contracts/menu.resolver.json +122 -0
  32. package/src/contracts/overlay.css +11 -13
  33. package/src/contracts/overlay.native.css +13 -0
  34. package/src/contracts/overlay.tokens.json +103 -0
  35. package/src/contracts/segment.css +22 -68
  36. package/src/contracts/segment.tokens.json +19 -0
  37. package/src/contracts/selection.css +5 -1
  38. package/src/contracts/selection.tokens.json +42 -0
  39. package/src/contracts/skeleton.css +18 -2
  40. package/src/contracts/skeleton.resolver.json +62 -0
  41. package/src/contracts/surface.css +17 -24
  42. package/src/contracts/surface.native.css +17 -0
  43. package/src/contracts/surface.tokens.json +154 -0
  44. package/src/contracts/toggle.css +27 -3
  45. package/src/contracts/toggle.resolver.json +129 -0
  46. package/src/contracts/track.css +3 -1
  47. package/src/contracts/track.tokens.json +39 -0
  48. package/src/contracts/trigger.css +5 -2
  49. package/src/contracts/trigger.native.css +9 -0
  50. package/src/contracts/trigger.tokens.json +58 -0
  51. package/src/contracts/widget.css +76 -51
  52. package/src/contracts/widget.native.css +23 -0
  53. package/src/contracts/widget.resolver.json +1110 -0
  54. package/src/tailwind/roles.json +189 -0
  55. package/src/tailwind/theme.css +216 -24
  56. package/src/tailwind/utilities.css +17 -0
  57. package/src/themes/aqua.css +126 -89
  58. package/src/themes/aqua.native.css +80 -0
  59. package/src/themes/aqua.resolver.json +1304 -0
  60. package/src/themes/modern.css +10 -14
  61. package/src/themes/modern.native.css +16 -0
  62. package/src/themes/modern.resolver.json +350 -0
  63. package/src/themes/retro.css +22 -26
  64. package/src/themes/retro.native.css +16 -0
  65. package/src/themes/retro.resolver.json +794 -0
  66. package/src/themes/terminal.css +164 -41
  67. package/src/themes/terminal.native.css +22 -0
  68. package/src/themes/terminal.resolver.json +585 -0
  69. package/src/tokens/brand-action.css +65 -0
  70. package/src/tokens/brand-action.resolver.json +155 -0
  71. package/src/tokens/brand-ramp.css +108 -0
  72. package/src/tokens/brand-ramp.resolver.json +418 -0
  73. package/src/tokens/brand.css +6 -146
  74. package/src/tokens/chart-palette.browser.test.ts +188 -0
  75. package/src/tokens/color-mix-space.browser.test.ts +101 -0
  76. package/src/tokens/contrast.browser.test.ts +303 -5
  77. package/src/tokens/density.browser.test.ts +167 -0
  78. package/src/tokens/figma-parity.browser.test.ts +270 -0
  79. package/src/tokens/font.css +12 -0
  80. package/src/tokens/font.resolver.json +128 -0
  81. package/src/tokens/form-factor.browser.test.ts +129 -0
  82. package/src/tokens/foundation/effects.css +13 -13
  83. package/src/tokens/foundation/effects.resolver.json +433 -0
  84. package/src/tokens/foundation/fonts.css +11 -5
  85. package/src/tokens/foundation/fonts.tokens.json +45 -0
  86. package/src/tokens/foundation/motion.css +3 -0
  87. package/src/tokens/foundation/motion.tokens.json +53 -0
  88. package/src/tokens/foundation/palette.css +3 -0
  89. package/src/tokens/foundation/palette.tokens.json +1743 -0
  90. package/src/tokens/foundation/primitives.css +20 -43
  91. package/src/tokens/foundation/primitives.tokens.json +3258 -0
  92. package/src/tokens/foundation/radius.css +11 -9
  93. package/src/tokens/foundation/radius.tokens.json +108 -0
  94. package/src/tokens/foundation/typography.css +49 -12
  95. package/src/tokens/foundation/typography.native.css +21 -0
  96. package/src/tokens/foundation/typography.resolver.json +1008 -0
  97. package/src/tokens/foundation.css +0 -1
  98. package/src/tokens/index.css +1 -0
  99. package/src/tokens/motion.css +3 -1
  100. package/src/tokens/motion.resolver.json +135 -0
  101. package/src/tokens/native-values.json +38 -0
  102. package/src/tokens/personalization-axes.browser.test.ts +18 -3
  103. package/src/tokens/plate-scope.browser.test.ts +73 -0
  104. package/src/tokens/radius.css +12 -8
  105. package/src/tokens/radius.resolver.json +138 -0
  106. package/src/tokens/semantics.css +716 -41
  107. package/src/tokens/semantics.resolver.json +1639 -0
  108. package/src/tokens/space.css +8 -6
  109. package/src/tokens/space.tokens.json +78 -0
  110. package/src/tokens/surface.css +13 -19
  111. package/src/tokens/surface.resolver.json +706 -0
  112. package/src/tokens/swift-parity.browser.test.ts +141 -0
  113. package/src/tokens/type-roles.css +88 -0
  114. package/src/tokens/type-roles.resolver.json +419 -0
  115. package/src/tokens/type-scale.css +6 -0
  116. package/src/tokens/type-scale.resolver.json +67 -0
  117. package/generated/atom63.figma-tokens.json +0 -21664
  118. package/src/tokens/foundation/aliases.css +0 -28
  119. package/src/z-layers.ts +0 -40
@@ -1,148 +1,8 @@
1
1
  /*
2
- * Brand personalization axis (semantic ramp indirection).
3
- *
4
- * --a63-brand-50..950 is the primary/brand tonal scale the whole system derives
5
- * from (--a63-action-primary = --a63-brand-500, foreground = --a63-brand-50,
6
- * plus any soft/hover/border tints). [data-a63-brand="b1".."b6"] remaps the
7
- * ENTIRE ramp to one of the six brand palettes at runtime — one switch recolors
8
- * the whole primary family coherently. Only the brand ramp changes; status
9
- * colors (danger, etc.) stay semantic.
10
- *
11
- * The base (:root, and explicit b1) is the default blue ramp, so components work
12
- * with no [data-a63-brand] set. Brand is meant to be set at a root/personalization
13
- * scope and inherited; a subtree can override by setting [data-a63-brand] again.
2
+ * Brand personalization axis. Both parts are generated from DTCG resolvers:
3
+ * the ramp (--a63-brand-50..950 per [data-a63-brand]) from brand-ramp.resolver.json,
4
+ * and the primary action derived from it from brand-action.resolver.json. This
5
+ * file stays the one entry point for the brand axis.
14
6
  */
15
-
16
- :root,
17
- [data-a63-brand='b1'] {
18
- --a63-brand-50: var(--color-b1-50);
19
- --a63-brand-100: var(--color-b1-100);
20
- --a63-brand-200: var(--color-b1-200);
21
- --a63-brand-300: var(--color-b1-300);
22
- --a63-brand-400: var(--color-b1-400);
23
- --a63-brand-500: var(--color-b1-500);
24
- --a63-brand-600: var(--color-b1-600);
25
- --a63-brand-700: var(--color-b1-700);
26
- --a63-brand-800: var(--color-b1-800);
27
- --a63-brand-900: var(--color-b1-900);
28
- --a63-brand-950: var(--color-b1-950);
29
- }
30
-
31
- [data-a63-brand='b2'] {
32
- --a63-brand-50: var(--color-b2-50);
33
- --a63-brand-100: var(--color-b2-100);
34
- --a63-brand-200: var(--color-b2-200);
35
- --a63-brand-300: var(--color-b2-300);
36
- --a63-brand-400: var(--color-b2-400);
37
- --a63-brand-500: var(--color-b2-500);
38
- --a63-brand-600: var(--color-b2-600);
39
- --a63-brand-700: var(--color-b2-700);
40
- --a63-brand-800: var(--color-b2-800);
41
- --a63-brand-900: var(--color-b2-900);
42
- --a63-brand-950: var(--color-b2-950);
43
- }
44
-
45
- [data-a63-brand='b3'] {
46
- --a63-brand-50: var(--color-b3-50);
47
- --a63-brand-100: var(--color-b3-100);
48
- --a63-brand-200: var(--color-b3-200);
49
- --a63-brand-300: var(--color-b3-300);
50
- --a63-brand-400: var(--color-b3-400);
51
- --a63-brand-500: var(--color-b3-500);
52
- --a63-brand-600: var(--color-b3-600);
53
- --a63-brand-700: var(--color-b3-700);
54
- --a63-brand-800: var(--color-b3-800);
55
- --a63-brand-900: var(--color-b3-900);
56
- --a63-brand-950: var(--color-b3-950);
57
- }
58
-
59
- [data-a63-brand='b4'] {
60
- --a63-brand-50: var(--color-b4-50);
61
- --a63-brand-100: var(--color-b4-100);
62
- --a63-brand-200: var(--color-b4-200);
63
- --a63-brand-300: var(--color-b4-300);
64
- --a63-brand-400: var(--color-b4-400);
65
- --a63-brand-500: var(--color-b4-500);
66
- --a63-brand-600: var(--color-b4-600);
67
- --a63-brand-700: var(--color-b4-700);
68
- --a63-brand-800: var(--color-b4-800);
69
- --a63-brand-900: var(--color-b4-900);
70
- --a63-brand-950: var(--color-b4-950);
71
- }
72
-
73
- [data-a63-brand='b5'] {
74
- --a63-brand-50: var(--color-b5-50);
75
- --a63-brand-100: var(--color-b5-100);
76
- --a63-brand-200: var(--color-b5-200);
77
- --a63-brand-300: var(--color-b5-300);
78
- --a63-brand-400: var(--color-b5-400);
79
- --a63-brand-500: var(--color-b5-500);
80
- --a63-brand-600: var(--color-b5-600);
81
- --a63-brand-700: var(--color-b5-700);
82
- --a63-brand-800: var(--color-b5-800);
83
- --a63-brand-900: var(--color-b5-900);
84
- --a63-brand-950: var(--color-b5-950);
85
- }
86
-
87
- [data-a63-brand='b6'] {
88
- --a63-brand-50: var(--color-b6-50);
89
- --a63-brand-100: var(--color-b6-100);
90
- --a63-brand-200: var(--color-b6-200);
91
- --a63-brand-300: var(--color-b6-300);
92
- --a63-brand-400: var(--color-b6-400);
93
- --a63-brand-500: var(--color-b6-500);
94
- --a63-brand-600: var(--color-b6-600);
95
- --a63-brand-700: var(--color-b6-700);
96
- --a63-brand-800: var(--color-b6-800);
97
- --a63-brand-900: var(--color-b6-900);
98
- --a63-brand-950: var(--color-b6-950);
99
- }
100
-
101
- /*
102
- * Auto brand — the ramp is derived at runtime from the active wallpaper; the
103
- * controller writes --color-auto-50..950 inline. Here we map the brand ramp onto
104
- * those values, with a b1-blue fallback so `auto` is coherent before extraction
105
- * (or if it fails).
106
- */
107
- [data-a63-brand='auto'] {
108
- --a63-brand-50: var(--color-auto-50, var(--color-b1-50));
109
- --a63-brand-100: var(--color-auto-100, var(--color-b1-100));
110
- --a63-brand-200: var(--color-auto-200, var(--color-b1-200));
111
- --a63-brand-300: var(--color-auto-300, var(--color-b1-300));
112
- --a63-brand-400: var(--color-auto-400, var(--color-b1-400));
113
- --a63-brand-500: var(--color-auto-500, var(--color-b1-500));
114
- --a63-brand-600: var(--color-auto-600, var(--color-b1-600));
115
- --a63-brand-700: var(--color-auto-700, var(--color-b1-700));
116
- --a63-brand-800: var(--color-auto-800, var(--color-b1-800));
117
- --a63-brand-900: var(--color-auto-900, var(--color-b1-900));
118
- --a63-brand-950: var(--color-auto-950, var(--color-b1-950));
119
- }
120
-
121
- /*
122
- * Primary action derived from the brand ramp.
123
- *
124
- * These MUST be declared on the same scope that remaps the ramp (:root and every
125
- * [data-a63-brand]) — a var() inside a custom property resolves on the element
126
- * where the property is DECLARED, then inherits as a resolved value. Declaring
127
- * only on :root would freeze primary to the base ramp regardless of a downstream
128
- * [data-a63-brand] override.
129
- */
130
- :root,
131
- [data-a63-brand] {
132
- --a63-action-primary: var(--a63-brand-500);
133
- --a63-action-primary-hover: var(--a63-brand-600);
134
- /* Auto-contrast foreground: near-white on dark primaries, near-black on bright
135
- ones (light brand picks / wallpaper golds). oklch `l` is the proxy; the steep
136
- clamp is a light/dark STEP at l≈0.665 — aligned to WCAG *large-text* white
137
- 3:1 (Y≈0.30), not the old 0.70 / JS 0.36 pair which left mid-golds (mustard
138
- ~L 0.69, Y≈0.33) on light text at ~2.8:1. Default blue (~L 0.62) stays light;
139
- green/orange brand 500s that fail white 3:1 flip to dark. Chroma ×0.06 keeps
140
- a faint brand tint. Supersedes JS --color-auto-foreground for components. */
141
- --a63-action-primary-foreground: oklch(
142
- from var(--a63-action-primary) clamp(0.15, (0.665 - l) * 1000, 0.985) calc(c * 0.06) h
143
- );
144
- /* Subtle depth shadow under text on the primary (theme lever; Aqua sets a gel
145
- tint). Default none so flat themes stay crisp. */
146
- --a63-action-primary-text-shadow: none;
147
- --a63-focus-ring: color-mix(in oklch, var(--a63-brand-500) 30%, transparent);
148
- }
7
+ @import './brand-ramp.css';
8
+ @import './brand-action.css';
@@ -0,0 +1,188 @@
1
+ import { afterEach, describe, expect, it } from 'vitest'
2
+ import { root } from '../test/apply-styles'
3
+
4
+ /*
5
+ * The chart series colours (--a63-chart-1..5) must stay apart from each other
6
+ * and from the brand for every brand, theme and mode, and each series must
7
+ * reach WCAG 1.4.11's 3:1 for graphics against the surfaces a chart sits on.
8
+ * Series 2 to 5 turn the hue of series 1 (the brand), so a wallpaper brand
9
+ * (`auto`) is checked too, with a few ramps written the way the
10
+ * personalization controller writes them.
11
+ */
12
+
13
+ const themes = ['modern', 'aqua', 'retro', 'terminal'] as const
14
+ const modes = ['light', 'dark'] as const
15
+ const series = [1, 2, 3, 4, 5].map(index => `--a63-chart-${index}`)
16
+ const surfaces = ['--a63-surface-page', '--a63-surface-panel'] as const
17
+
18
+ /** WCAG 1.4.11: graphics that carry meaning reach 3:1 against what is next to them. */
19
+ const GRAPHICS_CONTRAST = 3
20
+ /** Smallest OKLab distance between two series, and between a series and the brand. */
21
+ const MIN_DISTANCE = 0.1
22
+
23
+ /** Wallpaper ramps the controller could write: a teal, a gold and a magenta. */
24
+ const autoRamps: Record<string, Record<number, string>> = {
25
+ 'auto (teal)': { 400: 'oklch(0.78 0.12 190)', 600: 'oklch(0.55 0.1 195)' },
26
+ 'auto (gold)': { 400: 'oklch(0.84 0.14 85)', 600: 'oklch(0.56 0.12 75)' },
27
+ 'auto (magenta)': { 400: 'oklch(0.74 0.2 330)', 600: 'oklch(0.55 0.22 335)' },
28
+ }
29
+
30
+ const brands: { label: string; brand: string; ramp?: Record<number, string> }[] = [
31
+ ...['b1', 'b2', 'b3', 'b4', 'b5', 'b6'].map(brand => ({ label: brand, brand })),
32
+ ...Object.entries(autoRamps).map(([label, ramp]) => ({ label, brand: 'auto', ramp })),
33
+ ]
34
+
35
+ afterEach(() => {
36
+ const element = root()
37
+ for (const attribute of ['data-a63-theme', 'data-a63-mode', 'data-a63-brand']) {
38
+ element.removeAttribute(attribute)
39
+ }
40
+ element.removeAttribute('style')
41
+ element.classList.remove('dark', 'light')
42
+ })
43
+
44
+ interface Rgb {
45
+ r: number
46
+ g: number
47
+ b: number
48
+ }
49
+
50
+ const canvas = document.createElement('canvas')
51
+ canvas.width = 1
52
+ canvas.height = 1
53
+ const context = canvas.getContext('2d', { colorSpace: 'srgb', willReadFrequently: true })
54
+
55
+ /** Paints one pixel and reads it back as sRGB, so any CSS colour syntax works. */
56
+ function toRgb(color: string): Rgb {
57
+ if (!context) throw new Error('2d canvas unavailable')
58
+ context.clearRect(0, 0, 1, 1)
59
+ context.fillStyle = color
60
+ context.fillRect(0, 0, 1, 1)
61
+ const [r = 0, g = 0, b = 0] = context.getImageData(0, 0, 1, 1).data
62
+ return { r, g, b }
63
+ }
64
+
65
+ function painted(cssVar: string): string {
66
+ const probe = document.createElement('div')
67
+ probe.style.color = `var(${cssVar})`
68
+ document.body.appendChild(probe)
69
+ const value = getComputedStyle(probe).color
70
+ probe.remove()
71
+ return value
72
+ }
73
+
74
+ const linear = (channel: number) => {
75
+ const value = channel / 255
76
+ return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4
77
+ }
78
+
79
+ function luminance({ r, g, b }: Rgb): number {
80
+ return 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b)
81
+ }
82
+
83
+ function contrast(a: Rgb, b: Rgb): number {
84
+ const [light, dark] = [luminance(a), luminance(b)].sort((x, y) => y - x) as [number, number]
85
+ return (light + 0.05) / (dark + 0.05)
86
+ }
87
+
88
+ function oklab({ r, g, b }: Rgb): [number, number, number] {
89
+ const [lr, lg, lb] = [linear(r), linear(g), linear(b)]
90
+ const l = Math.cbrt(0.4122214708 * lr + 0.5363325363 * lg + 0.0514459929 * lb)
91
+ const m = Math.cbrt(0.2119034982 * lr + 0.6806995451 * lg + 0.1073969566 * lb)
92
+ const s = Math.cbrt(0.0883024619 * lr + 0.2817188376 * lg + 0.6299787005 * lb)
93
+ return [
94
+ 0.2104542553 * l + 0.793617785 * m - 0.0040720468 * s,
95
+ 1.9779984951 * l - 2.428592205 * m + 0.4505937099 * s,
96
+ 0.0259040371 * l + 0.7827717662 * m - 0.808675766 * s,
97
+ ]
98
+ }
99
+
100
+ function distance(a: Rgb, b: Rgb): number {
101
+ const [x, y] = [oklab(a), oklab(b)]
102
+ return Math.hypot(x[0] - y[0], x[1] - y[1], x[2] - y[2])
103
+ }
104
+
105
+ function apply(theme: string, mode: string, brand: (typeof brands)[number]): void {
106
+ const element = root()
107
+ element.setAttribute('data-a63-theme', theme)
108
+ element.setAttribute('data-a63-mode', mode)
109
+ element.classList.remove('dark', 'light')
110
+ element.classList.add(mode)
111
+ element.setAttribute('data-a63-brand', brand.brand)
112
+ element.removeAttribute('style')
113
+ for (const [step, value] of Object.entries(brand.ramp ?? {})) {
114
+ element.style.setProperty(`--color-auto-${step}`, value)
115
+ }
116
+ }
117
+
118
+ describe('chart palette', () => {
119
+ it('keeps every series apart from the others and from the brand, at 3:1 on its surfaces', () => {
120
+ const failures: string[] = []
121
+
122
+ for (const brand of brands) {
123
+ for (const theme of themes) {
124
+ for (const mode of modes) {
125
+ apply(theme, mode, brand)
126
+ const where = `${brand.label} ${theme}/${mode}`
127
+ const colors = series.map(name => toRgb(painted(name)))
128
+ const primary = toRgb(painted('--a63-action-primary'))
129
+
130
+ for (const surface of surfaces) {
131
+ const background = toRgb(painted(surface))
132
+ colors.forEach((color, index) => {
133
+ const ratio = contrast(color, background)
134
+ if (ratio < GRAPHICS_CONTRAST) {
135
+ failures.push(`${where} ${series[index]} on ${surface}: ${ratio.toFixed(2)}:1`)
136
+ }
137
+ })
138
+ }
139
+
140
+ for (let a = 0; a < colors.length; a += 1) {
141
+ // Series 1 is the brand; the others must not land on it.
142
+ if (a > 0) {
143
+ const apart = distance(colors[a]!, primary)
144
+ if (apart < MIN_DISTANCE) {
145
+ failures.push(`${where} ${series[a]} vs the brand: ${apart.toFixed(3)}`)
146
+ }
147
+ }
148
+ for (let b = a + 1; b < colors.length; b += 1) {
149
+ const apart = distance(colors[a]!, colors[b]!)
150
+ if (apart < MIN_DISTANCE) {
151
+ failures.push(`${where} ${series[a]} vs ${series[b]}: ${apart.toFixed(3)}`)
152
+ }
153
+ }
154
+ }
155
+ }
156
+ }
157
+ }
158
+
159
+ expect(failures, failures.join('\n')).toEqual([])
160
+ })
161
+
162
+ /* A UIProvider can set only the brand, under a root that sets the mode. The
163
+ series must turn from that nested brand, not the root's. */
164
+ it('turns from a brand set on a nested scope', () => {
165
+ const scope = document.createElement('div')
166
+ scope.setAttribute('data-a63-brand', 'b5')
167
+ document.body.appendChild(scope)
168
+ const read = (name: string) => {
169
+ const probe = document.createElement('div')
170
+ probe.style.color = `var(${name})`
171
+ scope.appendChild(probe)
172
+ const value = toRgb(getComputedStyle(probe).color)
173
+ probe.remove()
174
+ return value
175
+ }
176
+ const primary = read('--a63-action-primary')
177
+ const failures = series
178
+ .slice(1)
179
+ .map(name => ({ name, apart: distance(read(name), primary) }))
180
+ .filter(({ apart }) => apart < MIN_DISTANCE)
181
+ .map(({ name, apart }) => `${name} vs the nested brand: ${apart.toFixed(3)}`)
182
+ expect(distance(read('--a63-chart-1'), toRgb(painted('--a63-chart-1')))).toBeGreaterThan(
183
+ MIN_DISTANCE
184
+ )
185
+ scope.remove()
186
+ expect(failures, failures.join('\n')).toEqual([])
187
+ })
188
+ })
@@ -0,0 +1,101 @@
1
+ import { afterEach, describe, expect, it } from 'vitest'
2
+ import manifest from '../../generated/atom63.tokens.json'
3
+ import { resetRoot, root } from '../test/apply-styles'
4
+
5
+ /*
6
+ * An oklch color-mix() interpolates hue. When one operand is a slightly tinted
7
+ * neutral (the n2–n6 surface palettes), engines disagree on that operand's hue:
8
+ * Chromium treats a hue this close to gray as powerless and takes the other
9
+ * operand's hue, while Firefox and WebKit interpolate from it, so the same mix
10
+ * renders a different color per browser (up to 170/255 apart on a theme). oklab
11
+ * has no hue, so every engine agrees. CSS Color 4 makes an oklch hue powerless
12
+ * only at C <= 0.000004, so Firefox and WebKit follow the spec and Chromium's
13
+ * wider threshold is the bug. The rule: an oklch mix may not take an operand
14
+ * whose chroma is above that epsilon but low (a tinted neutral) under any
15
+ * surface or mode; mix those in oklab. Exact grays, black and white fall under
16
+ * the epsilon, have no hue in any engine, and are fine.
17
+ */
18
+
19
+ const lowChroma = { min: 0.000004, max: 0.04 }
20
+
21
+ type Entry = { cssVar: string; type: string; value: string; scope: string }
22
+
23
+ /* A nested brand or surface scope declares a mode role as
24
+ `var(--a63-if-light, <light>) var(--a63-if-dark, <dark>)`; only one branch
25
+ applies, so each is checked as its own value. */
26
+ function branches(entry: Entry): Entry[] {
27
+ const found: Entry[] = []
28
+ for (const mode of ['light', 'dark']) {
29
+ const start = entry.value.search(new RegExp(`var\\(\\s*--a63-if-${mode}\\s*,`))
30
+ if (start === -1) continue
31
+ let depth = 0
32
+ let end = start
33
+ for (; end < entry.value.length; end++) {
34
+ if (entry.value[end] === '(') depth++
35
+ if (entry.value[end] === ')' && --depth === 0) break
36
+ }
37
+ const value = entry.value.slice(entry.value.indexOf(',', start) + 1, end)
38
+ found.push({ ...entry, value, scope: `${entry.scope} (${mode})` })
39
+ }
40
+ return found.length > 0 ? found : [entry]
41
+ }
42
+
43
+ const mixes = (manifest as { entries: Entry[] }).entries
44
+ .flatMap(branches)
45
+ .filter(entry => entry.type === 'color' && /color-mix\(\s*in oklch/.test(entry.value))
46
+
47
+ const probe = document.createElement('div')
48
+ document.body.append(probe)
49
+
50
+ /** oklch chroma of a CSS color, or null when it does not parse. */
51
+ function chroma(color: string): number | null {
52
+ probe.style.color = ''
53
+ probe.style.color = `oklch(from ${color} l c h)`
54
+ if (!probe.style.color) return null
55
+ const match = /^oklch\(([-\d.e]+) ([-\d.e]+)/.exec(getComputedStyle(probe).color)
56
+ return match ? Number(match[2]) : null
57
+ }
58
+
59
+ /** The var() operands of a color-mix(), in order. */
60
+ function operands(value: string): string[] {
61
+ return [...value.matchAll(/var\((--[\w-]+)\)/g)].map(match => match[1])
62
+ }
63
+
64
+ afterEach(() => {
65
+ resetRoot()
66
+ for (const attribute of ['data-a63-theme', 'data-a63-surface', 'data-a63-mode'])
67
+ root().removeAttribute(attribute)
68
+ root().classList.remove('light', 'dark')
69
+ })
70
+
71
+ describe('color-mix interpolation space', () => {
72
+ it('finds the oklch mixes', () => {
73
+ expect(mixes.length).toBeGreaterThan(0)
74
+ })
75
+
76
+ it('never mixes a tinted neutral in oklch', () => {
77
+ const violations = new Set<string>()
78
+ for (const theme of ['modern', 'aqua', 'retro', 'terminal'])
79
+ for (const surface of ['n1', 'n2', 'n3', 'n4', 'n5', 'n6'])
80
+ for (const mode of ['light', 'dark']) {
81
+ const element = root()
82
+ element.setAttribute('data-a63-theme', theme)
83
+ element.setAttribute('data-a63-surface', surface)
84
+ element.setAttribute('data-a63-mode', mode)
85
+ element.classList.remove('light', 'dark')
86
+ element.classList.add(mode)
87
+ const styles = getComputedStyle(element)
88
+ for (const entry of mixes) {
89
+ for (const operand of operands(entry.value)) {
90
+ const value = styles.getPropertyValue(operand).trim()
91
+ const c = value && chroma(value)
92
+ if (typeof c === 'number' && c >= lowChroma.min && c < lowChroma.max)
93
+ violations.add(
94
+ `${entry.cssVar} [${entry.scope.replace(/\s+/g, ' ')}] via ${operand}`
95
+ )
96
+ }
97
+ }
98
+ }
99
+ expect([...violations].sort()).toEqual([])
100
+ })
101
+ })