Raxol.Style.Colors.Color (Raxol v0.2.0)
View SourceRepresents a color in various formats with conversion utilities. Supports ANSI 16/256 colors and True Color (24-bit).
Examples
# Creating colors
red = Raxol.Style.Colors.Color.from_hex("#FF0000")
blue = Raxol.Style.Colors.Color.from_rgb(0, 0, 255)
green = Raxol.Style.Colors.Color.from_ansi(2)
# Converting colors
hex_code = Raxol.Style.Colors.Color.to_hex(red)
ansi_code = Raxol.Style.Colors.Color.to_ansi_256(blue)
# Color operations
light_red = Raxol.Style.Colors.Color.lighten(red, 0.2)
dark_blue = Raxol.Style.Colors.Color.darken(blue, 0.3)
purple = Raxol.Style.Colors.Color.mix(red, blue, 0.5)
Summary
Functions
Blends two colors with the specified alpha value (0.0 to 1.0).
Returns the complementary color.
Darkens a color by the specified amount (0.0 to 1.0).
Creates a color from an ANSI color code.
Creates a color from a hex string.
Creates a color from RGB values.
Lightens a color by the specified amount (0.0 to 1.0).
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 (0.0 to 1.0).
Examples
iex> color1 = Raxol.Style.Colors.Color.from_rgb(255, 0, 0)
iex> color2 = Raxol.Style.Colors.Color.from_rgb(0, 0, 255)
iex> blended = Raxol.Style.Colors.Color.alpha_blend(color1, color2, 0.5)
iex> {blended.r, blended.g, blended.b}
{127, 0, 127}
Returns the complementary color.
Examples
iex> color = Raxol.Style.Colors.Color.from_rgb(255, 0, 0)
iex> complement = Raxol.Style.Colors.Color.complement(color)
iex> {complement.r, complement.g, complement.b}
{0, 255, 255}
Darkens a color by the specified amount (0.0 to 1.0).
Examples
iex> color = Raxol.Style.Colors.Color.from_rgb(200, 200, 200)
iex> darkened = Raxol.Style.Colors.Color.darken(color, 0.5)
iex> {darkened.r, darkened.g, darkened.b}
{100, 100, 100}
Creates a color from an ANSI color code.
Examples
iex> Raxol.Style.Colors.Color.from_ansi(1)
%Raxol.Style.Colors.Color{r: 128, g: 0, b: 0, ansi_code: 1, hex: "#800000"}
Creates a color from a hex string.
Accepts hex strings in the following formats:
- "#RGB"
- "#RRGGBB"
- "RGB"
- "RRGGBB"
Returns {:error, :invalid_hex}
if the string is invalid.
Examples
iex> Raxol.Style.Colors.Color.from_hex("#FF0000")
%Raxol.Style.Colors.Color{r: 255, g: 0, b: 0, hex: "#FF0000"}
iex> Raxol.Style.Colors.Color.from_hex("0F0")
%Raxol.Style.Colors.Color{r: 0, g: 255, b: 0, hex: "#00FF00"}
@spec from_rgb(0..255, 0..255, 0..255) :: t()
Creates a color from RGB values.
Examples
iex> Raxol.Style.Colors.Color.from_rgb(255, 0, 0)
%Raxol.Style.Colors.Color{r: 255, g: 0, b: 0, hex: "#FF0000"}
Lightens a color by the specified amount (0.0 to 1.0).
Examples
iex> color = Raxol.Style.Colors.Color.from_rgb(100, 100, 100)
iex> lightened = Raxol.Style.Colors.Color.lighten(color, 0.5)
iex> {lightened.r, lightened.g, lightened.b}
{177, 177, 177}
Mixes two colors with the specified weight (0.0 to 1.0).
Examples
iex> color1 = Raxol.Style.Colors.Color.from_rgb(255, 0, 0)
iex> color2 = Raxol.Style.Colors.Color.from_rgb(0, 0, 255)
iex> mixed = Raxol.Style.Colors.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.
TODO: Implement proper ANSI sequence generation based on type and terminal capabilities.
Converts a color to the closest ANSI 16-color code.
Examples
iex> color = Raxol.Style.Colors.Color.from_rgb(255, 0, 0)
iex> Raxol.Style.Colors.Color.to_ansi_16(color)
9
Converts a color to the closest ANSI 256-color code.
Examples
iex> color = Raxol.Style.Colors.Color.from_rgb(255, 0, 0)
iex> Raxol.Style.Colors.Color.to_ansi_256(color)
196
Converts a color to its hex representation.
Examples
iex> color = Raxol.Style.Colors.Color.from_rgb(255, 0, 0)
iex> Raxol.Style.Colors.Color.to_hex(color)
"#FF0000"