opencode icon indicating copy to clipboard operation
opencode copied to clipboard

@opencode/ui: v2-text-text-faint and light v2-state-fg-warning fall below WCAG AA 4.5:1 contrast

Open Hona opened this issue 1 week ago • 3 comments

In @opencode/ui's v2 tokens (packages/ui/src/styles/tokens/colors.css and packages/ui/src/styles/tokens/theme.css, plus packages/ui/src/theme/v2/default-primitives.ts and packages/ui/src/theme/v2/mapping.ts), three text colour pairings fall below the WCAG 2.1 AA 4.5:1 contrast target for normal-sized text:

  1. Light --v2-text-text-faint (var(--v2-grey-600), #808080)

    • on --v2-background-bg-base (#ffffff): 3.95:1
    • on --v2-background-bg-layer-01 (#fafafa): 3.78:1
    • on --v2-background-bg-layer-02 (#f2f2f2): 3.53:1
    • on --v2-background-bg-layer-03 (#eeeeee): 3.40:1
  2. Dark --v2-text-text-faint (var(--v2-grey-600), #808080)

    • on --v2-background-bg-base (#161616): 4.58:1 (passes on bg-base)
    • on --v2-background-bg-layer-01 (#242424): 3.93:1
    • on --v2-background-bg-layer-02 (#2e2e2e): 3.44:1
    • on --v2-background-bg-layer-03 (#3a3a3a): 2.88:1
  3. Light --v2-state-fg-warning (var(--v2-yellow-800), #cb9f34)

    • on --v2-state-bg-warning (var(--v2-yellow-100), #fefaec): 2.35:1
    • on --v2-background-bg-base (#ffffff): 2.46:1
    • on --v2-background-bg-layer-01 (#fafafa): 2.35:1
    • on --v2-background-bg-layer-02 (#f2f2f2): 2.19:1

All ratios are computed with the WCAG 2.1 relative-luminance formula. Raising --v2-text-text-faint (light and dark) and light --v2-state-fg-warning to meet the 4.5:1 AA target on their intended surfaces will bring both @opencode/ui and downstream themes that mirror these tokens (such as rune-theme-opencode) up to AA contrast.

Context: https://github.com/anomalyco/rune/issues/289

Hona avatar Nov 23 '26 23:11 Hona

Not observed; this looks likely from reading the code.

I checked the token values on the v2.2.7 tag and on the latest dev (7599851). All 12 ratios in the issue come out exactly as you listed them.

The values come from the source: grey-600 is #808080 and yellow-800 is #cb9f34 in packages/ui/src/styles/tokens/colors.css and default-primitives.ts. theme.css assigns them to --v2-text-text-faint in the light :root block and the [data-color-scheme="dark"] block, and to light --v2-state-fg-warning. services/site/DESIGN.md sets the 4.5:1 minimum for text.

I got these ratios by reading the token files, not from the running app: the released 2.2.7 CLI binary doesn't contain these CSS tokens. The script below reads the token files from a checkout and prints each ratio (python3 contrast.py <repo-root>).

Some details for whoever fixes this:

  • The prefers-color-scheme dark block in theme.css is commented out, so the explicit [data-color-scheme="dark"] block is the one in effect.
  • packages/ui/src/theme/v2/mapping.ts has no v2-text-text-faint entry, and its background mapping differs from the CSS. A fix probably needs to update the CSS and the TS mapping/primitives together.
  • --v2-icon-icon-muted also uses grey-600. Icons only need 3:1, so it may be fine as it is.
Reproduction script (contrast.py)
#!/usr/bin/env python3
"""Compute WCAG 2.1 contrast for @opencode/ui v2 token pairs, reading values from the checkout's CSS."""
import re, sys
root = sys.argv[1] if len(sys.argv) > 1 else "/workspace/repo"
colors = open(f"{root}/packages/ui/src/styles/tokens/colors.css").read()
theme = open(f"{root}/packages/ui/src/styles/tokens/theme.css").read()
prim = dict(re.findall(r"--(v2-[a-z]+-\d+):\s*(#[0-9a-fA-F]{6})", colors))
def tokens(block):
    return {k: prim[v] for k, v in re.findall(r"--(v2-[\w-]+):\s*var\(--(v2-[a-z]+-\d+)\)", block) if v in prim}
def lum(h):
    c = [int(h[i:i+2], 16) / 255 for i in (1, 3, 5)]
    c = [x / 12.92 if x <= 0.03928 else ((x + 0.055) / 1.055) ** 2.4 for x in c]
    return 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]
def ratio(a, b):
    la, lb = sorted([lum(a), lum(b)], reverse=True)
    return (la + 0.05) / (lb + 0.05)
# active blocks: the :root defaults (light) and [data-color-scheme="dark"]
light_block = theme[: theme.index("/* OS preference fallback")]
dark_start = theme.index('[data-color-scheme="dark"]')
dark_block = theme[dark_start: theme.index("}", dark_start)]
for name, blk, fg, bgs in [
    ("light", light_block, "v2-text-text-faint", ["v2-background-bg-base", "v2-background-bg-layer-01", "v2-background-bg-layer-02", "v2-background-bg-layer-03"]),
    ("dark [data-color-scheme]", dark_block, "v2-text-text-faint", ["v2-background-bg-base", "v2-background-bg-layer-01", "v2-background-bg-layer-02", "v2-background-bg-layer-03"]),
    ("light", light_block, "v2-state-fg-warning", ["v2-state-bg-warning", "v2-background-bg-base", "v2-background-bg-layer-01", "v2-background-bg-layer-02"]),
]:
    t = tokens(blk)
    for bg in bgs:
        r = ratio(t[fg], t[bg])
        print(f"{name:28} {fg} {t[fg]} on {bg} {t[bg]}: {r:.2f}:1 {'PASS' if r >= 4.5 else 'FAIL'} (AA 4.5)")

Run it: python3 contrast.py

opencodejr[bot] avatar Nov 23 '26 23:11 opencodejr[bot]

I would like to work on this. I will check where the faint-text and warning foreground tokens are used, then verify the updated contrast ratios on the affected light and dark backgrounds. Could a maintainer assign this to me?

KhadeerBasha1232 avatar Nov 24 '26 08:11 KhadeerBasha1232

I saw #66094 is handling the warning text. I'm happy to take the faint text part in light and dark mode. Should faint and muted stay different colors?

KhadeerBasha1232 avatar Nov 24 '26 17:11 KhadeerBasha1232