-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwcag.js
More file actions
159 lines (139 loc) · 5.91 KB
/
wcag.js
File metadata and controls
159 lines (139 loc) · 5.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* WCAG Accessibility Testing Example
*
* Demonstrates how to analyze images for WCAG color contrast violations
* and generate visual overlays showing problematic regions.
*/
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { analyzeWcagContrast, saveWcagOverlay } from '../index.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
async function main() {
console.log('=== WCAG Accessibility Testing Demo ===\n');
// Example 1: Basic WCAG analysis
console.log('1. Running basic WCAG AA analysis...');
try {
let analysis = await analyzeWcagContrast(
path.join(__dirname, '../../../tests/fixtures/screenshots/vizzly-baseline.png'),
{
edgeThreshold: 60,
minRegionSize: 50,
checkAA: true,
checkAAA: false,
}
);
console.log(` Total edges detected: ${analysis.totalEdges}`);
console.log(` AA normal text pass rate: ${analysis.aaNormalPassPercentage.toFixed(1)}%`);
console.log(` AA large text pass rate: ${analysis.aaLargePassPercentage.toFixed(1)}%`);
console.log(` Violations found: ${analysis.violations.length}\n`);
if (analysis.violations.length > 0) {
console.log(' Top 5 violations by size:');
for (let i = 0; i < Math.min(5, analysis.violations.length); i++) {
let violation = analysis.violations[i];
console.log(
` ${i + 1}. Region at (${violation.boundingBox.x}, ${violation.boundingBox.y})`
);
console.log(` Contrast ratio: ${violation.contrastRatio.toFixed(2)}:1`);
console.log(` Pixels affected: ${violation.pixelCount}`);
console.log(
` Fails AA normal: ${violation.failsAaNormal}, Fails AA large: ${violation.failsAaLarge}`
);
}
console.log();
}
} catch (error) {
console.error(' Error:', error.message);
}
// Example 2: WCAG AAA analysis (stricter)
console.log('2. Running strict WCAG AAA analysis...');
try {
let analysis = await analyzeWcagContrast(
path.join(__dirname, '../../../tests/fixtures/screenshots/vizzly-baseline.png'),
{
edgeThreshold: 60,
minRegionSize: 50,
checkAA: true,
checkAAA: true,
}
);
console.log(` Total edges detected: ${analysis.totalEdges}`);
console.log(` AAA normal text pass rate: ${analysis.aaaNormalPassPercentage.toFixed(1)}%`);
console.log(` AAA large text pass rate: ${analysis.aaaLargePassPercentage.toFixed(1)}%`);
console.log(` AAA violations found: ${analysis.violations.length}\n`);
} catch (error) {
console.error(' Error:', error.message);
}
// Example 3: Generate visual overlay
console.log('3. Generating visual overlay...');
try {
let analysis = await analyzeWcagContrast(
path.join(__dirname, '../../../tests/fixtures/screenshots/vizzly-baseline.png'),
{
checkAA: true,
checkAAA: false,
}
);
let outputPath = path.join(__dirname, '../wcag-violations.png');
await saveWcagOverlay(
path.join(__dirname, '../../../tests/fixtures/screenshots/vizzly-baseline.png'),
analysis,
outputPath,
{
highlightColor: [255, 0, 0, 180], // Semi-transparent red
}
);
console.log(` ✓ Overlay saved to: ${outputPath}\n`);
} catch (error) {
console.error(' Error:', error.message);
}
// Example 4: Synchronous API (for simple scripts)
console.log('4. Using synchronous API...');
try {
const { analyzeWcagContrastSync } = require('../index.js');
let analysis = analyzeWcagContrastSync(
path.join(__dirname, '../../../tests/fixtures/screenshots/vizzly-baseline.png'),
{
edgeThreshold: 80, // More lenient threshold
minRegionSize: 100, // Larger regions only
}
);
console.log(` Total edges: ${analysis.totalEdges}`);
console.log(` Violations: ${analysis.violations.length}`);
console.log(` Pass rate: ${analysis.aaNormalPassPercentage.toFixed(1)}%\n`);
} catch (error) {
console.error(' Error:', error.message);
}
// Example 5: Detailed violation analysis
console.log('5. Analyzing violation details...');
try {
let analysis = await analyzeWcagContrast(
path.join(__dirname, '../../../tests/fixtures/screenshots/vizzly-baseline.png')
);
if (analysis.violations.length > 0) {
let violation = analysis.violations[0];
console.log(' First violation details:');
console.log(` - Location: (${violation.boundingBox.x}, ${violation.boundingBox.y})`);
console.log(` - Size: ${violation.boundingBox.width}x${violation.boundingBox.height}`);
console.log(
` - Center: (${violation.centerOfMass[0].toFixed(1)}, ${violation.centerOfMass[1].toFixed(1)})`
);
console.log(` - Foreground color: rgba(${violation.foregroundColor.join(', ')})`);
console.log(` - Background color: rgba(${violation.backgroundColor.join(', ')})`);
console.log(` - Foreground luminance: ${violation.foregroundLuminance.toFixed(3)}`);
console.log(` - Background luminance: ${violation.backgroundLuminance.toFixed(3)}`);
console.log(
` - Contrast ratio: ${violation.contrastRatio.toFixed(2)}:1 (${violation.minContrastRatio.toFixed(2)} - ${violation.maxContrastRatio.toFixed(2)})`
);
console.log(` - Pixel count: ${violation.pixelCount}`);
console.log(' - WCAG Status:');
console.log(` AA Normal (4.5:1): ${violation.failsAaNormal ? '❌ FAIL' : '✓ PASS'}`);
console.log(` AA Large (3.0:1): ${violation.failsAaLarge ? '❌ FAIL' : '✓ PASS'}`);
console.log(` AAA Normal (7.0:1): ${violation.failsAaaNormal ? '❌ FAIL' : '✓ PASS'}`);
console.log(` AAA Large (4.5:1): ${violation.failsAaaLarge ? '❌ FAIL' : '✓ PASS'}`);
}
} catch (error) {
console.error(' Error:', error.message);
}
console.log('\n=== Demo Complete ===');
}
main().catch(console.error);