Raxol.Core.Style (Raxol v2.6.0)

View Source

Style utilities for Raxol.Core.Buffer.

Provides functions to create, merge, and convert styles for terminal rendering. Supports named colors, RGB colors, and 256-color palette.

Example

style = Raxol.Core.Style.new(bold: true, fg_color: :blue)
ansi = Raxol.Core.Style.to_ansi(style)
# => ""

Summary

Functions

Create a 256-color palette index.

Merge two style maps, with override values taking precedence.

Validate and return a named color atom.

Create a new style map with the given options.

Reset ANSI sequence to clear all styling.

Create an RGB color tuple.

Convert a style map to ANSI escape codes.

Types

color()

t()

@type t() :: %{
  bold: boolean(),
  italic: boolean(),
  underline: boolean(),
  fg_color: color() | nil,
  bg_color: color() | nil
}

Functions

color_256(index)

@spec color_256(0..255) :: 0..255

Create a 256-color palette index.

Example

Raxol.Core.Style.color_256(196)
# => 196

merge(base, override)

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

Merge two style maps, with override values taking precedence.

Example

base = Raxol.Core.Style.new(bold: true, fg_color: :red)
override = Raxol.Core.Style.new(fg_color: :blue)
Raxol.Core.Style.merge(base, override)
# => %{bold: true, ..., fg_color: :blue, ...}

named_color(color)

@spec named_color(
  :black
  | :red
  | :green
  | :yellow
  | :blue
  | :magenta
  | :cyan
  | :white
) ::
  :black | :red | :green | :yellow | :blue | :magenta | :cyan | :white

Validate and return a named color atom.

Valid colors: :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white

Example

Raxol.Core.Style.named_color(:blue)
# => :blue

new(opts \\ [])

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

Create a new style map with the given options.

Options

  • :bold - Boolean, enables bold text
  • :italic - Boolean, enables italic text
  • :underline - Boolean, enables underlined text
  • :fg_color - Foreground color (atom, integer 0-255, or RGB tuple)
  • :bg_color - Background color (atom, integer 0-255, or RGB tuple)

Example

Raxol.Core.Style.new(bold: true, fg_color: :blue)
# => %{bold: true, italic: false, underline: false, fg_color: :blue, bg_color: nil}

reset()

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

Reset ANSI sequence to clear all styling.

Example

Raxol.Core.Style.reset()
# => ""

rgb(r, g, b)

@spec rgb(0..255, 0..255, 0..255) :: {0..255, 0..255, 0..255}

Create an RGB color tuple.

Example

Raxol.Core.Style.rgb(255, 100, 50)
# => {255, 100, 50}

to_ansi(style)

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

Convert a style map to ANSI escape codes.

Example

style = Raxol.Core.Style.new(bold: true, fg_color: :blue)
Raxol.Core.Style.to_ansi(style)
# => ""