Raxol.Style.Colors.Utilities (Raxol v0.4.0)

View Source

Shared color utilities for the Raxol color system.

This module provides common color manipulation and analysis functions, including contrast calculations, accessibility checks, and color format conversions.

Summary

Functions

Adjusts a color to meet contrast requirements with another color.

Returns black or white, whichever has better contrast with the background.

Checks if two colors have sufficient contrast according to WCAG guidelines.

Calculates the contrast ratio between two colors according to WCAG 2.0.

Increases the contrast of a color by making it more extreme.

Checks if two colors meet WCAG contrast requirements.

Calculates the relative luminance of a color according to WCAG 2.0.

Functions

adjust_for_contrast(color, background, level, size)

Adjusts a color to meet contrast requirements with another color.

Parameters

  • color - The color to adjust (Color struct or RGB tuple)
  • background - The background color (Color struct or RGB tuple)
  • level - WCAG level (:A, :AA, or :AAA)
  • size - Text size (:normal or :large)

Returns

An adjusted Color struct that meets contrast requirements

best_bw_contrast(background, min_ratio \\ 4.5)

Returns black or white, whichever has better contrast with the background.

check_contrast(color1, color2, level \\ :aa, size \\ :normal)

Checks if two colors have sufficient contrast according to WCAG guidelines.

contrast_ratio(color1, color2)

Calculates the contrast ratio between two colors according to WCAG 2.0.

Parameters

  • color1 - First color (Color struct or RGB tuple)
  • color2 - Second color (Color struct or RGB tuple)

Returns

A float representing the contrast ratio (1:1 to 21:1)

increase_contrast(color)

Increases the contrast of a color by making it more extreme.

Parameters

  • color - The color to increase contrast for (Color struct or RGB tuple)

Returns

A new Color struct with increased contrast

meets_contrast_requirements?(color1, color2, level, size)

Checks if two colors meet WCAG contrast requirements.

Parameters

  • color1 - First color (Color struct or RGB tuple)
  • color2 - Second color (Color struct or RGB tuple)
  • level - WCAG level (:A, :AA, or :AAA)
  • size - Text size (:normal or :large)

Returns

true if the contrast meets requirements, false otherwise

relative_luminance(color)

Calculates the relative luminance of a color according to WCAG 2.0.

Parameters

  • color - A Color struct or RGB tuple

Returns

A float between 0 and 1 representing the relative luminance