Color manipulation utilities for parsing, converting, and manipulating colors.
Supports multiple color spaces: RGB, HSL, HCL (also known as LCH), and Lab. Provides color parsing, conversion, interpolation, and manipulation functions.
Examples
# Parsing various formats
color = Visualize.Color.parse("#ff6600")
color = Visualize.Color.parse("rgb(255, 102, 0)")
color = Visualize.Color.parse("hsl(24, 100%, 50%)")
color = Visualize.Color.parse("steelblue")
# Creating colors in different spaces
color = Visualize.Color.rgb(255, 102, 0)
color = Visualize.Color.hsl(24, 100, 50)
# Conversions
color |> Visualize.Color.to_hex() # => "#ff6600"
color |> Visualize.Color.to_hsl() # => %{h: 24, s: 100, l: 50, ...}
# Manipulation
color |> Visualize.Color.brighter(1.5)
color |> Visualize.Color.darker(0.5)
color |> Visualize.Color.with_opacity(0.5)
# Interpolation
Visualize.Color.interpolate_rgb(color1, color2, 0.5)
Summary
Functions
Returns a brighter copy of the color.
Returns the contrast ratio between two colors (1-21)
Returns a darker copy of the color.
Returns a desaturated copy (in HSL space)
Returns a grayscale version of the color
Creates a color from HCL values (h: 0-360, c: 0-~230, l: 0-100)
Creates a color from HSL values (h: 0-360, s: 0-100, l: 0-100)
Interpolates between two colors in HCL space (perceptually uniform)
Interpolates between two colors in HSL space
Interpolates between two colors in Lab space
Interpolates between two colors in RGB space
Inverts the color
Creates a color from Lab values (l: 0-100, a: -128 to 127, b: -128 to 127)
Returns the relative luminance (0-1)
Mixes two colors equally (in RGB space)
Gets a named color's hex value
Returns all named color names
Parses a color string into a Color struct.
Creates a color from RGB values (0-255)
Rotates the hue by the specified degrees
Returns a saturated copy (in HSL space)
Converts to HCL map %{h: h, c: c, l: l, opacity: opacity}
Converts to hex string
Converts to HSL map %{h: h, s: s, l: l, opacity: a}
Converts to Lab map %{l: l, a: a, b: b, opacity: opacity}
Converts to RGB tuple {r, g, b}
Converts to RGBA tuple {r, g, b, a}
Converts to CSS string
Returns a copy with the specified opacity
Types
Functions
Returns a brighter copy of the color.
k=1 is one "unit" brighter (default). k=2 is two units, etc.
Returns the contrast ratio between two colors (1-21)
Returns a darker copy of the color.
k=1 is one "unit" darker (default). k=2 is two units, etc.
Returns a desaturated copy (in HSL space)
Returns a grayscale version of the color
Creates a color from HCL values (h: 0-360, c: 0-~230, l: 0-100)
Creates a color from HSL values (h: 0-360, s: 0-100, l: 0-100)
Interpolates between two colors in HCL space (perceptually uniform)
Interpolates between two colors in HSL space
Interpolates between two colors in Lab space
Interpolates between two colors in RGB space
Inverts the color
Creates a color from Lab values (l: 0-100, a: -128 to 127, b: -128 to 127)
Returns the relative luminance (0-1)
Mixes two colors equally (in RGB space)
Gets a named color's hex value
@spec named_colors() :: [String.t()]
Returns all named color names
Parses a color string into a Color struct.
Supports:
- Hex: "#rgb", "#rrggbb", "#rgba", "#rrggbbaa"
- RGB: "rgb(r, g, b)", "rgba(r, g, b, a)"
- HSL: "hsl(h, s%, l%)", "hsla(h, s%, l%, a)"
- Named colors: "steelblue", "red", etc.
Creates a color from RGB values (0-255)
Rotates the hue by the specified degrees
Returns a saturated copy (in HSL space)
Converts to HCL map %{h: h, c: c, l: l, opacity: opacity}
Converts to hex string
Converts to HSL map %{h: h, s: s, l: l, opacity: a}
Converts to Lab map %{l: l, a: a, b: b, opacity: opacity}
Converts to RGB tuple {r, g, b}
Converts to RGBA tuple {r, g, b, a}
Converts to CSS string
Returns a copy with the specified opacity