feat: add color contrast calculation utility with WCAG standards

This commit is contained in:
张翔
2026-03-06 20:11:32 +08:00
parent c9715ee7d0
commit 940598c5cc
2 changed files with 82 additions and 0 deletions
+19
View File
@@ -0,0 +1,19 @@
import { test, expect } from '@playwright/test';
import { calculateContrastRatio, meetsWCAGStandard } from '@/lib/color-contrast';
test('should calculate correct contrast ratio for black on white', () => {
const ratio = calculateContrastRatio('#000000', '#FFFFFF');
expect(ratio).toBeCloseTo(21, 1);
});
test('should identify WCAG AA compliance for normal text', () => {
const result = meetsWCAGStandard('#000000', '#FFFFFF', 'AA', 'normal');
expect(result.passes).toBe(true);
expect(result.ratio).toBeGreaterThan(4.5);
});
test('should fail WCAG AA for low contrast colors', () => {
const result = meetsWCAGStandard('#808080', '#FFFFFF', 'AA', 'normal');
expect(result.passes).toBe(false);
expect(result.ratio).toBeLessThan(4.5);
});