Raxol.Style.Colors.PaletteManager (Raxol v0.2.0)

View Source

Manages color palettes for the Raxol terminal emulator.

This module provides functionality to create, store, and retrieve color palettes. It works with the ColorSystem to provide a comprehensive color management solution that respects user preferences and accessibility settings.

Features

  • Create and manage named color palettes
  • Generate color scales (light to dark variations)
  • Create accessible color combinations
  • Calculate contrast ratios
  • Support for user preference persistence
  • Integration with the accessibility system

Usage

# Initialize the palette manager
PaletteManager.init()

# Register a custom palette
PaletteManager.register_palette(:brand, %{
  main: "#0077CC",
  accent: "#FF5722",
  neutral: "#F0F0F0"
})

# Generate a color scale
scale = PaletteManager.generate_scale("#0077CC", 9)

Summary

Functions

Generate a color scale from a base color.

Get a color from a registered palette.

Get all registered palettes.

Get user color preferences.

Initialize the palette manager.

Save user color preferences.

Suggest an accessible color alternative for a given color.

Functions

generate_scale(base_color, steps \\ 9, opts \\ [])

Generate a color scale from a base color.

Creates a series of color variations from light to dark based on the given color.

Parameters

  • base_color - The starting color (hex string like "#0077CC")
  • steps - The number of steps in the scale (default: 9)
  • opts - Additional options

Options

  • :name - Name to identify this scale
  • :lightness_range - Tuple of {min_lightness, max_lightness} (default: {0.1, 0.9})
  • :saturation_adjust - Adjustment to saturation for each step (default: 0.05)

Examples

iex> PaletteManager.generate_scale("#0077CC", 9)
[
  "#E6F0FA", "#CCE0F5", "#B3D1F0", "#99C2EB",
  "#80B3E6", "#66A3E0", "#4D94DB", "#3385D6", "#0077CC"
]

get_color(palette_name, color_name)

Get a color from a registered palette.

Parameters

  • palette_name - The name of the palette
  • color_name - The name of the color within the palette

Examples

iex> PaletteManager.get_color(:ocean, :main)
"#0077CC"

get_palettes(opts \\ [])

Get all registered palettes.

Options

  • :category - Filter palettes by category
  • :tags - Filter palettes by tags
  • :accessible - Filter by accessibility

Examples

iex> PaletteManager.get_palettes()
%{ocean: %{colors: %{main: "#0077CC", ...}, ...}, ...}

iex> PaletteManager.get_palettes(category: :brand)
%{brand: %{colors: %{main: "#FF5722", ...}, ...}}

get_user_preferences(user_id)

Get user color preferences.

Parameters

  • user_id - Identifier for the user

Examples

iex> PaletteManager.get_user_preferences("user123")
%{theme: :dark, accent_color: "#FF5722"}

init()

Initialize the palette manager.

This sets up the necessary state for managing color palettes and integrates with the ColorSystem.

Examples

iex> PaletteManager.init()
:ok

register_palette(palette_name, colors, opts \\ [])

Register a color palette.

Parameters

  • palette_name - Unique identifier for the palette
  • colors - Map of color names to color values
  • opts - Additional options

Options

  • :description - Description of the palette
  • :category - Category for organizing palettes
  • :tags - List of tags for filtering
  • :accessible - Whether the palette is designed for accessibility

Examples

iex> PaletteManager.register_palette(:ocean, %{
...>   main: "#0077CC",
...>   accent: "#00AAFF",
...>   background: "#F0F7FF"
...> })
:ok

save_user_preferences(user_id, preferences)

Save user color preferences.

Parameters

  • user_id - Identifier for the user
  • preferences - Map of color preferences

Examples

iex> PaletteManager.save_user_preferences("user123", %{
...>   theme: :dark,
...>   accent_color: "#FF5722"
...> })
:ok

suggest_accessible_color(color, background, level \\ :aa)

Suggest an accessible color alternative for a given color.

Parameters

  • color - The original color
  • background - The background color the text will appear on
  • level - The WCAG level to achieve (:aa or :aaa) (default: :aa)

Examples

iex> PaletteManager.suggest_accessible_color("#777777", "#FFFFFF")
"#595959"