Raxol.UI.Theming.Colors (Raxol v0.3.0)

View Source

Color management utilities for theme handling.

This module provides functions for working with colors in the context of UI theming, including color format conversion, manipulation, and theme-specific operations.

Features

  • Color format conversion (hex, RGB, ANSI)
  • Color manipulation (lighten, darken, blend)
  • Theme color management
  • Accessibility checks

Summary

Functions

Converts an ANSI color code to RGB values.

Blends two colors with the specified alpha value.

Calculates the contrast ratio between two colors.

Converts a color or theme map to use a specific palette (e.g., 256 colors). Finds the closest color in the palette for each color in the theme. Currently only supports named palettes like :xterm256

Darkens a color by the specified amount.

Finds the closest ANSI 256-color index (0-255) to the given RGB color.

Finds the closest ANSI basic color index (0-15) to the given RGB color.

Converts a hex color string to RGB values.

Converts HSL values to RGB values.

Lightens a color by the specified amount.

Checks if two colors meet WCAG contrast requirements.

Converts RGB values to the closest ANSI color code.

Converts RGB values to a hex color string.

Converts RGB values to HSL values.

Converts a color to its ANSI representation.

Converts a color to its ANSI 16-color representation.

Converts a color to its hex representation.

Converts a color from one format to RGB values.

Types

color_hex()

@type color_hex() :: String.t()

color_hsl()

@type color_hsl() :: {hue :: 0..360, saturation :: 0..100, lightness :: 0..100}

color_name()

@type color_name() :: atom()

color_rgb()

@type color_rgb() :: {red :: 0..255, green :: 0..255, blue :: 0..255}

color_rgba()

@type color_rgba() ::
  {red :: 0..255, green :: 0..255, blue :: 0..255, alpha :: 0..255}

Functions

ansi_to_rgb(code)

Converts an ANSI color code to RGB values.

Examples

iex> ansi_to_rgb(1)
{205, 0, 0}

blend(color1, color2, alpha)

Blends two colors with the specified alpha value.

Examples

iex> blend("#FF0000", "#0000FF", 0.5)
"#800080"
iex> blend(:red, :blue, 0.5)
"#800080"

contrast_ratio(color1, color2)

Calculates the contrast ratio between two colors.

Examples

iex> contrast_ratio("#FFFFFF", "#000000")
21.0
iex> contrast_ratio(:white, :black)
21.0

convert_to_basic(color)

convert_to_palette(value, palette_name \\ :xterm256)

Converts a color or theme map to use a specific palette (e.g., 256 colors). Finds the closest color in the palette for each color in the theme. Currently only supports named palettes like :xterm256

darken(color, amount)

Darkens a color by the specified amount.

Examples

iex> darken("#FFFFFF", 0.5)
"#808080"
iex> darken(:red, 0.5)
"#800000"

find_closest_256_color(rgb)

@spec find_closest_256_color(color_rgb()) :: 0..255

Finds the closest ANSI 256-color index (0-255) to the given RGB color.

find_closest_basic_color(rgb)

@spec find_closest_basic_color(color_rgb()) :: 0..15

Finds the closest ANSI basic color index (0-15) to the given RGB color.

hex_to_rgb(hex)

Converts a hex color string to RGB values.

Examples

iex> hex_to_rgb("#FF0000")
{255, 0, 0}

hsl_to_rgb(h, s, l)

Converts HSL values to RGB values.

Examples

iex> hsl_to_rgb(0, 1.0, 0.5)
{255, 0, 0}

lighten(color, amount)

Lightens a color by the specified amount.

Examples

iex> lighten("#000000", 0.5)
"#808080"
iex> lighten(:red, 0.5)
"#FF8080"

meets_contrast_requirements?(color1, color2, level, size)

Checks if two colors meet WCAG contrast requirements.

Examples

iex> meets_contrast_requirements?("#FFFFFF", "#000000", :AA, :normal)
true

rgb_to_ansi(r, g, b)

Converts RGB values to the closest ANSI color code.

Examples

iex> rgb_to_ansi(255, 0, 0)
196

rgb_to_hex(r, g, b)

Converts RGB values to a hex color string.

Examples

iex> rgb_to_hex(255, 0, 0)
"#FF0000"

rgb_to_hsl(r, g, b)

Converts RGB values to HSL values.

Examples

iex> rgb_to_hsl(255, 0, 0)
{0, 1.0, 0.5}

to_ansi(color)

Converts a color to its ANSI representation.

Examples

iex> Colors.to_ansi(:red)
196

iex> Colors.to_ansi("#FF0000")
196

to_ansi_16(color)

Converts a color to its ANSI 16-color representation.

Examples

iex> Colors.to_ansi_16(:red)
1

iex> Colors.to_ansi_16("#FF0000")
1

to_hex(color)

Converts a color to its hex representation.

Examples

iex> Colors.to_hex(:red)
"#FF0000"

iex> Colors.to_hex("#00FF00")
"#00FF00"

to_rgb(color)

Converts a color from one format to RGB values.

Examples

iex> Colors.to_rgb("#FF0000")
{255, 0, 0}

iex> Colors.to_rgb(:blue)
{0, 0, 255}