From bc91e7b961df195b1d136db5c76b45ef2eb1e911 Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Thu, 19 Nov 2026 13:25:12 +0000 Subject: [PATCH 1/2] fix: preserve primary and accent foreground ink --- docs/api-migrations.md | 19 ++++++++++++++++++- .../lui/src/main/kotlin/dev/lui/LuiTheme.kt | 5 ++--- .../kotlin/dev/lui/LuiDesignTokensTest.kt | 17 ++++++++++++++++- .../LUIAppleBackend/LUIThemePolicy.swift | 3 +-- platform/native/lui_paint/lui_paint.ml | 3 +-- platform/native/test/lui_native_test.ml | 13 ++++++++++++- platform/web/melange/core/lui_web_util.ml | 3 +-- platform/web/test/design-tokens.test.mjs | 3 +++ tooling/test/design-token-parity.test.mjs | 5 ++++- 9 files changed, 58 insertions(+), 13 deletions(-) diff --git a/docs/api-migrations.md b/docs/api-migrations.md index 6d87bdc47..2b2f5a087 100644 --- a/docs/api-migrations.md +++ b/docs/api-migrations.md @@ -1,5 +1,21 @@ # LUI API migration guide +### Foreground accent ink compatibility + +`primary` and `accent` in a foreground slot retain their accent ink color; +they are not remapped to `primary-foreground` or `accent-foreground`. This +restores pre-ADR-0007 heading and icon contrast, including Android light +themes where `onPrimary` is white. Theme overrides for `primary` and `accent` +continue to select that ink. Explicit `*-foreground` values remain available +for content painted on a corresponding filled control. + +Container and surface tokens such as `surface`, `card`, `secondary` and +`muted` still resolve to their ink counterparts in foreground slots. For +example, keep `text ~foreground:"primary" ~value:"General" []` for an +accent-colored heading, and use `~background:"primary"` with +`~foreground:"primary-foreground"` for text on a primary fill. Background +and border resolution is unchanged on all backends. + ### Phase 4 review: explicit sections in form sheets `Ui.form_sheet` now supplies an inset-grouped list as its content container, @@ -607,7 +623,8 @@ that need fixed line spacing can supply an explicit `line_height` metric. Existing colors, custom colors, theme overrides, and legacy aliases such as `title2` and `mono` remain available. Newly admitted colors reuse existing platform surfaces and accents rather than introducing a new palette. -Foreground slots resolve container tokens through their corresponding ink +Foreground slots resolve container tokens (excluding the accent ink names +`primary` and `accent`) through their corresponding ink token: for example `~foreground:"secondary"` uses `secondary-foreground`, not the pale secondary background. Explicit `*-foreground` theme overrides win. Apple keeps its adaptive secondary style only when the corresponding diff --git a/platform/android/lui/src/main/kotlin/dev/lui/LuiTheme.kt b/platform/android/lui/src/main/kotlin/dev/lui/LuiTheme.kt index dc5b53195..2c7fa003a 100644 --- a/platform/android/lui/src/main/kotlin/dev/lui/LuiTheme.kt +++ b/platform/android/lui/src/main/kotlin/dev/lui/LuiTheme.kt @@ -74,7 +74,7 @@ fun luiHexColor(name: String): Color? { /** Resolves a semantic color name against the inherited token table then * the Material 3 scheme, mirroring the Flutter `_color` mapping so both * mobile hosts agree. [foreground] picks the "on-*" variant for pair - * tokens like `accent`/`secondary`/`warning`/`success`/`error`. */ + * tokens like `secondary`/`warning`/`success`/`error`. */ @Composable fun luiThemeColor(name: String?, foreground: Boolean = false): Color? { if (name == null || (foreground && name.isBlank())) return null @@ -100,8 +100,7 @@ internal val LocalLuiInheritedForeground = compositionLocalOf { null } internal val luiForegroundTokens = mapOf( "background" to "foreground", "surface" to "foreground", "surface-raised" to "foreground", "card" to "card-foreground", - "primary" to "primary-foreground", "secondary" to "secondary-foreground", - "accent" to "accent-foreground", "muted" to "muted-foreground", + "secondary" to "secondary-foreground", "muted" to "muted-foreground", "destructive" to "destructive-foreground", "warning" to "warning-foreground", "success" to "success-foreground", "info" to "info-foreground", "glass" to "foreground", "bar" to "foreground", diff --git a/platform/android/lui/src/test/kotlin/dev/lui/LuiDesignTokensTest.kt b/platform/android/lui/src/test/kotlin/dev/lui/LuiDesignTokensTest.kt index 3d9f63d0c..570f0df3a 100644 --- a/platform/android/lui/src/test/kotlin/dev/lui/LuiDesignTokensTest.kt +++ b/platform/android/lui/src/test/kotlin/dev/lui/LuiDesignTokensTest.kt @@ -19,7 +19,7 @@ class LuiDesignTokensTest { } assertEquals(scheme.onSecondaryContainer, resolveLuiThemeColor("secondary", true, emptyMap(), scheme)) assertEquals(scheme.secondaryContainer, resolveLuiThemeColor("secondary", false, emptyMap(), scheme)) - assertEquals(scheme.onSecondaryContainer, resolveLuiThemeColor("accent", true, emptyMap(), scheme)) + assertEquals(scheme.primary, resolveLuiThemeColor("accent", true, emptyMap(), scheme)) assertEquals(scheme.onPrimary, resolveLuiThemeColor("accent-foreground", true, emptyMap(), scheme)) assertEquals(Color.Red, resolveLuiThemeColor("secondary", true, mapOf("secondary-foreground" to Color.Red), scheme)) assertEquals(Color.Transparent, resolveLuiThemeColor("transparent", false, emptyMap(), scheme)) @@ -27,6 +27,21 @@ class LuiDesignTokensTest { } } + @Test fun accentInkIsNotReplacedByContainerContentColors() { + for (scheme in listOf(LuiThemeDefaults.lightScheme(), LuiThemeDefaults.darkScheme())) { + assertEquals(scheme.primary, resolveLuiThemeColor("primary", true, emptyMap(), scheme)) + assertEquals(scheme.primary, resolveLuiThemeColor("accent", true, emptyMap(), scheme)) + assertEquals(scheme.primary, resolveLuiThemeColor("primary", false, emptyMap(), scheme)) + assertEquals(scheme.secondaryContainer, resolveLuiThemeColor("accent", false, emptyMap(), scheme)) + val tokens = mapOf("primary" to Color.Red, "primary-foreground" to Color.White, + "accent" to Color.Blue, "accent-foreground" to Color.Yellow) + assertEquals(Color.Red, resolveLuiThemeColor("primary", true, tokens, scheme)) + assertEquals(Color.Blue, resolveLuiThemeColor("accent", true, tokens, scheme)) + assertEquals(Color.White, resolveLuiThemeColor("primary-foreground", true, tokens, scheme)) + assertEquals(Color.Yellow, resolveLuiThemeColor("accent-foreground", true, tokens, scheme)) + } + } + @Test fun singleLineIsAnExactClassToken() { assertTrue(LuiTypography.singleLine("caption single-line semibold")) assertFalse(LuiTypography.singleLine(null)) diff --git a/platform/apple/Sources/LUIAppleBackend/LUIThemePolicy.swift b/platform/apple/Sources/LUIAppleBackend/LUIThemePolicy.swift index 1c3a88fd6..57aca4855 100644 --- a/platform/apple/Sources/LUIAppleBackend/LUIThemePolicy.swift +++ b/platform/apple/Sources/LUIAppleBackend/LUIThemePolicy.swift @@ -358,8 +358,7 @@ enum LUIThemeColorResolver { static let foregroundTokens = [ "background": "foreground", "surface": "foreground", "surface-raised": "foreground", "card": "card-foreground", - "primary": "primary-foreground", "secondary": "secondary-foreground", - "accent": "accent-foreground", "muted": "muted-foreground", + "secondary": "secondary-foreground", "muted": "muted-foreground", "destructive": "destructive-foreground", "success": "success-foreground", "warning": "warning-foreground", "info": "info-foreground", "glass": "foreground", "bar": "foreground", diff --git a/platform/native/lui_paint/lui_paint.ml b/platform/native/lui_paint/lui_paint.ml index 8f41bc06f..8f2cf3c94 100644 --- a/platform/native/lui_paint/lui_paint.ml +++ b/platform/native/lui_paint/lui_paint.ml @@ -578,8 +578,7 @@ let pbool store id name = let foreground_tokens = [ "background", "foreground"; "surface", "surface-foreground"; "surface-raised", "surface-raised-foreground"; "card", "card-foreground"; - "primary", "primary-foreground"; "secondary", "secondary-foreground"; - "accent", "accent-foreground"; "muted", "muted-foreground"; + "secondary", "secondary-foreground"; "muted", "muted-foreground"; "destructive", "destructive-foreground"; "warning", "warning-foreground"; "success", "success-foreground"; "info", "info-foreground"; "glass", "foreground"; "bar", "foreground" ] diff --git a/platform/native/test/lui_native_test.ml b/platform/native/test/lui_native_test.ml index 7c9cb556c..2510c2345 100644 --- a/platform/native/test/lui_native_test.ml +++ b/platform/native/test/lui_native_test.ml @@ -104,6 +104,16 @@ let test_blur_weight () = (* ---------- store→paint pipeline ---------- *) +let test_foreground_tokens () = + List.iter (fun token -> + check string "accent ink and explicit ink tokens stay unchanged" token + (Lui_paint.foreground_token token)) + ["primary"; "accent"; "primary-foreground"; "accent-foreground"; "#123456"]; + check string "surface colors still select their ink" "surface-foreground" + (Lui_paint.foreground_token "surface"); + check string "secondary containers still select their ink" "secondary-foreground" + (Lui_paint.foreground_token "secondary") + let test_pipeline () = let t = Lui_store.create () in Lui_store.apply_batch t @@ -142,4 +152,5 @@ let () = test_case "backdrop" `Quick test_backdrop_of; test_case "blur weight" `Quick test_blur_weight ]); ("pipeline", - [ test_case "store→paint" `Quick test_pipeline ]) ] + [ test_case "foreground tokens" `Quick test_foreground_tokens; + test_case "store→paint" `Quick test_pipeline ]) ] diff --git a/platform/web/melange/core/lui_web_util.ml b/platform/web/melange/core/lui_web_util.ml index 7bee4d272..65b337728 100644 --- a/platform/web/melange/core/lui_web_util.ml +++ b/platform/web/melange/core/lui_web_util.ml @@ -524,8 +524,7 @@ let semantic_color_names = let foreground_tokens = [ "background", "foreground"; "surface", "surface-foreground"; "surface-raised", "surface-raised-foreground"; "card", "card-foreground"; - "primary", "primary-foreground"; "secondary", "secondary-foreground"; - "accent", "accent-foreground"; "muted", "muted-foreground"; + "secondary", "secondary-foreground"; "muted", "muted-foreground"; "destructive", "destructive-foreground"; "warning", "warning-foreground"; "success", "success-foreground"; "info", "info-foreground"; "glass", "glass-foreground"; "bar", "bar-foreground" ] diff --git a/platform/web/test/design-tokens.test.mjs b/platform/web/test/design-tokens.test.mjs index 34f0467cf..2d2dc6d58 100644 --- a/platform/web/test/design-tokens.test.mjs +++ b/platform/web/test/design-tokens.test.mjs @@ -14,6 +14,9 @@ test('canonical colors resolve as CSS values and container foregrounds resolve a assert.equal(Util.web_color_value(true, 'secondary'), 'var(--color-secondary-foreground)'); assert.equal(Util.web_color_value(undefined, 'secondary'), 'var(--color-secondary)'); assert.equal(Util.web_color_value(true, 'surface'), 'var(--color-surface-foreground)'); + for (const color of ['primary', 'accent', 'primary-foreground', 'accent-foreground']) { + assert.equal(Util.web_color_value(true, color), `var(--color-${color})`); + } assert.equal(Util.web_color_value(true, '#123456'), '#123456'); assert.equal(Util.web_color_value(undefined, ''), ''); assert.equal(Util.web_color_value(undefined, 'custom-token'), 'custom-token'); diff --git a/tooling/test/design-token-parity.test.mjs b/tooling/test/design-token-parity.test.mjs index b9838bcc1..7893b1b71 100644 --- a/tooling/test/design-token-parity.test.mjs +++ b/tooling/test/design-token-parity.test.mjs @@ -42,7 +42,7 @@ test('every canonical color resolves in each backend and both native palettes', }); test('container colors have admitted foreground counterparts rather than raw fills', () => { - const nonContainers = new Set(['foreground', 'muted-foreground', 'border', 'input', 'ring', 'transparent']); + const nonContainers = new Set(['primary', 'accent', 'foreground', 'muted-foreground', 'border', 'input', 'ring', 'transparent']); const containers = vocabulary.colors.filter(name => !nonContainers.has(name)); const tables = { Web: [between(web, 'let foreground_tokens =', 'let space_tokens'), /"([^"]+)",\s*"([^"]+)"/g], @@ -52,6 +52,9 @@ test('container colors have admitted foreground counterparts rather than raw fil }; for (const [backend, [source, pattern]] of Object.entries(tables)) { const pairs = new Map([...source.matchAll(pattern)].map(m => [m[1], m[2]])); + for (const name of ['primary', 'accent']) { + assert.equal(pairs.get(name), undefined, `${backend} keeps ${name} as accent ink`); + } const admitted = colorTables[backend === 'Native' ? 'NativeLight' : backend]; for (const name of containers) { const target = pairs.get(name); From 42bc5b3785e29b9f59e0b0a54122cb755a13c79a Mon Sep 17 00:00:00 2001 From: Tienson Qin Date: Thu, 19 Nov 2026 13:57:19 +0000 Subject: [PATCH 2/2] fix: separate Web accent ink from neutral fills --- docs/api-migrations.md | 11 +++++- platform/web/melange/core/lui_web_util.ml | 3 +- platform/web/src/lui.css | 4 +++ platform/web/test/design-tokens.test.mjs | 44 ++++++++++++++++++++++- 4 files changed, 59 insertions(+), 3 deletions(-) diff --git a/docs/api-migrations.md b/docs/api-migrations.md index 372e3a30c..e8b001b97 100644 --- a/docs/api-migrations.md +++ b/docs/api-migrations.md @@ -6,7 +6,7 @@ they are not remapped to `primary-foreground` or `accent-foreground`. This restores pre-ADR-0007 heading and icon contrast, including Android light themes where `onPrimary` is white. Theme overrides for `primary` and `accent` -continue to select that ink. Explicit `*-foreground` values remain available +continue to select that ink on native backends. Explicit `*-foreground` values remain available for content painted on a corresponding filled control. Container and surface tokens such as `surface`, `card`, `secondary` and @@ -16,6 +16,15 @@ accent-colored heading, and use `~background:"primary"` with `~foreground:"primary-foreground"` for text on a primary fill. Background and border resolution is unchanged on all backends. +Web separates accent ink from its neutral accent fill: `foreground:"accent"` +uses `--lui-accent-ink` (`#1d4ed8` in light mode, `#93c5fd` in dark mode, +including system dark mode). These defaults meet 4.5:1 contrast against +background, secondary and accent fills. The fill tokens are unchanged; +`foreground:"primary"` still uses primary ink. Web themes can customize +`--lui-accent-ink` on their `[data-kb-theme]` scope independently of +`--lui-accent`. For example, use `--lui-accent-ink: #1d4ed8` for blue text +while keeping a pale neutral accent fill. + ### Dynamic platform tweak attachment order Anchorless dynamic segments now reconcile child edges from descendants to diff --git a/platform/web/melange/core/lui_web_util.ml b/platform/web/melange/core/lui_web_util.ml index 65b337728..f37fc697e 100644 --- a/platform/web/melange/core/lui_web_util.ml +++ b/platform/web/melange/core/lui_web_util.ml @@ -534,7 +534,8 @@ let radius_tokens = [ "none", 0; "sm", 4; "md", 6; "lg", 8; "xl", 12; "full", 99 let web_color_value ?(foreground = false) color = let color = if foreground then Option.value ~default:color (List.assoc_opt color foreground_tokens) else color in - if color = "transparent" then "transparent" + if foreground && color = "accent" then "var(--color-accent-ink)" + else if color = "transparent" then "transparent" (* Material names (Apple glass/bar surfaces) have no CSS equivalent; approximate with a translucent theme surface. *) else if color = "glass" || color = "glass-container" || color = "bar" then diff --git a/platform/web/src/lui.css b/platform/web/src/lui.css index 7c522abbc..c7d8f9779 100644 --- a/platform/web/src/lui.css +++ b/platform/web/src/lui.css @@ -20,6 +20,7 @@ } :root, .dark, [data-kb-theme] { + --color-accent-ink: var(--lui-accent-ink); --lui-surface: var(--lui-secondary); --lui-surface-raised: var(--lui-card); --lui-muted: var(--lui-secondary); @@ -69,6 +70,7 @@ --lui-secondary: oklch(0.97 0 0); --lui-secondary-foreground: oklch(0.205 0 0); --lui-accent: oklch(0.97 0 0); + --lui-accent-ink: #1d4ed8; --lui-accent-foreground: oklch(0.205 0 0); --lui-muted-foreground: oklch(0.556 0 0); --lui-destructive: oklch(0.577 0.245 27.325); @@ -98,6 +100,7 @@ --lui-secondary: oklch(0.269 0 0); --lui-secondary-foreground: oklch(0.985 0 0); --lui-accent: oklch(0.269 0 0); + --lui-accent-ink: #93c5fd; --lui-accent-foreground: oklch(0.985 0 0); --lui-muted-foreground: oklch(0.708 0 0); --lui-destructive: oklch(0.396 0.141 25.723); @@ -127,6 +130,7 @@ --lui-secondary: oklch(0.269 0 0); --lui-secondary-foreground: oklch(0.985 0 0); --lui-accent: oklch(0.269 0 0); + --lui-accent-ink: #93c5fd; --lui-accent-foreground: oklch(0.985 0 0); --lui-muted-foreground: oklch(0.708 0 0); --lui-destructive: oklch(0.396 0.141 25.723); diff --git a/platform/web/test/design-tokens.test.mjs b/platform/web/test/design-tokens.test.mjs index 2d2dc6d58..cafb95d1b 100644 --- a/platform/web/test/design-tokens.test.mjs +++ b/platform/web/test/design-tokens.test.mjs @@ -1,6 +1,7 @@ import assert from 'node:assert/strict'; import {readFileSync} from 'node:fs'; import test from 'node:test'; +import {chromium} from 'playwright'; const build = process.env.LUI_TEST_BUILD_DIR ?? '_build'; const Util = await import(new URL(`../../../${build}/default/examples/components/web/lui-components-web/node_modules/lui.web.dom/core/lui_web_util.js`, import.meta.url)); @@ -14,14 +15,55 @@ test('canonical colors resolve as CSS values and container foregrounds resolve a assert.equal(Util.web_color_value(true, 'secondary'), 'var(--color-secondary-foreground)'); assert.equal(Util.web_color_value(undefined, 'secondary'), 'var(--color-secondary)'); assert.equal(Util.web_color_value(true, 'surface'), 'var(--color-surface-foreground)'); - for (const color of ['primary', 'accent', 'primary-foreground', 'accent-foreground']) { + for (const color of ['primary', 'primary-foreground', 'accent-foreground']) { assert.equal(Util.web_color_value(true, color), `var(--color-${color})`); } + assert.equal(Util.web_color_value(true, 'accent'), 'var(--color-accent-ink)'); + assert.equal(Util.web_color_value(false, 'accent'), 'var(--color-accent)'); assert.equal(Util.web_color_value(true, '#123456'), '#123456'); assert.equal(Util.web_color_value(undefined, ''), ''); assert.equal(Util.web_color_value(undefined, 'custom-token'), 'custom-token'); }); +test('accent ink contrasts with unchanged background, secondary and accent fills', async () => { + const browser = await chromium.launch({headless: true}); + try { + const page = await browser.newPage(); + const css = readFileSync(new URL('../dist/lui.css', import.meta.url), 'utf8'); + for (const mode of ['light', 'dark', 'system']) { + await page.emulateMedia({colorScheme: mode === 'light' ? 'light' : 'dark'}); + await page.setContent(`
`); + const results = await page.evaluate(ink => { + const scope = document.querySelector('section'); + const canvas = document.createElement('canvas').getContext('2d', {willReadFrequently: true}); + const luminance = color => { + canvas.fillStyle = color; + canvas.fillRect(0, 0, 1, 1); + return [...canvas.getImageData(0, 0, 1, 1).data].slice(0, 3) + .map(value => value / 255) + .map(value => value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4) + .reduce((sum, value, index) => sum + value * [0.2126, 0.7152, 0.0722][index], 0); + }; + return ['background', 'secondary', 'accent'].map(fill => { + scope.style.color = ink; + scope.style.backgroundColor = `var(--color-${fill})`; + const style = getComputedStyle(scope); + const a = luminance(style.color), b = luminance(style.backgroundColor); + return {fill, background: style.backgroundColor, + contrast: (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05)}; + }); + }, Util.web_color_value(true, 'accent')); + assert.equal(results[1].background, results[2].background, 'accent fill stays secondary'); + for (const {fill, contrast} of results) { + console.log(`${mode} accent ink on ${fill}: ${contrast.toFixed(2)}:1`); + assert.ok(contrast >= 4.5, `${mode} ${fill}: ${contrast}`); + } + } + } finally { + await browser.close(); + } +}); + test('built CSS retains semantic typography and single-line truncation', () => { const css = readFileSync(new URL('../dist/lui.css', import.meta.url), 'utf8'); for (const name of [...vocabulary.textRoles, ...vocabulary.textModifiers]) {