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

View Source

Core color representation and manipulation module.

This module provides a unified interface for working with colors in Raxol, supporting various color formats and operations.

Color Formats

  • RGB: {r, g, b} where each component is 0-255
  • RGBA: {r, g, b, a} where alpha is 0-255
  • Hex: "#RRGGBB" or "#RRGGBBAA"
  • ANSI: 0-255 for 256-color mode, 0-15 for basic colors
  • Named: :red, :blue, etc.

Examples

iex> Color.from_hex("#FF0000")
%Color{r: 255, g: 0, b: 0, hex: "#FF0000"}

iex> Color.from_rgb(255, 0, 0)
%Color{r: 255, g: 0, b: 0, hex: "#FF0000"}

iex> Color.from_ansi(1)
%Color{r: 205, g: 0, b: 0, hex: "#CD0000", ansi_code: 1}

Summary

Functions

Blends two colors with the specified alpha value.

Returns the complementary color.

Darkens a color by the specified amount.

Creates a color from an ANSI color code.

Creates a color from a hex string.

Creates a color from RGB values.

Creates a color from RGBA values.

Lightens a color by the specified amount.

Mixes two colors with the specified weight (0.0 to 1.0).

Converts a color to an ANSI code (currently defaults to 256-color code). The type parameter (:foreground or :background) is currently ignored.

Converts a color to the closest ANSI 16-color code.

Converts a color to the closest ANSI 256-color code.

Converts a color to its hex representation.

Types

t()

@type t() :: %Raxol.Style.Colors.Color{
  a: integer() | nil,
  ansi_code: integer() | nil,
  b: integer(),
  g: integer(),
  hex: String.t(),
  name: String.t() | nil,
  r: integer()
}

Functions

blend(color1, color2, alpha)

@spec blend(t(), t(), float()) :: t()

Blends two colors with the specified alpha value.

Examples

iex> Color.blend(Color.from_hex("#FF0000"), Color.from_hex("#0000FF"), 0.5)
%Color{r: 128, g: 0, b: 128, hex: "#800080"}

complement(color)

@spec complement(t()) :: t()

Returns the complementary color.

Examples

iex> Color.from_hex("#FF0000") |> Color.complement()
%Color{r: 0, g: 255, b: 255, hex: "#00FFFF"}

darken(color, amount)

@spec darken(t(), float()) :: t()

Darkens a color by the specified amount.

Examples

iex> Color.from_hex("#FFFFFF") |> Color.darken(0.5)
%Color{r: 128, g: 128, b: 128, hex: "#808080"}

from_ansi(code)

@spec from_ansi(integer()) :: t()

Creates a color from an ANSI color code.

Examples

iex> Color.from_ansi(1)
%Color{r: 205, g: 0, b: 0, hex: "#CD0000", ansi_code: 1}

from_hex(hex_string)

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

Creates a color from a hex string.

Examples

iex> Color.from_hex("#FF0000")
%Color{r: 255, g: 0, b: 0, hex: "#FF0000"}

iex> Color.from_hex("#FF000080")
%Color{r: 255, g: 0, b: 0, a: 128, hex: "#FF000080"}

from_rgb(r, g, b)

@spec from_rgb(integer(), integer(), integer()) :: t()

Creates a color from RGB values.

Examples

iex> Color.from_rgb(255, 0, 0)
%Color{r: 255, g: 0, b: 0, hex: "#FF0000"}

from_rgba(r, g, b, a)

@spec from_rgba(integer(), integer(), integer(), integer()) :: t()

Creates a color from RGBA values.

Examples

iex> Color.from_rgba(255, 0, 0, 128)
%Color{r: 255, g: 0, b: 0, a: 128, hex: "#FF000080"}

lighten(color, amount)

@spec lighten(t(), float()) :: t()

Lightens a color by the specified amount.

Examples

iex> Color.from_hex("#000000") |> Color.lighten(0.5)
%Color{r: 128, g: 128, b: 128, hex: "#808080"}

mix(color1, color2, weight \\ 0.5)

Mixes two colors with the specified weight (0.0 to 1.0).

Examples

iex> color1 = Color.from_rgb(255, 0, 0)
iex> color2 = Color.from_rgb(0, 0, 255)
iex> mixed = Color.mix(color1, color2, 0.5)
iex> {mixed.r, mixed.g, mixed.b}
{127, 0, 127}

to_ansi(color, type)

@spec to_ansi(t(), :foreground | :background) :: integer()

Converts a color to an ANSI code (currently defaults to 256-color code). The type parameter (:foreground or :background) is currently ignored.

NOTE: This is a placeholder. Implement proper ANSI sequence generation based on type and terminal capabilities in the future.

to_ansi_16(color)

Converts a color to the closest ANSI 16-color code.

Examples

iex> color = Color.from_rgb(255, 0, 0)
iex> Color.to_ansi_16(color)
9

to_ansi_256(color)

Converts a color to the closest ANSI 256-color code.

Examples

iex> color = Color.from_rgb(255, 0, 0)
iex> Color.to_ansi_256(color)
196

to_hex(color)

Converts a color to its hex representation.

Examples

iex> color = Color.from_rgb(255, 0, 0)
iex> Color.to_hex(color)
"#FF0000"