Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(glass): check the contrast worst case exactly, not just at white and black

iceclatterWT5949875709bd
diff --git a/src/renderer/lib/glassContrast.test.ts b/src/renderer/lib/glassContrast.test.ts
index dffdab2d9..5a5c99564 100644
--- a/src/renderer/lib/glassContrast.test.ts
+++ b/src/renderer/lib/glassContrast.test.ts
@@ -6,7 +6,8 @@ import {
composite,
contrastRatio,
glassTintAlpha,
- parseHex
+ parseHex,
+ worstContrast
} from './glassContrast'
const WHITE = [255, 255, 255] as const
@@ -46,6 +47,29 @@ describe('glassTintAlpha', () => {
expect(glassTintAlpha(light.theme.foreground!, light.theme.background!)).toBe(1)
})
+ it.each(TERMINAL_THEMES.map((t) => [t.id, t] as const))(
+ '%s: no grey backdrop (0..255 step 15) is worse than the white/black worst case',
+ (_id, t) => {
+ const fg = parseHex(t.theme.foreground!)!
+ const tint = parseHex(t.theme.background!)!
+ const a = glassTintAlpha(t.theme.foreground!, t.theme.background!)
+ const worst = worstContrast(fg, tint, a)
+ for (let g = 0; g <= 255; g += 15) {
+ expect(contrastRatio(fg, composite(tint, [g, g, g], a))).toBeGreaterThanOrEqual(worst - 1e-9)
+ }
+ }
+ )
+
+ it('a text colour inside the composite range is caught (some backdrop matches it)', () => {
+ // Mid-grey text on a black tint at 0.55: over black the composite is black, over white it is
+ // ~#737373, and #404040 sits between them — a grey backdrop exists that erases it.
+ const fg = parseHex('#404040')!
+ expect(worstContrast(fg, [0, 0, 0], 0.55)).toBe(1)
+ let min = Infinity
+ for (let g = 0; g <= 255; g++) min = Math.min(min, contrastRatio(fg, composite([0, 0, 0], [g, g, g], 0.55)))
+ expect(min).toBeLessThan(1.05)
+ })
+
it('every alpha above the answer passes too (non-monotonic contrast is handled)', () => {
const fg = parseHex('#d4d4d4')!
const tint = parseHex('#000000')!
diff --git a/src/renderer/lib/glassContrast.ts b/src/renderer/lib/glassContrast.ts
index 6038502f7..623ab39f8 100644
--- a/src/renderer/lib/glassContrast.ts
+++ b/src/renderer/lib/glassContrast.ts
@@ -3,9 +3,17 @@
*
* A glass node paints the terminal theme's background as a translucent tint over whatever is
* behind it (a wallpaper, the canvas), so the effective background of every glyph is
- * `tint·a + backdrop·(1−a)`. The backdrop is unknowable — any pixel of any wallpaper — so the alpha
- * is chosen against the two extremes: pure white and pure black. Every other backdrop pixel lies
- * between them channel-wise, and so does its composite.
+ * `tint·a + backdrop·(1−a)`. The backdrop is unknowable — any pixel of any wallpaper.
+ *
+ * Why checking pure white and pure black is enough: contrast depends only on luminance, and the
+ * composite's luminance rises monotonically with each backdrop channel, so EVERY backdrop's
+ * composite lies in the luminance interval [composite over black, composite over white]. Against a
+ * fixed text colour, contrast falls as the background's luminance approaches the text's and rises
+ * away from it — so over that interval the worst case is an endpoint, UNLESS the text's own
+ * luminance falls inside the interval, where some backdrop matches it exactly (ratio 1). That case
+ * is checked explicitly (`worstContrast`) and fails. For the built-in themes' foregrounds it never
+ * arises at a ≥ 0.55: the tint then dominates the composite, keeping the whole interval on the
+ * tint's side of the text. `glassContrast.test.ts` samples grey backdrops to pin the claim.
*
* Compositing is in gamma-encoded sRGB, the way the browser blends an rgba() fill; the WCAG
* relative luminance is computed on the result.
@@ -50,11 +58,17 @@ const WHITE: Rgb = [255, 255, 255]
const BLACK: Rgb = [0, 0, 0]
const STEP = 0.005
+/** The lowest contrast `fg` can have against the tint over ANY backdrop (see the module header). */
+export function worstContrast(fg: Rgb, tint: Rgb, alpha: number): number {
+ const overBlack = composite(tint, BLACK, alpha)
+ const overWhite = composite(tint, WHITE, alpha)
+ const l = relativeLuminance(fg)
+ if (l > relativeLuminance(overBlack) && l < relativeLuminance(overWhite)) return 1
+ return Math.min(contrastRatio(fg, overWhite), contrastRatio(fg, overBlack))
+}
+
function passes(fg: Rgb, tint: Rgb, alpha: number, minRatio: number): boolean {
- return (
- contrastRatio(fg, composite(tint, WHITE, alpha)) >= minRatio &&
- contrastRatio(fg, composite(tint, BLACK, alpha)) >= minRatio
- )
+ return worstContrast(fg, tint, alpha) >= minRatio
}
/**