Raxol.Core.Style (Raxol Core v2.0.0)

View Source

Style 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

color()

@type color() ::
  nil
  | {non_neg_integer(), non_neg_integer(), non_neg_integer()}
  | non_neg_integer()
  | atom()

t()

@type t() :: %Raxol.Core.Style{
  bg_color: color(),
  bold: boolean(),
  fg_color: color(),
  italic: boolean(),
  reverse: boolean(),
  strikethrough: boolean(),
  underline: boolean()
}

Functions

color_256(code)

@spec color_256(non_neg_integer()) :: color()

Returns a color from the 256-color palette.

Parameters

  • code - Color code (0-255)

merge(style1, style2)

@spec merge(t(), t()) :: t()

Merges two styles, with the second style taking precedence.

Parameters

  • style1 - Base style
  • style2 - Style to merge on top

named_color(name)

@spec named_color(atom()) :: color()

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

new(opts \\ [])

@spec new(keyword()) :: t()

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}

rgb(r, g, b)

Creates an RGB color value.

Parameters

  • r - Red component (0-255)
  • g - Green component (0-255)
  • b - Blue component (0-255)

to_ansi(style)

@spec to_ansi(t()) :: String.t()

Converts a style to ANSI escape codes.

Parameters

  • style - The style to convert

Returns

A string containing the ANSI escape codes for the style.