Skip to content

Commit 4f5f5a1

Browse files
ipanasenkoclaudejuliusmarminge
authored
fix(providers): badge initials stay readable on light accent colors (pingdotgg#16128)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 78c6de4 commit 4f5f5a1

4 files changed

Lines changed: 47 additions & 11 deletions

File tree

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { Image } from "expo-image";
22
import { Defs, LinearGradient, Path, Stop, 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+
normalizeProviderAccentColor,
6+
providerAccentForegroundColor,
7+
providerInstanceInitials,
8+
} from "@t3tools/client-runtime/state/provider-instance-display";
59
import { useId, useState } from "react";
610
import { resolveOfficialAcpRegistryIconUrl } from "@t3tools/contracts";
711
import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
@@ -156,6 +160,7 @@ export function ProviderInstanceIcon(props: {
156160
readonly showBadge?: boolean;
157161
readonly surfaceColor: string;
158162
}) {
163+
const accentColor = normalizeProviderAccentColor(props.accentColor);
159164
return (
160165
<View style={{ position: "relative" }}>
161166
<View style={{ opacity: 0.6 }}>
@@ -169,26 +174,26 @@ export function ProviderInstanceIcon(props: {
169174
style={{ position: "absolute", right: -3, bottom: -3, width: 32, alignItems: "flex-end" }}
170175
>
171176
<View
172-
className={props.accentColor ? undefined : "bg-card"}
177+
className={accentColor ? undefined : "bg-card"}
173178
style={{
174179
height: 12,
175180
minWidth: 12,
176181
paddingHorizontal: 2,
177182
borderRadius: 999,
178183
borderWidth: 1,
179184
borderColor: props.surfaceColor,
180-
backgroundColor: props.accentColor,
185+
backgroundColor: accentColor,
181186
alignItems: "center",
182187
justifyContent: "center",
183188
}}
184189
>
185190
<Text
186-
className={props.accentColor ? undefined : "text-foreground-muted"}
191+
className={accentColor ? undefined : "text-foreground-muted"}
187192
style={{
188193
fontSize: 7,
189194
fontWeight: "600",
190195
lineHeight: 9,
191-
color: props.accentColor ? "#ffffff" : undefined,
196+
color: accentColor ? providerAccentForegroundColor(accentColor) : undefined,
192197
}}
193198
>
194199
{providerInstanceInitials(props.displayName)}

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

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

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

59
import { ProviderDriverKind } from "@t3tools/contracts";
610
import { AntigravityIcon, ClaudeAI, Icon, OpenAI } from "../Icons";
@@ -71,8 +75,12 @@ export const ProviderInstanceIcon = memo(function ProviderInstanceIcon(props: {
7175
const Icon = PROVIDER_ICON_BY_PROVIDER[props.driverKind] ?? null;
7276
const packageIcon = providerClients.get(props.driverKind)?.icon;
7377
const indicatorBackground = props.indicatorBackground ?? "var(--card)";
74-
const accentStyle = props.accentColor
75-
? ({ "--provider-accent": props.accentColor } as CSSProperties)
78+
const accentColor = normalizeProviderAccentColor(props.accentColor);
79+
const accentStyle = accentColor
80+
? ({
81+
"--provider-accent": accentColor,
82+
"--provider-accent-foreground": providerAccentForegroundColor(accentColor),
83+
} as CSSProperties)
7684
: undefined;
7785
const badgeContent = props.badgeContent ?? "initials";
7886
const iconClassName = cn(props.size === "compact" && "size-3.5", props.iconClassName);
@@ -90,7 +98,7 @@ export const ProviderInstanceIcon = memo(function ProviderInstanceIcon(props: {
9098
props.className,
9199
)}
92100
style={accentStyle}
93-
data-provider-accent-color={props.accentColor}
101+
data-provider-accent-color={accentColor}
94102
>
95103
{isAcpRegistry ? (
96104
<AcpRegistryAgentIcon
@@ -125,8 +133,8 @@ export const ProviderInstanceIcon = memo(function ProviderInstanceIcon(props: {
125133
<span
126134
className={cn(
127135
"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",
128-
props.accentColor
129-
? "bg-(--provider-accent) text-white"
136+
accentColor
137+
? "bg-(--provider-accent) text-(--provider-accent-foreground)"
130138
: "bg-card text-muted-foreground",
131139
props.size === "compact" &&
132140
"right-[-0.1875rem] bottom-[-0.1875rem] h-3 min-w-3 px-0.5 text-5xs",

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

Lines changed: 11 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,16 @@ describe("normalizeProviderAccentColor", () => {
8990
});
9091
});
9192

93+
describe("providerAccentForegroundColor", () => {
94+
it.each([
95+
["#b5f23d", "#000000"],
96+
["#2563eb", "#ffffff"],
97+
["#777777", "#000000"],
98+
])("chooses the higher WCAG contrast foreground for %s", (accent, foreground) => {
99+
expect(providerAccentForegroundColor(accent)).toBe(foreground);
100+
});
101+
});
102+
92103
describe("shouldShowInstanceBadge", () => {
93104
it("hides badges for distinct ACP agents sharing the registry driver", () => {
94105
const mistral = { driverKind: acpRegistry, acpRegistryAgentId: "mistral-vibe", enabled: true };

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

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

45+
export function providerAccentForegroundColor(accentColor: string) {
46+
const linearChannel = (offset: number) => {
47+
const value = Number.parseInt(accentColor.slice(offset, offset + 2), 16) / 255;
48+
return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4;
49+
};
50+
const luminance =
51+
0.2126 * linearChannel(1) + 0.7152 * linearChannel(3) + 0.0722 * linearChannel(5);
52+
const blackContrast = (luminance + 0.05) / 0.05;
53+
const whiteContrast = 1.05 / (luminance + 0.05);
54+
return blackContrast >= whiteContrast ? "#000000" : "#ffffff";
55+
}
56+
4557
export function shouldShowInstanceBadge(
4658
entry: {
4759
readonly driverKind: ProviderDriverKind;

0 commit comments

Comments
 (0)