Raxol.Style.Colors.System (Raxol v2.0.1)

View Source

Refactored Color System module with GenServer-based state management.

This module provides backward compatibility while eliminating Process dictionary usage. All state is now managed through the Colors.System.Server GenServer.

Migration Notes

This module replaces direct Process dictionary usage with supervised GenServer state. The API remains the same, but the implementation is now OTP-compliant and more robust.

Features Maintained

  • Theme management and switching
  • High contrast mode support
  • Automatic accessibility adjustments
  • Color caching and resolution
  • Event-driven theme changes

Summary

Functions

Applies a theme to the color system.

Get all UI colors for the current theme as a map of role => color.

Get all UI colors for a specific theme as a map of role => color.

Get a color from the current theme.

Get the current theme name.

Get a UI color by role (e.g., :primary_button) from the current theme. Resolves the role using the theme's ui_mappings, then fetches the color. Returns nil if the role or color is not found.

Handle high contrast mode changes from the accessibility module.

Initialize the color system.

Register a custom theme.

Functions

adjust_for_contrast(color, background, level, size)

apply_theme(theme_name, opts \\ [])

Applies a theme to the color system.

Parameters

  • theme_name - The name 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

create_dark_theme()

create_high_contrast_theme()

darken_color(color, amount)

get_all_ui_colors()

@spec get_all_ui_colors() :: map()

Get all UI colors for the current theme as a map of role => color.

get_all_ui_colors(theme)

Get all UI colors for a specific theme as a map of role => color.

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()

Get the current theme name.

get_ui_color(ui_role)

@spec get_ui_color(atom()) :: any()

Get a UI color by role (e.g., :primary_button) from the current theme. Resolves the role using the theme's ui_mappings, then fetches the color. Returns nil if the role or color is not found.

handle_high_contrast(event_type \\ :accessibility_high_contrast, event_data)

Handle high contrast mode changes from the accessibility module.

increase_contrast(color)

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

lighten_color(color, amount)

meets_contrast_requirements?(fg, bg, level, size)

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