Raxol.Style.Colors.Adaptive (Raxol v0.5.0)
View SourceDetects terminal capabilities and adapts color schemes accordingly.
This module provides functionality to detect what color capabilities are supported by the current terminal and adapt colors and themes to work optimally with the detected capabilities.
Examples
# Check if the terminal supports true color
if Raxol.Style.Colors.Adaptive.supports_true_color?() do
# Use true color features
else
# Fall back to 256 colors or 16 colors
end
# Adapt a color to the current terminal capabilities
color = Raxol.Style.Colors.Color.from_hex("#FF5500")
adapted_color = Raxol.Style.Colors.Adaptive.adapt_color(color)
# Check if we're in a dark terminal
if Raxol.Style.Colors.Adaptive.dark_terminal?() do
# Use light text on dark background
else
# Use dark text on light background
end
Summary
Functions
Adapts a color to the current terminal capabilities.
Adapts a theme (canonical structure) to the current terminal capabilities and background.
Checks if the terminal has a dark background.
Detects the color support level of the current terminal.
Gets the optimal color format for the current terminal.
Initializes the terminal capabilities cache.
Gets the optimal color format for the current terminal.
Resets the cached terminal capabilities.
Checks if the terminal supports 256 colors.
Checks if the terminal supports true color (24-bit color).
Detects the terminal background color (light or dark).
Functions
Adapts a color to the current terminal capabilities.
If the terminal does not support the full color range, this will convert the color to the best available representation.
Parameters
color
- The color to adapt
Examples
iex> color = Raxol.Style.Colors.Color.from_hex("#FF5500")
iex> adapted = Raxol.Style.Colors.Adaptive.adapt_color(color)
iex> adapted.hex
"#FF5500" # If terminal supports true color, otherwise closest supported color
Adapts a theme (canonical structure) to the current terminal capabilities and background.
Parameters
theme
- The canonical theme map to adapt
Examples
iex> theme = Raxol.UI.Theming.Theme.default_theme()
iex> adapted = Raxol.Style.Colors.Adaptive.adapt_theme(theme)
iex> adapted.name
"default (Adapted)" # Adapted to terminal capabilities
Checks if the terminal has a dark background.
Examples
iex> Raxol.Style.Colors.Adaptive.dark_terminal?()
true # Depends on your terminal
Detects the color support level of the current terminal.
Returns one of:
:true_color
- 24-bit color (16 million colors):ansi_256
- 256 colors:ansi_16
- 16 colors:no_color
- No color support
Examples
iex> Raxol.Style.Colors.Adaptive.detect_color_support()
:true_color # Depends on your terminal
Gets the optimal color format for the current terminal.
Returns one of:
:true_color
- 24-bit color (16 million colors):ansi_256
- 256 colors:ansi_16
Examples
iex> Raxol.Style.Colors.Adaptive.get_optimal_format()
:true_color # Depends on your terminal
Initializes the terminal capabilities cache.
This should be called once, usually during application startup. It creates the ETS table used for caching detected capabilities.
Gets the optimal color format for the current terminal.
Returns one of:
:true_color
- 24-bit color (16 million colors):ansi_256
- 256 colors:ansi_16
- 16 colors:no_color
- No color support
Examples
iex> Raxol.Style.Colors.Adaptive.optimal_format()
:true_color # Depends on your terminal
Resets the cached terminal capabilities.
This forces the next call to detection functions to re-evaluate the terminal environment.
Checks if the terminal supports 256 colors.
Examples
iex> Raxol.Style.Colors.Adaptive.supports_256_colors?()
true # Depends on your terminal
Checks if the terminal supports true color (24-bit color).
Examples
iex> Raxol.Style.Colors.Adaptive.supports_true_color?()
true # Depends on your terminal
Detects the terminal background color (light or dark).
Returns one of:
:dark
- Dark background:light
- Light background:unknown
- Unable to determine
Examples
iex> Raxol.Style.Colors.Adaptive.terminal_background()
:dark # Depends on your terminal