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

View Source

Provides basic color utility functions.

Includes checks for dark colors, brightness, luminance, and hex format.

Examples

alias Raxol.Style.Colors.{Color, Utilities}

# Check if a color is dark
Utilities.dark_color?("#333333") # true

# Get perceived brightness
Utilities.brightness(Color.from_hex("#FF0000")) # 76

# Check hex format
Utilities.hex_color?("#ABC") # true
Utilities.hex_color?("blue") # false

Summary

Functions

Calculates the perceived brightness of a color.

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

Checks if a color is considered dark.

Checks if a hex string is a valid hex color.

Calculates the relative luminance of a color according to WCAG.

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

Functions

brightness(color)

Calculates the perceived brightness of a color.

Returns a value between 0 (darkest) and 255 (brightest).

Parameters

  • color - The color to analyze

Examples

iex> black = Raxol.Style.Colors.Color.from_hex("#000000")
iex> Raxol.Style.Colors.Utilities.brightness(black)
0

iex> white = Raxol.Style.Colors.Color.from_hex("#FFFFFF")
iex> Raxol.Style.Colors.Utilities.brightness(white)
255

contrast_ratio(color1, color2)

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

Parameters

  • color1 - The first color (hex string or Color struct)
  • color2 - The second color (hex string or Color struct)

Returns

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

Examples

iex> Raxol.Style.Colors.Utilities.contrast_ratio("#000000", "#FFFFFF")
21.0

iex> Raxol.Style.Colors.Utilities.contrast_ratio("#777777", "#999999")
1.3

dark_color?(color)

Checks if a color is considered dark.

Uses relative luminance (a value < 0.5 is considered dark).

Parameters

  • color - The color to check (hex string or Color struct)

Returns

  • true if the color is dark, false otherwise

Examples

iex> Raxol.Style.Colors.Utilities.dark_color?("#000000")
true

iex> Raxol.Style.Colors.Utilities.dark_color?("#FFFFFF")
false

hex_color?(hex_string)

@spec hex_color?(String.t()) :: boolean()

Checks if a hex string is a valid hex color.

Parameters

  • hex_string - The hex string to check

Returns

  • true if the hex string is a valid hex color, false otherwise

Examples

iex> Raxol.Style.Colors.Utilities.hex_color?("#FF00AA")
true
iex> Raxol.Style.Colors.Utilities.hex_color?("blue")
false

luminance(color)

Calculates the relative luminance of a color according to WCAG.

Returns a value between 0 (darkest) and 1 (brightest).

Parameters

  • color - The color to analyze

Examples

iex> black = Raxol.Style.Colors.Color.from_hex("#000000")
iex> Raxol.Style.Colors.Utilities.luminance(black)
0.0

iex> white = Raxol.Style.Colors.Color.from_hex("#FFFFFF")
iex> Raxol.Style.Colors.Utilities.luminance(white)
1.0

relative_luminance(color)

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

Parameters

  • color - The color to calculate luminance for (hex string or Color struct)

Returns

  • A float between 0 and 1 representing the relative luminance

Examples

iex> Raxol.Style.Colors.Utilities.relative_luminance("#000000")
0.0

iex> Raxol.Style.Colors.Utilities.relative_luminance("#FFFFFF")
1.0