Skip to content

Commit d88743d

Browse files
committed
fix(providers): badge initials stay readable on light accent colors
Instance badges always drew white initials, so a light accent such as lime hid them. Pick black or white text by WCAG contrast against the accent in a shared client-runtime helper, used by web and mobile.
1 parent 51f7f77 commit d88743d

4 files changed

Lines changed: 51 additions & 5 deletions

File tree

‎apps/mobile/src/components/ProviderIcon.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { Image } from "expo-image";
22
import { Path, Svg } from "react-native-svg";
33
import { View } from "react-native";
4-
import { providerInstanceInitials } from "@t3tools/client-runtime/state/provider-instance-display";
4+
import {
5+
providerAccentForegroundColor,
6+
providerInstanceInitials,
7+
} from "@t3tools/client-runtime/state/provider-instance-display";
58
import { useState } from "react";
69
import { resolveOfficialAcpRegistryIconUrl } from "@t3tools/contracts";
710
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
@@ -200,7 +203,9 @@ export function ProviderInstanceIcon(props: {
200203
fontSize: 7,
201204
fontWeight: "600",
202205
lineHeight: 9,
203-
color: props.accentColor ? "#ffffff" : undefined,
206+
color: props.accentColor
207+
? providerAccentForegroundColor(props.accentColor)
208+
: undefined,
204209
}}
205210
>
206211
{providerInstanceInitials(props.displayName)}

‎apps/web/src/components/chat/ProviderInstanceIcon.tsx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { type CSSProperties, memo } from "react";
22

3-
import { providerInstanceInitials } from "@t3tools/client-runtime/state/provider-instance-display";
3+
import {
4+
providerAccentForegroundColor,
5+
providerInstanceInitials,
6+
} from "@t3tools/client-runtime/state/provider-instance-display";
47

58
import { ProviderDriverKind } from "@t3tools/contracts";
69
import {
@@ -74,7 +77,10 @@ export const ProviderInstanceIcon = memo(function ProviderInstanceIcon(props: {
7477
const Icon = PROVIDER_ICON_BY_PROVIDER[props.driverKind] ?? null;
7578
const indicatorBackground = props.indicatorBackground ?? "var(--card)";
7679
const accentStyle = props.accentColor
77-
? ({ "--provider-accent": props.accentColor } as CSSProperties)
80+
? ({
81+
"--provider-accent": props.accentColor,
82+
"--provider-accent-foreground": providerAccentForegroundColor(props.accentColor),
83+
} as CSSProperties)
7884
: undefined;
7985
const badgeContent = props.badgeContent ?? "initials";
8086
const isAcpRegistry = props.driverKind === "acpRegistry";
@@ -123,7 +129,7 @@ export const ProviderInstanceIcon = memo(function ProviderInstanceIcon(props: {
123129
className={cn(
124130
"pointer-events-none absolute right-0 bottom-0 z-10 flex h-3.5 min-w-3.5 items-center justify-center rounded-full border px-0.5 text-4xs font-semibold leading-none shadow-sm",
125131
props.accentColor
126-
? "bg-(--provider-accent) text-white"
132+
? "bg-(--provider-accent) text-(--provider-accent-foreground)"
127133
: "bg-card text-muted-foreground",
128134
props.badgeClassName,
129135
)}

‎packages/client-runtime/src/state/providerInstanceDisplay.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it } from "vite-plus/test";
33

44
import {
55
normalizeProviderAccentColor,
6+
providerAccentForegroundColor,
67
providerInstanceInitials,
78
resolveProviderInstanceDisplayName,
89
shouldShowInstanceBadge,
@@ -89,6 +90,22 @@ describe("normalizeProviderAccentColor", () => {
8990
});
9091
});
9192

93+
describe("providerAccentForegroundColor", () => {
94+
it("uses dark text on light accents", () => {
95+
expect(providerAccentForegroundColor("#b5f23d")).toBe("#000000");
96+
expect(providerAccentForegroundColor("#FFFFFF")).toBe("#000000");
97+
});
98+
99+
it("uses light text on dark accents", () => {
100+
expect(providerAccentForegroundColor("#2563eb")).toBe("#ffffff");
101+
expect(providerAccentForegroundColor("#000000")).toBe("#ffffff");
102+
});
103+
104+
it("keeps light text when the accent is invalid", () => {
105+
expect(providerAccentForegroundColor("blue")).toBe("#ffffff");
106+
});
107+
});
108+
92109
describe("shouldShowInstanceBadge", () => {
93110
it("hides badges for distinct ACP agents sharing the registry driver", () => {
94111
const mistral = { driverKind: acpRegistry, acpRegistryAgentId: "mistral-vibe" };

‎packages/client-runtime/src/state/providerInstanceDisplay.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ export function normalizeProviderAccentColor(value: string | undefined): string
7070
return /^#[0-9a-fA-F]{6}$/u.test(trimmed) ? trimmed : undefined;
7171
}
7272

73+
/**
74+
* Text color for initials drawn on an accent-color badge: black or white,
75+
* whichever has the higher WCAG contrast against the accent, so light accents
76+
* stay readable. An invalid accent keeps the white default.
77+
*/
78+
export function providerAccentForegroundColor(accentColor: string): "#000000" | "#ffffff" {
79+
const hex = normalizeProviderAccentColor(accentColor);
80+
if (!hex) return "#ffffff";
81+
const linearChannel = (offset: number) => {
82+
const value = Number.parseInt(hex.slice(offset, offset + 2), 16) / 255;
83+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
84+
};
85+
const luminance =
86+
0.2126 * linearChannel(1) + 0.7152 * linearChannel(3) + 0.0722 * linearChannel(5);
87+
// Black wins when (L + 0.05) / 0.05 exceeds 1.05 / (L + 0.05).
88+
return (luminance + 0.05) ** 2 > 0.0525 ? "#000000" : "#ffffff";
89+
}
90+
7391
/**
7492
* Whether an instance's icon carries the account badge: accent color set, or
7593
* several instances sharing a provider so the brand glyph alone is ambiguous.

0 commit comments

Comments
 (0)