Raxol.Style.Colors.System (Raxol v0.3.0)

View Source

Core color system for the Raxol terminal emulator.

This module provides a robust color system that:

  • Manages color palettes with semantic naming
  • Provides accessible color alternatives for high contrast mode
  • Supports theme customization
  • Calculates contrast ratios for text/background combinations
  • Automatically adjusts colors for optimal readability
  • Integrates with the accessibility module

Usage

# Initialize the color system
ColorSystem.init()

# Get a semantic color (will respect accessibility settings)
color = ColorSystem.get_color(:primary)

# Get a specific color variation
hover_color = ColorSystem.get_color(:primary, :hover)

# Register a custom theme
ColorSystem.register_theme(:ocean, %{
  primary: "#0077CC",
  secondary: "#00AAFF",
  background: "#001133",
  foreground: "#FFFFFF",
  accent: "#FF9900"
})

# Apply a theme
ColorSystem.apply_theme(:ocean)

Summary

Functions

Applies a theme to the color system.

Get a color from the current theme.

Handle high contrast mode changes from the accessibility module.

Initialize the color system.

Register a custom theme.

Functions

apply_theme(theme_id, opts \\ [])

Applies a theme to the color system.

Parameters

  • theme_id - The ID of the theme to apply
  • opts - Additional options
    • :high_contrast - Whether to apply high contrast mode (default: current setting)

Returns

  • :ok on success
  • {:error, reason} on failure

get_color(color_name, variant \\ :base)

Get a color from the current theme.

This function respects the current accessibility settings, automatically returning high-contrast alternatives when needed.

Parameters

  • color_name - The semantic name of the color (e.g., :primary, :error)
  • variant - The variant of the color (e.g., :base, :hover, :active) (default: :base)

Examples

iex> ColorSystem.get_color(:primary)
"#0077CC"

iex> ColorSystem.get_color(:primary, :hover)
"#0088DD"

get_current_theme_name()

@spec get_current_theme_name() :: atom() | String.t()

handle_high_contrast(arg)

Handle high contrast mode changes from the accessibility module.

init(opts \\ [])

Initialize the color system.

This sets up the default themes, registers event handlers for accessibility changes, and establishes the default color palette.

Options

  • :theme - The initial theme to use (default: :default)
  • :high_contrast - Whether to start in high contrast mode (default: from accessibility settings)

Examples

iex> ColorSystem.init()
:ok

iex> ColorSystem.init(theme: :dark)
:ok

register_theme(theme_attrs)

Register a custom theme.

Parameters

  • theme_attrs - Map of theme attributes

Examples

iex> ColorSystem.register_theme(%{
...>   primary: "#0077CC",
...>   secondary: "#00AAFF",
...>   background: "#001133",
...>   foreground: "#FFFFFF",
...>   accent: "#FF9900"
...> })
:ok