Raxol.Style.Colors.Gradient (Raxol v0.5.0)

View Source

Creates and manages color gradients for terminal applications.

This module provides functionality for creating gradients between colors and applying them to text, creating visually striking terminal effects.

Examples

# Create a simple linear gradient between red and blue
alias Raxol.Style.Colors.{Color, Gradient}

red = Color.from_hex("#FF0000")
blue = Color.from_hex("#0000FF")
gradient = Gradient.linear(red, blue, 10)

# Apply the gradient to text
colored_text = Gradient.apply_to_text(gradient, "Hello, World!")

# Create a rainbow gradient
rainbow = Gradient.rainbow(20)
rainbow_text = Gradient.apply_to_text(rainbow, "Rainbow Text")

# Create a multi-stop gradient
colors = [
  Color.from_hex("#FF0000"),  # Red
  Color.from_hex("#FFFF00"),  # Yellow
  Color.from_hex("#00FF00")   # Green
]
multi = Gradient.multi_stop(colors, 15)

Summary

Functions

Applies a gradient to text, returning an ANSI-formatted string.

Gets the color at a specific position in the gradient.

Creates a heat map gradient from cool to hot colors.

Creates a linear gradient between two colors.

Creates a multi-stop gradient with multiple color stops.

Creates a rainbow gradient with the given number of steps.

Reverses the direction of a gradient.

Alias for apply_to_text/2.

Types

gradient_type()

@type gradient_type() :: :linear | :rainbow | :heat_map | :multi_stop

t()

@type t() :: %Raxol.Style.Colors.Gradient{
  colors: [Raxol.Style.Colors.Color.t()],
  steps: non_neg_integer(),
  type: gradient_type()
}

Functions

apply_to_text(gradient, text)

Applies a gradient to text, returning an ANSI-formatted string.

Parameters

  • gradient - The gradient to apply
  • text - The text to colorize

Examples

iex> red = Raxol.Style.Colors.Color.from_hex("#FF0000")
iex> blue = Raxol.Style.Colors.Color.from_hex("#0000FF")
iex> gradient = Raxol.Style.Colors.Gradient.linear(red, blue, 5)
iex> Raxol.Style.Colors.Gradient.apply_to_text(gradient, "Hello")
"Hello"

at_position(gradient, position)

Gets the color at a specific position in the gradient.

Parameters

  • gradient - The gradient to sample from
  • position - A value between 0.0 and 1.0 representing the position in the gradient

Examples

iex> red = Raxol.Style.Colors.Color.from_hex("#FF0000")
iex> blue = Raxol.Style.Colors.Color.from_hex("#0000FF")
iex> gradient = Raxol.Style.Colors.Gradient.linear(red, blue, 5)
iex> color = Raxol.Style.Colors.Gradient.at_position(gradient, 0.5)
iex> color.hex
"#800080"  # Purple (mix of red and blue)

heat_map(steps)

Creates a heat map gradient from cool to hot colors.

Parameters

  • steps - The number of color steps in the heat map

Examples

iex> gradient = Raxol.Style.Colors.Gradient.heat_map(5)
iex> length(gradient.colors)
5

linear(start_color, end_color, steps)

Creates a linear gradient between two colors.

Parameters

  • start_color - The starting color
  • end_color - The ending color
  • steps - The number of color steps in the gradient (including start and end)

Examples

iex> red = Raxol.Style.Colors.Color.from_hex("#FF0000")
iex> blue = Raxol.Style.Colors.Color.from_hex("#0000FF")
iex> gradient = Raxol.Style.Colors.Gradient.linear(red, blue, 5)
iex> length(gradient.colors)
5

multi_stop(color_stops, steps)

Creates a multi-stop gradient with multiple color stops.

Parameters

  • color_stops - A list of colors to transition between
  • steps - The total number of color steps in the gradient

Examples

iex> colors = [
...>   Raxol.Style.Colors.Color.from_hex("#FF0000"),
...>   Raxol.Style.Colors.Color.from_hex("#00FF00"),
...>   Raxol.Style.Colors.Color.from_hex("#0000FF")
...> ]
iex> gradient = Raxol.Style.Colors.Gradient.multi_stop(colors, 10)
iex> length(gradient.colors)
10

rainbow(steps)

Creates a rainbow gradient with the given number of steps.

Parameters

  • steps - The number of color steps in the rainbow

Examples

iex> gradient = Raxol.Style.Colors.Gradient.rainbow(6)
iex> length(gradient.colors)
6

reverse(gradient)

Reverses the direction of a gradient.

Parameters

  • gradient - The gradient to reverse

Examples

iex> red = Raxol.Style.Colors.Color.from_hex("#FF0000")
iex> blue = Raxol.Style.Colors.Color.from_hex("#0000FF")
iex> gradient = Raxol.Style.Colors.Gradient.linear(red, blue, 3)
iex> reversed = Raxol.Style.Colors.Gradient.reverse(gradient)
iex> hd(reversed.colors).hex
"#0000FF"

to_ansi_sequence(gradient, text)

Alias for apply_to_text/2.