Raxol.Style.Colors.Gradient (Raxol v0.2.0)
View SourceCreates 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
@type gradient_type() :: :linear | :rainbow | :heat_map | :multi_stop
@type t() :: %Raxol.Style.Colors.Gradient{ colors: [Raxol.Style.Colors.Color.t()], steps: non_neg_integer(), type: gradient_type() }
Functions
Applies a gradient to text, returning an ANSI-formatted string.
Parameters
gradient
- The gradient to applytext
- 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")
# Returns ANSI-colored text with each character a different color
Gets the color at a specific position in the gradient.
Parameters
gradient
- The gradient to sample fromposition
- 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)
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
Creates a linear gradient between two colors.
Parameters
start_color
- The starting colorend_color
- The ending colorsteps
- 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
Creates a multi-stop gradient with multiple color stops.
Parameters
color_stops
- A list of colors to transition betweensteps
- 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
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
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"
Alias for apply_to_text/2.