Overview
HuxWCAG provides WCAG (Web Content Accessibility Guidelines) 2.1 compliant utilities for calculating color contrast ratios and determining accessible text colors. These utilities ensure your custom components meet accessibility standards.
Hux UI components automatically use these utilities internally, but they’re also available as a public API for your custom components.
Basic Usage
Get Contrasting Text Color
The most common use case is determining the appropriate text color for a background:API Reference
getContrastingTextColor
Determines the appropriate text color based on WCAG AA contrast requirements.
Parameters:
backgroundColor(required) - The background color to check againstcontext(required) - BuildContext for theme-aware color tokens
Color - The text color with better contrast ratio
Example:
calculateContrastRatio
Calculates the contrast ratio between two colors according to WCAG guidelines.
Parameters:
color1- First colorcolor2- Second color
double - Contrast ratio between 1 and 21 (higher = better contrast)
WCAG Requirements:
- Normal text: Minimum 4.5:1 ratio
- Large text (18pt+ or 14pt+ bold): Minimum 3:1 ratio
getRelativeLuminance
Calculates the relative luminance of a color according to WCAG guidelines.
Parameters:
color- The color to calculate luminance for
double - Luminance value between 0 (black) and 1 (white)
Implementation Details:
- Uses ITU-R BT.709 coefficients for accurate calculation
- Applies proper gamma correction for sRGB color space
- Follows WCAG 2.1 specification exactly
meetsContrastAA
Checks if two colors meet WCAG AA contrast requirements.
Parameters:
foreground(required) - Text/foreground colorbackground(required) - Background colorisLargeText(optional) - Whether text is large (18pt+ or 14pt+ bold). Default:false
bool - true if contrast meets WCAG AA requirements
Example:
Use Cases
Custom Components
Use HuxWCAG utilities in your custom components to ensure accessibility:Form Validation
Validate color combinations before applying them:Dynamic Theming
Calculate accessible colors for dynamic themes:Technical Details
WCAG 2.1 Compliance
All calculations follow the official WCAG 2.1 specification:- Relative Luminance: Uses ITU-R BT.709 coefficients (0.2126, 0.7152, 0.0722)
- Gamma Correction: Proper sRGB gamma correction applied
- Contrast Formula:
(L1 + 0.05) / (L2 + 0.05)where L1 is lighter, L2 is darker
Accuracy
HuxWCAG uses manual calculation rather than Flutter’scomputeLuminance() for:
- ✅ Exact WCAG 2.1 compliance
- ✅ Consistent results across all platforms
- ✅ Proper gamma correction for sRGB
Performance
All methods are static and optimized for performance:- No object instantiation required
- Pure mathematical calculations
- Suitable for real-time use in build methods
Integration with Hux Components
Hux UI components automatically use these utilities internally:- HuxButton - Primary buttons use
getContrastingTextColor()automatically - HuxDropdown - Text color adapts to primary color variants
- HuxBadge - Badge text color calculated for all variants
- HuxToggle - Icon and text colors adapt to background
- HuxCheckbox - Checkmark color adapts to primary color
- HuxPagination - Selected page text color calculated
Pro Tip: Hux UI components handle WCAG compliance automatically. Only use HuxWCAG utilities when creating custom components or when you need fine-grained control over accessibility calculations.