Raxol. Core. Theming. ThemeRegistry
(Raxol v2.6.0)
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.
Converts a hex string to RGB tuple.
Delegates to Raxol.Utils.ColorConversion.
Converts a hex string to RGBA map.
Lists all available theme names.
Lists all themes with their display names.
Converts an RGB tuple to hex string.
Delegates to Raxol.Utils.ColorConversion.
Converts a theme to LiveView format.
Converts a theme to terminal manager format (RGBA maps).
Types
@type color_format() :: :hex | :rgb | :rgba
@type hex_color() :: String.t()
@type rgb_color() :: {non_neg_integer(), non_neg_integer(), non_neg_integer()}
@type rgba_color() :: %{ r: non_neg_integer(), g: non_neg_integer(), b: non_neg_integer(), a: float() }
@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() } }
@type theme_name() :: atom()
Functions
@spec convert_color(hex_color() | nil, color_format()) :: hex_color() | rgb_color() | rgba_color() | nil
Converts a hex color to the specified format.
@spec default() :: :dracula
Returns the default theme name.
@spec get(theme_name()) :: {:ok, theme()} | {:error, :not_found}
Gets a theme by name.
Returns {:ok, theme} or {:error, :not_found}.
@spec get!(theme_name()) :: theme() | nil
Gets a theme by name, returning nil if not found.
@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
Converts a hex string to RGB tuple.
Delegates to Raxol.Utils.ColorConversion.
@spec hex_to_rgba(hex_color()) :: rgba_color()
Converts a hex string to RGBA map.
@spec list() :: [theme_name()]
Lists all available theme names.
@spec list_with_names() :: [{theme_name(), String.t()}]
Lists all themes with their display names.
Converts an RGB tuple to hex string.
Delegates to Raxol.Utils.ColorConversion.
@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.
@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).