Raxol.Core.Theming.ThemeRegistry (Raxol Core v2.8.0)

Copy Markdown View Source

Unified theme registry - single source of truth for all Raxol themes.

Provides theme definitions that can be consumed by both terminal and web components. Each theme includes the standard 16-color ANSI palette plus UI colors (background, foreground, cursor, selection).

Design

Following Chris McCord's recommendation: one source of truth for themes that both LiveView and terminal components can consume.

Color Formats

Colors are stored as hex strings internally. Use conversion functions to get colors in the format needed:

  • to_hex/1 - Returns hex string (for CSS/web)
  • to_rgb/1 - Returns {r, g, b} tuple (for terminal)
  • to_rgba_map/1 - Returns %{r: r, g: g, b: b, a: 1.0} (for terminal manager)

Usage

# Get a theme
{:ok, theme} = ThemeRegistry.get(:dracula)

# List available themes
ThemeRegistry.list()

# Get color in different formats
ThemeRegistry.get_color(:dracula, :red)           # "#ff5555"
ThemeRegistry.get_color(:dracula, :red, :rgb)     # {255, 85, 85}
ThemeRegistry.get_color(:dracula, :red, :rgba)    # %{r: 255, g: 85, b: 85, a: 1.0}

# Convert entire theme
ThemeRegistry.to_terminal_format(:dracula)
ThemeRegistry.to_liveview_format(:dracula)

Summary

Functions

Converts a hex color to the specified format.

Returns the default theme name.

Gets a theme by name.

Gets a theme by name, returning nil if not found.

Gets a specific color from a theme.

Lists all available theme names.

Lists all themes with their display names.

Converts a theme to LiveView format.

Converts a theme to terminal manager format (RGBA maps).

Types

color_format()

@type color_format() :: :hex | :rgb | :rgba

hex_color()

@type hex_color() :: String.t()

rgb_color()

@type rgb_color() :: {non_neg_integer(), non_neg_integer(), non_neg_integer()}

rgba_color()

@type rgba_color() :: %{
  r: non_neg_integer(),
  g: non_neg_integer(),
  b: non_neg_integer(),
  a: float()
}

theme()

@type theme() :: %{
  name: theme_name(),
  display_name: String.t(),
  description: String.t(),
  dark_mode: boolean(),
  ui: %{
    background: hex_color(),
    foreground: hex_color(),
    cursor: hex_color(),
    selection: hex_color()
  },
  colors: %{
    black: hex_color(),
    red: hex_color(),
    green: hex_color(),
    yellow: hex_color(),
    blue: hex_color(),
    magenta: hex_color(),
    cyan: hex_color(),
    white: hex_color(),
    bright_black: hex_color(),
    bright_red: hex_color(),
    bright_green: hex_color(),
    bright_yellow: hex_color(),
    bright_blue: hex_color(),
    bright_magenta: hex_color(),
    bright_cyan: hex_color(),
    bright_white: hex_color()
  }
}

theme_name()

@type theme_name() :: atom()

Functions

convert_color(hex, arg2)

@spec convert_color(hex_color() | nil, color_format()) ::
  hex_color() | rgb_color() | rgba_color() | nil

Converts a hex color to the specified format.

default()

@spec default() :: :dracula

Returns the default theme name.

get(name)

@spec get(theme_name()) :: {:ok, theme()} | {:error, :not_found}

Gets a theme by name.

Returns {:ok, theme} or {:error, :not_found}.

get!(name)

@spec get!(theme_name()) :: theme() | nil

Gets a theme by name, returning nil if not found.

get_color(theme_name, color_name, format \\ :hex)

@spec get_color(theme_name(), atom(), color_format()) ::
  hex_color() | rgb_color() | rgba_color() | nil

Gets a specific color from a theme.

Options

  • :hex (default) - Returns hex string
  • :rgb - Returns {r, g, b} tuple
  • :rgba - Returns %{r: r, g: g, b: b, a: 1.0} map

list()

@spec list() :: [theme_name()]

Lists all available theme names.

list_with_names()

@spec list_with_names() :: [{theme_name(), String.t()}]

Lists all themes with their display names.

to_liveview_format(theme_name)

@spec to_liveview_format(theme_name()) ::
  %{
    name: atom(),
    background: hex_color(),
    foreground: hex_color(),
    cursor: hex_color(),
    selection: hex_color(),
    colors: map()
  }
  | nil

Converts a theme to LiveView format.

to_terminal_format(theme_name)

@spec to_terminal_format(theme_name()) ::
  %{
    name: String.t(),
    description: String.t(),
    author: String.t(),
    version: String.t(),
    colors: map()
  }
  | nil

Converts a theme to terminal manager format (RGBA maps).