Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(webv2): keep gallery view counts legible on the selected segment

joshistoast7c5568c4aa83
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.browser.test.tsx b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.browser.test.tsx
new file mode 100644
index 0000000000..8dbf43c571
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.browser.test.tsx
@@ -0,0 +1,117 @@
+/* oxlint-disable react-perf/jsx-no-new-object-as-prop */
+import { ChakraProvider } from '@chakra-ui/react';
+import { system } from '@theme/system';
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, describe, expect, it, vi } from 'vitest';
+
+import type { GalleryWidgetContextValue } from './GalleryWidgetContext';
+
+import { GalleryViewTabs } from './GalleryViewTabs';
+import { GalleryWidgetContext } from './GalleryWidgetContext';
+
+let host: HTMLDivElement | null = null;
+let root: Root | null = null;
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+afterEach(async () => {
+ await act(() => root?.unmount());
+ host?.remove();
+ host = null;
+ root = null;
+});
+
+const toRgb = (color: string): [number, number, number] => {
+ const context = document.createElement('canvas').getContext('2d')!;
+
+ context.fillStyle = color;
+ context.fillRect(0, 0, 1, 1);
+
+ const [red, green, blue] = context.getImageData(0, 0, 1, 1).data;
+
+ return [red!, green!, blue!];
+};
+
+const getRelativeLuminance = ([red, green, blue]: [number, number, number]): number => {
+ const linearize = (channel: number): number => {
+ const value = channel / 255;
+
+ return value <= 0.03928 ? value / 12.92 : Math.pow((value + 0.055) / 1.055, 2.4);
+ };
+
+ return 0.2126 * linearize(red) + 0.7152 * linearize(green) + 0.0722 * linearize(blue);
+};
+
+/** Contrast of `foreground` at `alpha` composited over `background`. */
+const getContrastRatio = (foreground: string, background: string, alpha: number): number => {
+ const backgroundRgb = toRgb(background);
+ const composited = toRgb(foreground).map((channel, index) =>
+ Math.round(channel * alpha + backgroundRgb[index]! * (1 - alpha))
+ ) as [number, number, number];
+ const [lighter, darker] = [getRelativeLuminance(composited), getRelativeLuminance(backgroundRgb)].sort(
+ (a, b) => b - a
+ );
+
+ return (lighter! + 0.05) / (darker! + 0.05);
+};
+
+const renderViewTabs = async (): Promise<{
+ counts: HTMLElement[];
+ indicator: HTMLElement;
+ items: HTMLElement[];
+}> => {
+ host = document.createElement('div');
+ document.body.append(host);
+ root = createRoot(host);
+
+ const value = {
+ actions: { setView: vi.fn() },
+ gallery: {
+ boards: [{ assetCount: 7, id: 'board-1', imageCount: 148, videoCount: 0 }],
+ galleryView: 'images',
+ selectedBoardId: 'board-1',
+ },
+ } as unknown as GalleryWidgetContextValue;
+
+ await act(() => {
+ root?.render(
+ <ChakraProvider value={system}>
+ <GalleryWidgetContext value={value}>
+ <GalleryViewTabs />
+ </GalleryWidgetContext>
+ </ChakraProvider>
+ );
+ });
+
+ const items = [...host.querySelectorAll<HTMLElement>('[data-part="item"]')];
+
+ return {
+ // The count is the only span in an item whose text is purely numeric.
+ counts: items.map((item) =>
+ [...item.querySelectorAll<HTMLElement>('span')].find((span) => /^\d+$/.test(span.textContent!.trim()))!
+ ),
+ indicator: host.querySelector<HTMLElement>('[data-part="indicator"]')!,
+ items,
+ };
+};
+
+describe('GalleryViewTabs', () => {
+ it('shows each view its own count', async () => {
+ const { counts } = await renderViewTabs();
+
+ expect(counts.map((count) => count.textContent)).toEqual(['148', '7']);
+ });
+
+ it('keeps the count readable on the selected segment', async () => {
+ const { counts, indicator, items } = await renderViewTabs();
+ const checkedIndex = items.findIndex((item) => item.dataset.state === 'checked');
+ const count = counts[checkedIndex]!;
+ const style = getComputedStyle(count);
+
+ // The selected segment is filled with `accent.solid`, so a count pinned to
+ // `fg.muted` (rather than dimmed from the item's own colour) is unreadable.
+ const ratio = getContrastRatio(style.color, getComputedStyle(indicator).backgroundColor, Number(style.opacity));
+
+ expect(ratio).toBeGreaterThanOrEqual(4.5);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.tsx b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.tsx
index 12c6bfdcef..7f39435b79 100644
--- a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.tsx
+++ b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.tsx
@@ -38,7 +38,7 @@ export const GalleryViewTabs = () => {
return (
<SegmentGroup.Root
aria-label={t('common.view')}
- size="sm"
+ size="xs"
value={gallery.galleryView}
onValueChange={handleViewChange}
>
@@ -52,7 +52,11 @@ export const GalleryViewTabs = () => {
<SegmentGroup.ItemText display="flex" fontSize="xs" gap="1.5">
{t(labelKey)}
{count === null ? null : (
- <Text as="span" color="fg.muted" fontVariantNumeric="tabular-nums">
+ // Dimmed from the item's own text colour rather than pinned to
+ // `fg.muted`: the checked item swaps to `accent.contrast`, and a
+ // fixed muted grey is unreadable on the accent fill. 0.8 is the
+ // dimmest that still clears 4.5:1 in both states.
+ <Text as="span" color="currentColor" fontVariantNumeric="tabular-nums" opacity="0.8">
{count}
</Text>
)}
diff --git a/invokeai/frontend/webv2/src/platform/ui/theme/SegmentGroupContrast.browser.test.tsx b/invokeai/frontend/webv2/src/platform/ui/theme/SegmentGroupContrast.browser.test.tsx
new file mode 100644
index 0000000000..89f93f2e80
--- /dev/null
+++ b/invokeai/frontend/webv2/src/platform/ui/theme/SegmentGroupContrast.browser.test.tsx
@@ -0,0 +1,107 @@
+import { ChakraProvider, SegmentGroup, Text } from '@chakra-ui/react';
+import { system } from '@theme/system';
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { afterEach, describe, expect, it } from 'vitest';
+
+let host: HTMLDivElement | null = null;
+let root: Root | null = null;
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+
+afterEach(async () => {
+ await act(() => root?.unmount());
+ host?.remove();
+ host = null;
+ root = null;
+});
+
+const toRgb = (color: string): [number, number, number] => {
+ const context = document.createElement('canvas').getContext('2d')!;
+
+ context.fillStyle = color;
+ context.fillRect(0, 0, 1, 1);
+
+ const [red, green, blue] = context.getImageData(0, 0, 1, 1).data;
+
+ return [red!, green!, blue!];
+};
+
+const getRelativeLuminance = ([red, green, blue]: [number, number, number]): number => {
+ const linearize = (channel: number): number => {
+ const value = channel / 255;
+
+ return value <= 0.03928 ? value / 12.92 : Math.pow((value + 0.055) / 1.055, 2.4);
+ };
+
+ return 0.2126 * linearize(red) + 0.7152 * linearize(green) + 0.0722 * linearize(blue);
+};
+
+const getContrastRatio = (foreground: string, background: string, alpha: number): number => {
+ const backgroundRgb = toRgb(background);
+ const foregroundRgb = toRgb(foreground);
+ // Opacity is applied by compositing, so measure the blended pixel.
+ const composited = foregroundRgb.map((channel, index) =>
+ Math.round(channel * alpha + backgroundRgb[index]! * (1 - alpha))
+ ) as [number, number, number];
+ const [lighter, darker] = [getRelativeLuminance(composited), getRelativeLuminance(backgroundRgb)].sort(
+ (a, b) => b - a
+ );
+
+ return (lighter! + 0.05) / (darker! + 0.05);
+};
+
+const renderSegments = async (): Promise<{ checked: HTMLElement; count: HTMLElement; indicator: HTMLElement }> => {
+ host = document.createElement('div');
+ document.body.append(host);
+ root = createRoot(host);
+
+ await act(() => {
+ root?.render(
+ <ChakraProvider value={system}>
+ <SegmentGroup.Root size="xs" value="media">
+ <SegmentGroup.Indicator data-testid="indicator" />
+ {['media', 'assets'].map((value) => (
+ <SegmentGroup.Item key={value} data-testid={value} value={value}>
+ <SegmentGroup.ItemHiddenInput />
+ <SegmentGroup.ItemText display="flex" gap="1.5">
+ {value}
+ <Text as="span" color="currentColor" data-testid={`${value}-count`} opacity="0.8">
+ 12
+ </Text>
+ </SegmentGroup.ItemText>
+ </SegmentGroup.Item>
+ ))}
+ </SegmentGroup.Root>
+ </ChakraProvider>
+ );
+ });
+
+ return {
+ checked: host.querySelector<HTMLElement>('[data-testid="media"]')!,
+ count: host.querySelector<HTMLElement>('[data-testid="media-count"]')!,
+ indicator: host.querySelector<HTMLElement>('[data-testid="indicator"]')!,
+ };
+};
+
+describe('segmentGroup checked contrast', () => {
+ // The indicator is a solid accent fill, so anything inside the checked item
+ // has to be readable against accent — not against the panel behind it.
+ it('renders the checked label against the accent fill at AA contrast', async () => {
+ const { checked, indicator } = await renderSegments();
+
+ const ratio = getContrastRatio(getComputedStyle(checked).color, getComputedStyle(indicator).backgroundColor, 1);
+
+ expect(ratio).toBeGreaterThanOrEqual(4.5);
+ });
+
+ it('keeps text that inherits the item colour readable when dimmed', async () => {
+ const { count, indicator } = await renderSegments();
+ const style = getComputedStyle(count);
+
+ // Secondary text inside a segment must dim from `currentColor`; pinning it
+ // to a fixed `fg.muted` grey drops to ~1.5:1 on the accent fill.
+ const ratio = getContrastRatio(style.color, getComputedStyle(indicator).backgroundColor, Number(style.opacity));
+
+ expect(ratio).toBeGreaterThanOrEqual(4.5);
+ });
+});