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

View Source

Detects 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.is_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 palette to the current terminal capabilities.

Adapts a theme to the current terminal capabilities.

Detects the color support level of the current terminal.

Gets the optimal color format for the current terminal.

Initializes the terminal capabilities cache.

Checks if the terminal has a dark background.

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

adapt_color(color)

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

adapt_palette(palette)

Adapts a palette to the current terminal capabilities.

Parameters

  • palette - The palette to adapt

Examples

iex> palette = Raxol.Style.Colors.Palette.nord()
iex> adapted = Raxol.Style.Colors.Adaptive.adapt_palette(palette)
iex> adapted.name
"Nord (Adapted)"  # Adapted to terminal capabilities

adapt_theme(theme)

Adapts a theme to the current terminal capabilities.

Parameters

  • theme - The theme to adapt

Examples

iex> theme = Raxol.Style.Colors.Theme.from_palette(Raxol.Style.Colors.Palette.nord())
iex> adapted = Raxol.Style.Colors.Adaptive.adapt_theme(theme)
iex> adapted.name
"Nord (Adapted)"  # Adapted to terminal capabilities

detect_color_support()

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

get_optimal_format()

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

This is currently an alias for detect_color_support/0.

init()

Initializes the terminal capabilities cache.

This should be called once, usually during application startup. It creates the ETS table used for caching detected capabilities.

is_dark_terminal?()

Checks if the terminal has a dark background.

Examples

iex> Raxol.Style.Colors.Adaptive.is_dark_terminal?()
true  # Depends on your terminal

optimal_format()

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

reset_detection()

Resets the cached terminal capabilities.

This forces the next call to detection functions to re-evaluate the terminal environment.

supports_256_colors?()

Checks if the terminal supports 256 colors.

Examples

iex> Raxol.Style.Colors.Adaptive.supports_256_colors?()
true  # Depends on your terminal

supports_true_color?()

Checks if the terminal supports true color (24-bit color).

Examples

iex> Raxol.Style.Colors.Adaptive.supports_true_color?()
true  # Depends on your terminal

terminal_background()

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