Visualize.Color (Visualize v0.2.25)

Copy Markdown View Source

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

t()

@type t() :: %Visualize.Color{
  b: number(),
  g: number(),
  opacity: number(),
  r: number()
}

Functions

brighter(color, k \\ 1)

@spec brighter(t(), number()) :: t()

Returns a brighter copy of the color.

k=1 is one "unit" brighter (default). k=2 is two units, etc.

contrast(c1, c2)

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

Returns the contrast ratio between two colors (1-21)

darker(color, k \\ 1)

@spec darker(t(), number()) :: t()

Returns a darker copy of the color.

k=1 is one "unit" darker (default). k=2 is two units, etc.

desaturate(color, amount \\ 0.8)

@spec desaturate(t(), number()) :: t()

Returns a desaturated copy (in HSL space)

grayscale(color)

@spec grayscale(t()) :: t()

Returns a grayscale version of the color

hcl(h, c, l, opacity \\ 1.0)

@spec hcl(number(), number(), number(), number()) :: t()

Creates a color from HCL values (h: 0-360, c: 0-~230, l: 0-100)

hsl(h, s, l, opacity \\ 1.0)

@spec hsl(number(), number(), number(), number()) :: t()

Creates a color from HSL values (h: 0-360, s: 0-100, l: 0-100)

interpolate_hcl(c1, c2, t)

@spec interpolate_hcl(t(), t(), number()) :: t()

Interpolates between two colors in HCL space (perceptually uniform)

interpolate_hsl(c1, c2, t)

@spec interpolate_hsl(t(), t(), number()) :: t()

Interpolates between two colors in HSL space

interpolate_lab(c1, c2, t)

@spec interpolate_lab(t(), t(), number()) :: t()

Interpolates between two colors in Lab space

interpolate_rgb(c1, c2, t)

@spec interpolate_rgb(t(), t(), number()) :: t()

Interpolates between two colors in RGB space

invert(color)

@spec invert(t()) :: t()

Inverts the color

lab(l, a, b, opacity \\ 1.0)

@spec lab(number(), number(), number(), number()) :: t()

Creates a color from Lab values (l: 0-100, a: -128 to 127, b: -128 to 127)

luminance(color)

@spec luminance(t()) :: float()

Returns the relative luminance (0-1)

mix(c1, c2)

@spec mix(t(), t()) :: t()

Mixes two colors equally (in RGB space)

named(name)

@spec named(String.t()) :: String.t() | nil

Gets a named color's hex value

named_colors()

@spec named_colors() :: [String.t()]

Returns all named color names

parse(string)

@spec parse(String.t()) :: t() | nil

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.

rgb(r, g, b, opacity \\ 1.0)

@spec rgb(number(), number(), number(), number()) :: t()

Creates a color from RGB values (0-255)

rotate(color, degrees)

@spec rotate(t(), number()) :: t()

Rotates the hue by the specified degrees

saturate(color, amount \\ 1.2)

@spec saturate(t(), number()) :: t()

Returns a saturated copy (in HSL space)

to_hcl(color)

@spec to_hcl(t()) :: %{h: float(), c: float(), l: float(), opacity: float()}

Converts to HCL map %{h: h, c: c, l: l, opacity: opacity}

to_hex(color)

@spec to_hex(t()) :: String.t()

Converts to hex string

to_hsl(color)

@spec to_hsl(t()) :: %{h: float(), s: float(), l: float(), opacity: float()}

Converts to HSL map %{h: h, s: s, l: l, opacity: a}

to_lab(color)

@spec to_lab(t()) :: %{l: float(), a: float(), b: float(), opacity: float()}

Converts to Lab map %{l: l, a: a, b: b, opacity: opacity}

to_rgb(color)

@spec to_rgb(t()) :: {integer(), integer(), integer()}

Converts to RGB tuple {r, g, b}

to_rgba(color)

@spec to_rgba(t()) :: {integer(), integer(), integer(), float()}

Converts to RGBA tuple {r, g, b, a}

to_string(color)

@spec to_string(t()) :: String.t()

Converts to CSS string

with_opacity(color, opacity)

@spec with_opacity(t(), number()) :: t()

Returns a copy with the specified opacity