Pote.Gradients (Pote v1.0.0)

Copy Markdown View Source

Linear and multi-stop color gradient generation.

All operations are pure functions returning lists of RGB tuples or iodata-ready ANSI strings for terminal output.

Type aliases defined here reference the canonical types in Pote.

Usage

iex> Pote.Gradients.linear({255, 0, 0}, {0, 0, 255}, 5)
[{255, 0, 0}, {191, 0, 64}, {128, 0, 128}, {64, 0, 191}, {0, 0, 255}]

iex> Pote.Gradients.apply_to_text("Hello", {255, 0, 0}, {0, 0, 255})
# returns iodata with ANSI sequences applying the gradient to each character

Summary

Functions

Applies a gradient background to a text string.

Applies a gradient to a text string, coloring each character individually.

Generates a linear gradient between two colors.

Generates a multi-stop gradient across a list of colors.

Converts a list of RGB tuples to their corresponding HSL tuples.

Generates a vertical gradient as a list of lines where each line gets a different color stop.

Types

direction()

@type direction() :: :left_to_right | :right_to_left | :top_to_bottom | :bottom_to_top

rgb()

@type rgb() :: Pote.rgb()

Functions

apply_bg_to_text(text, from, to, text_color \\ {255, 255, 255})

@spec apply_bg_to_text(String.t(), rgb(), rgb(), rgb()) :: iodata()

Applies a gradient background to a text string.

Similar to apply_to_text/4 but applies the gradient to the background color instead of the foreground, using a contrasting text color.

Parameters

  • text - The string to colorize
  • from - Start background color
  • to - End background color
  • text_color - Foreground color for the text (default: white)

apply_to_text(text, from, to, direction \\ :left_to_right)

@spec apply_to_text(String.t(), rgb(), rgb(), direction()) :: iodata()

Applies a gradient to a text string, coloring each character individually.

Returns iodata with ANSI escape sequences applying the gradient from from color to to color across the full string length.

Parameters

  • text - The string to colorize
  • from - Start color
  • to - End color
  • direction - Direction of the gradient (default: :left_to_right)

Examples

iex> apply_to_text("Hi", {255, 0, 0}, {0, 0, 255})
# iodata with each char in a gradient color

linear(from, to, steps)

@spec linear(rgb(), rgb(), pos_integer()) :: [rgb()]

Generates a linear gradient between two colors.

Returns steps RGB tuples evenly interpolated from from to to.

Parameters

  • from - Start color as RGB tuple
  • to - End color as RGB tuple
  • steps - Number of color stops (minimum 2)

Examples

iex> linear({255, 0, 0}, {0, 0, 255}, 3)
[{255, 0, 0}, {128, 0, 128}, {0, 0, 255}]

multicolor(colors, steps)

@spec multicolor([rgb()], pos_integer()) :: [rgb()]

Generates a multi-stop gradient across a list of colors.

Returns steps total RGB tuples interpolated across all provided color stops with equal spacing between stops.

Parameters

  • colors - List of RGB tuples (minimum 2)
  • steps - Total number of output colors

Examples

iex> multicolor([{255,0,0}, {0,255,0}, {0,0,255}], 5)
[{255, 0, 0}, {128, 128, 0}, {0, 255, 0}, {0, 128, 128}, {0, 0, 255}]

to_hsl_stops(colors)

@spec to_hsl_stops([rgb()]) :: [Pote.Conversions.hsl()]

Converts a list of RGB tuples to their corresponding HSL tuples.

Useful for analyzing or transforming gradient stops in HSL space.

vertical_fill(from, to, lines, width, char \\ " ")

@spec vertical_fill(rgb(), rgb(), pos_integer(), pos_integer(), String.t()) ::
  iodata()

Generates a vertical gradient as a list of lines where each line gets a different color stop.

Useful for rendering gradient backgrounds in multi-line UI areas.

Parameters

  • from - Top color
  • to - Bottom color
  • lines - Number of lines (height of the area)
  • width - Width in characters of each line
  • char - Fill character (default: space " ")