Raxol.UI.Theming.Colors (Raxol v0.3.0)
View SourceColor 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
Functions
Converts an ANSI color code to RGB values.
Examples
iex> ansi_to_rgb(1)
{205, 0, 0}
Blends two colors with the specified alpha value.
Examples
iex> blend("#FF0000", "#0000FF", 0.5)
"#800080"
iex> blend(:red, :blue, 0.5)
"#800080"
Calculates the contrast ratio between two colors.
Examples
iex> contrast_ratio("#FFFFFF", "#000000")
21.0
iex> contrast_ratio(:white, :black)
21.0
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.
Examples
iex> darken("#FFFFFF", 0.5)
"#808080"
iex> darken(:red, 0.5)
"#800000"
@spec find_closest_256_color(color_rgb()) :: 0..255
Finds the closest ANSI 256-color index (0-255) to the given RGB color.
@spec find_closest_basic_color(color_rgb()) :: 0..15
Finds the closest ANSI basic color index (0-15) to the given RGB color.
Converts a hex color string to RGB values.
Examples
iex> hex_to_rgb("#FF0000")
{255, 0, 0}
Converts HSL values to RGB values.
Examples
iex> hsl_to_rgb(0, 1.0, 0.5)
{255, 0, 0}
Lightens a color by the specified amount.
Examples
iex> lighten("#000000", 0.5)
"#808080"
iex> lighten(:red, 0.5)
"#FF8080"
Checks if two colors meet WCAG contrast requirements.
Examples
iex> meets_contrast_requirements?("#FFFFFF", "#000000", :AA, :normal)
true
Converts RGB values to the closest ANSI color code.
Examples
iex> rgb_to_ansi(255, 0, 0)
196
Converts RGB values to a hex color string.
Examples
iex> rgb_to_hex(255, 0, 0)
"#FF0000"
Converts RGB values to HSL values.
Examples
iex> rgb_to_hsl(255, 0, 0)
{0, 1.0, 0.5}
Converts a color to its ANSI representation.
Examples
iex> Colors.to_ansi(:red)
196
iex> Colors.to_ansi("#FF0000")
196
Converts a color to its ANSI 16-color representation.
Examples
iex> Colors.to_ansi_16(:red)
1
iex> Colors.to_ansi_16("#FF0000")
1
Converts a color to its hex representation.
Examples
iex> Colors.to_hex(:red)
"#FF0000"
iex> Colors.to_hex("#00FF00")
"#00FF00"
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}