Raxol.Core.ColorSystem (Raxol v2.0.1)

View Source

Core color system for Raxol.

This module provides a unified interface for managing colors and themes throughout the application. It integrates with the Style.Colors modules to provide a consistent color experience.

Features

  • Theme management with semantic color naming
  • Color format conversion and validation
  • Accessibility checks and adjustments

Summary

Functions

Adjusts a color to meet contrast requirements with another color.

Creates a new theme with the given name and colors.

Gets the effective color value for a given semantic color name.

Gets a color value and ensures it's returned in a specific format (e.g., RGB tuple). Useful when a specific color representation is required for rendering.

Gets a color from the theme by its semantic name.

Gets a color from the theme by its semantic name with additional context.

Get the current theme configuration.

Initialize the color system with the given theme.

Checks if two colors meet WCAG contrast requirements.

Set the current theme.

Converts a color to its ANSI representation.

Functions

adjust_for_contrast(theme, foreground, background, level, size)

Adjusts a color to meet contrast requirements with another color.

Examples

iex> theme = create_theme("dark", %{
...>   text: "#808080",
...>   background: "#000000"
...> })
iex> adjusted = adjust_for_contrast(theme, :text, :background, :AA, :normal)
iex> meets_contrast_requirements?(adjusted, :text, :background, :AA, :normal)
true

create_theme(name, colors)

Creates a new theme with the given name and colors.

Examples

iex> theme = create_theme("dark", %{
...>   primary: "#FF0000",
...>   background: "#000000",
...>   text: "#FFFFFF"
...> })
iex> theme.name
"dark"

get(theme_id, color_name)

@spec get(atom(), atom()) :: Raxol.UI.Theming.Theme.color_value() | nil

Gets the effective color value for a given semantic color name.

It retrieves the color from the specified theme (by ID), automatically considering whether a high contrast variant is active based on accessibility settings.

Args:

  • theme_id: The atom ID of the theme to use (e.g., :default, :dark).
  • color_name: The semantic name of the color (e.g., :primary, :background).

Returns the color value (e.g., :red, {:rgb, r, g, b}) or nil if not found.

get_as(theme_id, color_name, format \\ :term)

@spec get_as(atom(), atom(), atom()) :: any() | nil

Gets a color value and ensures it's returned in a specific format (e.g., RGB tuple). Useful when a specific color representation is required for rendering.

Args:

  • theme_id: The atom ID of the theme to use.
  • color_name: The semantic name of the color.
  • format: The desired output format (:rgb_tuple, :hex_string, :term).

Supported formats: :rgb_tuple, :hex_string, :term

get_color(theme, name)

Gets a color from the theme by its semantic name.

Examples

iex> theme = create_theme("dark", %{primary: "#FF0000"})
iex> get_color(theme, :primary)
%Color{r: 255, g: 0, b: 0, hex: "#FF0000"}

get_color(theme, name, context)

Gets a color from the theme by its semantic name with additional context.

Examples

iex> theme = create_theme("dark", %{primary: "#FF0000"})
iex> get_color(theme, :primary, :foreground)
%Color{r: 255, g: 0, b: 0, hex: "#FF0000"}

get_current_theme()

Get the current theme configuration.

Returns

  • {:ok, theme} - Current theme configuration
  • {:error, reason} - Failed to get theme

Examples

iex> ColorSystem.get_current_theme()
{:ok, %{name: "default", colors: %{...}}}

init(theme_id \\ :default)

Initialize the color system with the given theme.

Parameters

  • theme_id - The theme identifier to use (default: :default)

Returns

  • :ok - Initialization successful
  • {:error, reason} - Initialization failed

Examples

iex> ColorSystem.init(:dark)
:ok

iex> ColorSystem.init()
:ok

meets_contrast_requirements?(theme, foreground, background, level, size)

Checks if two colors meet WCAG contrast requirements.

Examples

iex> theme = create_theme("dark", %{
...>   text: "#FFFFFF",
...>   background: "#000000"
...> })
iex> meets_contrast_requirements?(theme, :text, :background, :AA, :normal)
true

set_theme(theme_id)

Set the current theme.

Parameters

  • theme_id - The theme identifier to set

Returns

  • :ok - Theme set successfully
  • {:error, reason} - Failed to set theme

Examples

iex> ColorSystem.set_theme(:dark)
:ok

to_ansi(theme, color_name, type)

Converts a color to its ANSI representation.

Examples

iex> theme = create_theme("dark", %{primary: "#FF0000"})
iex> to_ansi(theme, :primary, :foreground)
196