escherlies/elm-color/1.1.0

Color.Accessibility

WCAG accessibility functions for color contrast analysis.

This module provides functions to calculate contrast ratios and check WCAG compliance for color combinations, helping ensure your color choices meet accessibility standards.

Types

= AA
| AAA
| NotCompliant

WCAG compliance levels for accessibility standards.

  • AA: Standard level (4.5:1 for normal text, 3:1 for large text)
  • AAA: Enhanced level (7:1 for normal text, 4.5:1 for large text)
  • NotCompliant: Does not meet any WCAG contrast requirements
= Normal
| Large

Text size categories that affect contrast requirements.

  • Normal: Regular text size (requires higher contrast ratios)
  • Large: Large text (18pt+ or 14pt+ bold, requires lower contrast ratios)

Contrast

contrastRatio : Color -> Color -> Float

Calculate the contrast ratio between two colors according to WCAG guidelines.

The contrast ratio ranges from 1:1 (no contrast) to 21:1 (maximum contrast). It's calculated using the formula: (L1 + 0.05) / (L2 + 0.05) where L1 is the lighter color's relative luminance and L2 is the darker color's relative luminance.

For semi-transparent colors, they are first blended with the background color before calculating the contrast ratio, as per WCAG guidelines.

contrastRatio (rgb 0 0 0) (rgb 1 1 1) --> 21.0 (black on white)


contrastRatio (rgb 0.5 0.5 0.5) (rgb 1 1 1) --> ~3.98 (gray on white)


contrastRatio (rgba 0 0 0 0.5) (rgb 1 1 1) --> ~3.98 (50% black blended with white)

WCAG Compliance

meetsWcagAA : TextSize -> Color -> Color -> Bool

Check if two colors meet WCAG AA contrast requirements for a given text size.

    meetsWcagAA Normal (rgb 0 0 0) (rgb 1 1 1) --> True (21:1 > 4.5:1)
    meetsWcagAA Normal (rgb 0.7 0.7 0.7) (rgb 1 1 1) --> False (~2.8:1 < 4.5:1)
    meetsWcagAA Large (rgb 0.7 0.7 0.7) (rgb 1 1 1) --> True (~2.8:1 > 3:1)
meetsWcagAAA : TextSize -> Color -> Color -> Bool

Check if two colors meet WCAG AAA contrast requirements for a given text size.

    meetsWcagAAA Normal (rgb 0 0 0) (rgb 1 1 1) --> True (21:1 > 7:1)
    meetsWcagAAA Normal (rgb 0.7 0.7 0.7) (rgb 1 1 1) --> False (~2.8:1 < 7:1)
    meetsWcagAAA Large (rgb 0.3 0.3 0.3) (rgb 1 1 1) --> True (~5.7:1 > 4.5:1)
Color
-> Color
-> { normal : WcagLevel
, large : WcagLevel
}

Get the highest WCAG compliance level that two colors meet for both normal and large text sizes.

Returns a record with the compliance levels for both text sizes.

wcagLevel (rgb 0 0 0) (rgb 1 1 1)
--> { normal = AAA, large = AAA }


wcagLevel (rgb 0.5 0.5 0.5) (rgb 1 1 1)
--> { normal = AA, large = AAA }


wcagLevel (rgb 0.8 0.8 0.8) (rgb 1 1 1)
--> { normal = NotCompliant, large = NotCompliant }

Formatting

formatContrastRatio : Float -> String

Format a contrast ratio as a string with ratio notation.

    formatContrastRatio 4.5 --> "4.5:1"
    formatContrastRatio 21.0 --> "21:1"