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.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
refactor(webv2): share the WCAG contrast math between accessibility tests
joshistoast68683fdfeb9f
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.tsxindex 8dbf43c571..011025c20d 100644--- a/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.browser.test.tsx+++ b/invokeai/frontend/webv2/src/features/gallery/ui/GalleryViewTabs.browser.test.tsx@@ -1,5 +1,6 @@/* oxlint-disable react-perf/jsx-no-new-object-as-prop */import { ChakraProvider } from '@chakra-ui/react';+import { getContrastRatio } from '@platform/ui/theme/contrastRatio.testing';import { system } from '@theme/system';import { act } from 'react';import { createRoot, type Root } from 'react-dom/client';@@ -21,40 +22,6 @@ afterEach(async () => {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;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.tsxindex 89f93f2e80..154b55e58f 100644--- a/invokeai/frontend/webv2/src/platform/ui/theme/SegmentGroupContrast.browser.test.tsx+++ b/invokeai/frontend/webv2/src/platform/ui/theme/SegmentGroupContrast.browser.test.tsx@@ -4,6 +4,8 @@ import { act } from 'react';import { createRoot, type Root } from 'react-dom/client';import { afterEach, describe, expect, it } from 'vitest';+import { getContrastRatio } from './contrastRatio.testing';+let host: HTMLDivElement | null = null;let root: Root | null = null;(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;@@ -15,41 +17,6 @@ afterEach(async () => {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);diff --git a/invokeai/frontend/webv2/src/platform/ui/theme/contrastRatio.testing.ts b/invokeai/frontend/webv2/src/platform/ui/theme/contrastRatio.testing.tsnew file mode 100644index 0000000000..b1383e97bc--- /dev/null+++ b/invokeai/frontend/webv2/src/platform/ui/theme/contrastRatio.testing.ts@@ -0,0 +1,51 @@+/**+ * WCAG contrast measurement for accessibility assertions in browser tests.+ *+ * Lives here rather than in each test because it is a fixed formula, not a+ * per-surface judgement: two copies of it can silently disagree about what a+ * ratio *is*, and a contrast test that measures wrong is worse than no test —+ * it reports the palette as accessible when it is not.+ *+ * Test-only. It resolves colours by painting them to a canvas, so it needs a+ * real browser and has no place in the app bundle.+ */++/** Resolve any CSS colour — named, `oklch()`, `color-mix()` — to sRGB channels. */+export 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`.+ *+ * Opacity is applied by compositing rather than by scaling the ratio, because+ * a translucent foreground is read against the pixel it actually produces.+ */+export 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);+};