AI-native design editor. Open-source Figma alternative.
fix(presence): darken the follow bar until its white label reads at AA (#1124)
Danila Poyarkovecd3b401af93
diff --git a/changes/1124/fixed.md b/changes/1124/fixed.mdnew file mode 100644index 000000000..69725f6de--- /dev/null+++ b/changes/1124/fixed.md@@ -0,0 +1 @@+- The bar that shows who you follow has a white label on the person's or agent's color, darkened just enough to read at WCAG AA, instead of black text on mid-tone colors such as the AI agent's blue.diff --git a/packages/scene-graph/src/color/contrast.ts b/packages/scene-graph/src/color/contrast.tsindex 7e6e956e4..d66822013 100644--- a/packages/scene-graph/src/color/contrast.ts+++ b/packages/scene-graph/src/color/contrast.ts@@ -25,6 +25,34 @@ export function contrastRatio()}+/**+ * `background` darkened just enough for light `text` to reach `ratio` on it, by scaling its+ * channels toward black, which keeps its hue; returned as it is when the text already does.+ * The default is white text at WCAG AA's 4.5:1 for normal text.+ */+export function darkenForContrast(+ background: Color,+ text: Pick<Color, 'r' | 'g' | 'b'> = WHITE,+ ratio = 4.5+): Color {+ if (contrastRatio(background, text) >= ratio) return background+ const scaled = (factor: number): Color => ({+ ...background,+ r: background.r * factor,+ g: background.g * factor,+ b: background.b * factor+ })+ // Darker always contrasts more with light text, so the smallest darkening is found by halving.+ let enough = 0+ let short = 1+ for (let step = 0; step < 20; step++) {+ const middle = (enough + short) / 2+ if (contrastRatio(scaled(middle), text) >= ratio) enough = middle+ else short = middle+ }+ return scaled(enough)+}+/** Black or white, whichever reads better on an opaque `background`. */export function readableForeground(background: Pick<Color, 'r' | 'g' | 'b'>): Color {return contrastRatio(background, BLACK) >= contrastRatio(background, WHITE) ? BLACK : WHITEdiff --git a/packages/scene-graph/tests/color/contrast.test.ts b/packages/scene-graph/tests/color/contrast.test.tsindex b4fa093b9..739595e93 100644--- a/packages/scene-graph/tests/color/contrast.test.ts+++ b/packages/scene-graph/tests/color/contrast.test.ts@@ -1,6 +1,11 @@import { describe, expect, test } from 'bun:test'-import { compositeOver, contrastRatio, readableForeground } from '@open-pencil/scene-graph/color'+import {+ compositeOver,+ contrastRatio,+ darkenForContrast,+ readableForeground+} from '@open-pencil/scene-graph/color'import type { Color } from '@open-pencil/scene-graph/primitives'const BLACK: Color = { r: 0, g: 0, b: 0, a: 1 }@@ -39,3 +44,27 @@ describe('readableForeground', () => {expect(readableForeground(GRAY_94)).toEqual(BLACK)})})++describe('darkenForContrast', () => {+ const amber: Color = { r: 1, g: 0.76, b: 0.03, a: 1 }++ test('darkens just enough for white text to reach 4.5:1, keeping the hue', () => {+ const darkened = darkenForContrast(amber)+ expect(contrastRatio(darkened, WHITE)).toBeGreaterThanOrEqual(4.5)+ expect(contrastRatio(darkened, WHITE)).toBeLessThan(4.51)+ // Every channel scaled by one factor keeps their proportions, and so the hue.+ expect(darkened.g / darkened.r).toBeCloseTo(amber.g / amber.r, 6)+ expect(darkened.b / darkened.r).toBeCloseTo(amber.b / amber.r, 6)+ expect(darkened.a).toBe(1)+ })++ test('leaves a color that already reaches the ratio as it is', () => {+ const deep: Color = { r: 0.2, g: 0.1, b: 0.5, a: 1 }+ expect(darkenForContrast(deep)).toBe(deep)+ })++ test('takes another text color and ratio', () => {+ const darkened = darkenForContrast(amber, GRAY_94, 3)+ expect(contrastRatio(darkened, GRAY_94)).toBeGreaterThanOrEqual(3)+ })+})diff --git a/src/components/presence/FollowFrame.vue b/src/components/presence/FollowFrame.vueindex dc7bc743e..0e0a41f64 100644--- a/src/components/presence/FollowFrame.vue+++ b/src/components/presence/FollowFrame.vue@@ -2,7 +2,7 @@import { useEventListener } from '@vueuse/core'import { computed } from 'vue'-import { colorToCSS, readableForeground } from '@open-pencil/scene-graph/color'+import { colorToCSS, darkenForContrast } from '@open-pencil/scene-graph/color'import { useI18n } from '@open-pencil/vue'import type { FollowedLabel } from '@/app/presence/registry'@@ -15,9 +15,10 @@ const emit = defineEmits<{ stop: [] }>()const { collaboration: messages } = useI18n()const ui = followFrame()-const color = computed(() => colorToCSS(followed.color))-// People's colors range from pale to deep, so the bar's text takes whichever reads on it.-const foreground = computed(() => colorToCSS(readableForeground(followed.color)))+// People's colors range from pale to deep, so the frame takes theirs darkened just enough for+// the bar's white label to read at WCAG AA, keeping its hue. A peer's color can arrive with+// alpha; the bar is drawn opaque, so the contrast holds over any canvas.+const color = computed(() => colorToCSS(darkenForContrast({ ...followed.color, a: 1 })))const text = computed(() =>followed.owner? messages.value.followingAgent({ agent: followed.name, owner: followed.owner })@@ -32,7 +33,7 @@ useEventListener(window, 'keydown', (event: KeyboardEvent) => {<template><div data-test-id="follow-frame" :class="ui.root()" :style="{ borderColor: color }">- <div role="status" :class="ui.bar()" :style="{ background: color, color: foreground }">+ <div role="status" :class="ui.bar()" :style="{ background: color }"><icon-lucide-sparkle v-if="followed.kind === 'agent'" :class="ui.icon()" /><span :class="ui.label()">{{ text }}</span><button type="button" data-test-id="follow-stop" :class="ui.stop()" @click="emit('stop')">diff --git a/src/theme/collaboration/follow-frame.ts b/src/theme/collaboration/follow-frame.tsindex c60b65ed3..e6976eab0 100644--- a/src/theme/collaboration/follow-frame.ts+++ b/src/theme/collaboration/follow-frame.ts@@ -4,9 +4,9 @@ import { tv } from 'tailwind-variants'export const followFrame = tv({slots: {root: 'pointer-events-none absolute inset-0 z-30 border-2',- bar: 'pointer-events-auto absolute top-0 left-1/2 flex max-w-[calc(100%-1rem)] -translate-x-1/2 items-center gap-1.5 rounded-b-md py-1 pr-1 pl-2.5 text-[11px] font-medium shadow-md',+ bar: 'pointer-events-auto absolute top-0 left-1/2 flex max-w-[calc(100%-1rem)] -translate-x-1/2 items-center gap-1.5 rounded-b-md py-1 pr-1 pl-2.5 text-[11px] font-medium text-white shadow-md',icon: 'size-3 shrink-0',label: 'min-w-0 truncate',- stop: 'shrink-0 cursor-pointer rounded bg-current/15 px-1.5 py-0.5 text-[11px] font-medium outline-none hover:bg-current/25 focus-visible:ring-1 focus-visible:ring-current'+ stop: 'ml-1.5 shrink-0 cursor-pointer rounded bg-black/15 px-1.5 py-0.5 text-[11px] font-medium outline-none hover:bg-black/25 focus-visible:ring-1 focus-visible:ring-current'}})