wcag_vision library
wcag_vision — an offline, algorithmic WCAG accessibility engine.
Modules:
- Contrast — relative luminance, WCAG 2.x contrast ratios, alpha compositing for translucent colours, and AA/AAA conformance evaluation.
- CVD simulation — colour-vision-deficiency simulation (protanopia, deuteranopia, tritanopia) using the Machado, Oliveira & Fairchild (2009) physiologically-based model.
- Colour extraction — dominant-colour extraction via k-means clustering with deterministic k-means++ seeding, designed for one-shot analysis of a single captured frame.
Classes
- ContrastReport
- The result of evaluating a foreground/background colour pair against the WCAG 2.x contrast requirements.
- ExtractedColor
- One dominant colour found by k-means extraction, together with how much of the sampled image it accounts for.
Enums
- CvdType
- A type of colour vision deficiency (CVD) that can be simulated.
- WcagConformanceLevel
- A WCAG 2.x conformance level for text contrast.
- WcagTextSize
- The text-size category a contrast requirement applies to.
Constants
- defaultClusterCount → const int
-
Default number of clusters (extractDominantColors's
k). - defaultConvergenceThreshold → const double
- Default centroid-movement convergence threshold, expressed as Euclidean distance in the RGB unit cube.
- defaultMaxIterations → const int
- Default cap on Lloyd iterations. Dominant-colour extraction on well-separated palettes converges in well under 10 iterations; 25 leaves generous headroom for noisy photographs without risking a long stall.
- defaultMaxSamples → const int
- Default maximum number of pixels sampled from the input.
- maxContrastRatio → const double
-
The highest possible WCAG contrast ratio:
21.0, i.e. pure black on pure white (or vice versa). - minContrastRatio → const double
-
The lowest possible WCAG contrast ratio:
1.0, when both colours share the same relative luminance.
Functions
-
compositeOver(
Color foreground, Color background) → Color -
Alpha-composites
foregroundoverbackgroundusing the Porter-Duff "source-over" operator, returning the resulting blended colour. -
contrastRatio(
Color a, Color b) → double - Computes the WCAG 2.x contrast ratio between two opaque colours.
-
contrastRatioOver(
Color foreground, Color background) → double -
Computes the WCAG contrast ratio of a possibly semi-transparent
foregroundagainst an opaquebackground. -
evaluateContrast(
Color foreground, Color background) → ContrastReport -
Evaluates the WCAG contrast between a
foregroundandbackgroundcolour, returning a ContrastReport. -
extractDominantColors(
List< Color> pixels, {int k = defaultClusterCount, int seed = 0, int maxIterations = defaultMaxIterations, double convergenceThreshold = defaultConvergenceThreshold, int maxSamples = defaultMaxSamples}) → List<ExtractedColor> - Extracts the dominant colours of an image using k-means clustering.
-
extractDominantColorsAsync(
List< Color> pixels, {int k = defaultClusterCount, int seed = 0, int maxIterations = defaultMaxIterations, double convergenceThreshold = defaultConvergenceThreshold, int maxSamples = defaultMaxSamples}) → Future<List< ExtractedColor> > - Extracts dominant colours off the main isolate — the entry point UI code should call.
-
relativeLuminance(
Color color) → double -
Computes the WCAG 2.x relative luminance of an opaque
color. -
simulateCvd(
Color color, CvdType type) → Color -
Simulates how
coloris perceived by a viewer with the given colour vision deficiencytype, returning the simulated colour. -
wcagThreshold(
WcagConformanceLevel level, WcagTextSize size) → double -
Returns the minimum contrast ratio required for the given
levelandsize, per WCAG 2.x Success Criteria 1.4.3 (AA) and 1.4.6 (AAA).