Raxol.Style.Colors.Formats (Raxol v0.3.0)

View Source

Color format conversion utilities.

This module provides functions for converting between different color formats:

  • RGB/RGBA tuples
  • Hex strings
  • ANSI color codes
  • Named colors

Summary

Functions

Converts an ANSI color code to RGB values.

Parses a hex string into RGB/RGBA values.

Converts RGB values to an ANSI color code.

Converts a color to its hex representation.

Functions

ansi_to_rgb(code)

@spec ansi_to_rgb(integer()) :: {integer(), integer(), integer()}

Converts an ANSI color code to RGB values.

Examples

iex> Formats.ansi_to_rgb(1)
{205, 0, 0}

from_hex(hex_string)

@spec from_hex(String.t()) ::
  {integer(), integer(), integer()}
  | {integer(), integer(), integer(), integer()}

Parses a hex string into RGB/RGBA values.

Examples

iex> Formats.from_hex("#FF0000")
{255, 0, 0}

iex> Formats.from_hex("#FF000080")
{255, 0, 0, 128}

rgb_to_ansi(arg)

@spec rgb_to_ansi({integer(), integer(), integer()}) :: integer()

Converts RGB values to an ANSI color code.

Examples

iex> Formats.rgb_to_ansi({255, 0, 0})
196

to_hex(arg)

@spec to_hex(
  {integer(), integer(), integer()}
  | {integer(), integer(), integer(), integer()}
) ::
  String.t()

Converts a color to its hex representation.

Examples

iex> Formats.to_hex({255, 0, 0})
"#FF0000"

iex> Formats.to_hex({255, 0, 0, 128})
"#FF000080"