Raxol.Core.Style (Raxol Core v2.0.0)
View SourceStyle management and ANSI escape code generation.
This module provides utilities for creating, merging, and converting terminal styles to ANSI escape codes.
Color Support
- RGB colors (24-bit true color)
- 256-color palette
- Named colors (16 basic ANSI colors)
Style Attributes
- Foreground and background colors
- Bold, italic, underline
- Reverse video
- Strikethrough
Examples
# Create a style
style = Raxol.Core.Style.new(
fg_color: Raxol.Core.Style.rgb(255, 0, 0),
bold: true
)
# Use named colors
style = Raxol.Core.Style.new(
fg_color: Raxol.Core.Style.named_color(:red),
bg_color: Raxol.Core.Style.named_color(:black)
)
# Merge styles
base_style = Raxol.Core.Style.new(bold: true)
colored_style = Raxol.Core.Style.new(fg_color: :red)
merged = Raxol.Core.Style.merge(base_style, colored_style)
# Generate ANSI codes
ansi = Raxol.Core.Style.to_ansi(style)
Summary
Functions
Returns a color from the 256-color palette.
Merges two styles, with the second style taking precedence.
Returns a named color.
Creates a new style with the given attributes.
Creates an RGB color value.
Converts a style to ANSI escape codes.
Types
@type color() :: nil | {non_neg_integer(), non_neg_integer(), non_neg_integer()} | non_neg_integer() | atom()
Functions
@spec color_256(non_neg_integer()) :: color()
Returns a color from the 256-color palette.
Parameters
code- Color code (0-255)
Merges two styles, with the second style taking precedence.
Parameters
style1- Base stylestyle2- Style to merge on top
Returns a named color.
Supported colors: :black, :red, :green, :yellow, :blue,
:magenta, :cyan, :white, :bright_black, :bright_red,
:bright_green, :bright_yellow, :bright_blue, :bright_magenta,
:bright_cyan, :bright_white
Creates a new style with the given attributes.
Parameters
opts- Keyword list of style attributes
Examples
iex> style = Raxol.Core.Style.new(bold: true, fg_color: :red)
%Raxol.Core.Style{bold: true, fg_color: :red}
@spec rgb(non_neg_integer(), non_neg_integer(), non_neg_integer()) :: color()
Creates an RGB color value.
Parameters
r- Red component (0-255)g- Green component (0-255)b- Blue component (0-255)
Converts a style to ANSI escape codes.
Parameters
style- The style to convert
Returns
A string containing the ANSI escape codes for the style.