Raxol.Style.Colors.Color (Raxol v0.3.0)
View SourceCore 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
Functions
Blends two colors with the specified alpha value.
Examples
iex> Color.from_hex("#FF0000") |> Color.alpha_blend(Color.from_hex("#0000FF"), 0.5)
%Color{r: 128, g: 0, b: 128, hex: "#800080"}
Returns the complementary color.
Examples
iex> Color.from_hex("#FF0000") |> Color.complement()
%Color{r: 0, g: 255, b: 255, hex: "#00FFFF"}
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"}
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}
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"}
Creates a color from RGB values.
Examples
iex> Color.from_rgb(255, 0, 0)
%Color{r: 255, g: 0, b: 0, hex: "#FF0000"}
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"}
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"}
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}
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.
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
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
Converts a color to its hex representation.
Examples
iex> color = Color.from_rgb(255, 0, 0)
iex> Color.to_hex(color)
"#FF0000"