From 0e370d8a56517dbd2f2c9bd6bb630060db494128 Mon Sep 17 00:00:00 2001 From: Tanmay Shah Date: Fri, 20 Nov 2026 04:49:43 +0000 Subject: [PATCH] fix(linter): do not judge contrast against a translucent backgroundColor A backgroundColor with alpha below 1 was compared to textColor as if it were opaque, so a 10% tint of the text color reported 1.00:1. The color that shows through depends on the surface beneath the component, which the component does not declare. Report an info finding that the pair was not checked instead of a false warning. Fixes #195 --- .../linter/rules/contrast-ratio.test.ts | 30 +++++++++++++++++++ .../src/linter/linter/rules/contrast-ratio.ts | 11 +++++++ 2 files changed, 41 insertions(+) diff --git a/packages/cli/src/linter/linter/rules/contrast-ratio.test.ts b/packages/cli/src/linter/linter/rules/contrast-ratio.test.ts index e8c911b4..fb859d2a 100644 --- a/packages/cli/src/linter/linter/rules/contrast-ratio.test.ts +++ b/packages/cli/src/linter/linter/rules/contrast-ratio.test.ts @@ -39,4 +39,34 @@ describe('contrastCheck', () => { const contrastWarnings = contrastCheck(state).filter(d => d.message.includes('contrast')); expect(contrastWarnings.length).toBe(0); }); + + it('does not judge a translucent backgroundColor against the opaque text color', () => { + // #195: a 10% tint of the text color was compared as if it were opaque + // and reported 1.00:1. What shows through depends on the surface beneath. + const state = buildState({ + colors: { warn: '#f2b33d', 'warn-tint': '#f2b33d1a' }, + components: { + alert: { backgroundColor: '{colors.warn-tint}', textColor: '{colors.warn}' }, + }, + }); + const findings = contrastCheck(state); + expect(findings.length).toBe(1); + expect(findings[0]!.severity).toBe('info'); + expect(findings[0]!.path).toBe('components.alert'); + expect(findings[0]!.message).toMatch(/translucent/); + expect(findings[0]!.message).not.toMatch(/below WCAG AA/); + }); + + it('still checks a backgroundColor whose alpha is fully opaque', () => { + const state = buildState({ + colors: { yellow: '#ffff00ff', white: '#ffffff' }, + components: { + 'button-bad': { backgroundColor: '{colors.yellow}', textColor: '{colors.white}' }, + }, + }); + const findings = contrastCheck(state); + expect(findings.length).toBe(1); + expect(findings[0]!.severity).toBeUndefined(); + expect(findings[0]!.message).toMatch(/below WCAG AA/); + }); }); diff --git a/packages/cli/src/linter/linter/rules/contrast-ratio.ts b/packages/cli/src/linter/linter/rules/contrast-ratio.ts index 22856490..25b7d69f 100644 --- a/packages/cli/src/linter/linter/rules/contrast-ratio.ts +++ b/packages/cli/src/linter/linter/rules/contrast-ratio.ts @@ -33,6 +33,17 @@ export function contrastCheck(state: DesignSystemState): RuleFinding[] { const textColor = resolveToColor(textValue); if (!bgColor || !textColor) continue; + // A translucent background shows whatever surface sits beneath it, and + // the component does not say what that is, so the ratio cannot be judged. + if (bgColor.a !== undefined && bgColor.a < 1) { + findings.push({ + path: `components.${compName}`, + severity: 'info', + message: `backgroundColor (${bgColor.hex}) is translucent, so the contrast of textColor (${textColor.hex}) depends on the surface beneath it and was not checked.`, + }); + continue; + } + const ratio = contrastRatio(bgColor, textColor); if (ratio < WCAG_AA_MINIMUM) { findings.push({