# `Visualize.Color`
[🔗](https://github.com/dcoai/Visualize/blob/v0.8.3/lib/visualize/color.ex#L1)

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)

## Typed colors

A design names a color as a typed value (spec/08 §1.9, D-173): `color(:red)` from the
theme's color table, `ramp(:thermal, 0.7)`, a literal string, or a transform of colors
— `intensity/2`, `blend/1`, `alpha/2`, `lighten/2`, `darken/2`, `saturate/2` and
`contrast/1` — computed in OKLab. Each takes the color first, so colors pipe; the
helpers build data, and `resolve/3` resolves it against a theme at render.

    import Visualize.Color, only: [color: 1, intensity: 2, blend: 1, alpha: 2]

    [color(:blue) |> intensity(0.65), color(:red) |> intensity(0.3)]
    |> blend()
    |> alpha(0.65)

Import the helpers by name: a bare `import Visualize.Color` also brings `to_string/1`,
which a call would find beside `Kernel`'s.

# `t`

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

# `value`

```elixir
@type value() ::
  String.t()
  | {:color, atom()}
  | {:series, pos_integer()}
  | {:ramp, atom(), number()}
  | {:intensity | :alpha | :lighten | :darken | :saturate | :shade, value(),
     number()}
  | {:blend, [value(), ...]}
  | {:contrast, value() | :under}
```

A color as a design names it (spec/08 §1.9): a name of the theme's color table, a series
or a ramp position, a literal string, or a transform of colors.

# `alpha`

```elixir
@spec alpha(value(), number()) :: value()
```

The color `c` at opacity `a` in `0..1`, whatever its opacity was (spec/08 §1.9).

    iex> Visualize.Color.color(:red) |> Visualize.Color.alpha(0.5)
    {:alpha, {:color, :red}, 0.5}

# `blend`

```elixir
@spec blend([value(), ...]) :: value()
```

The colors mixed in OKLab, each weighted by its `intensity/2` (1 without one), as CSS's
`color-mix` mixes them: weights summing below 1 leave the remainder transparent, and
above 1 are scaled to sum to 1 (spec/08 §1.9).

    iex> import Visualize.Color, only: [color: 1, intensity: 2, blend: 1, alpha: 2]
    iex> [color(:blue) |> intensity(0.65), color(:red) |> intensity(0.3)] |> blend() |> alpha(0.65)
    {:alpha, {:blend, [{:intensity, {:color, :blue}, 0.65}, {:intensity, {:color, :red}, 0.3}]}, 0.65}

# `brighter`

```elixir
@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.

# `color`

```elixir
@spec color(atom() | String.t()) :: value()
```

The color `name` of the theme's color table (spec/08 §1.9, §7.1): the library's names are
atoms, and a color a theme declares may be named by its string (D-195).

    iex> Visualize.Color.color(:red)
    {:color, :red}

# `contrast`

```elixir
@spec contrast() :: value()
```

The readable ink over whatever the text is drawn on (spec/14 §3.2): a mark's inline label
inks against its element's fill, any other text against the theme's background.

    iex> Visualize.Color.contrast()
    {:contrast, :under}

# `contrast`

```elixir
@spec contrast(value()) :: value()
```

The readable ink over `c`: the theme's text or background, else black or white,
whichever contrasts more (spec/08 §1.9, §7.3).

    iex> Visualize.Color.color(:bad) |> Visualize.Color.contrast()
    {:contrast, {:color, :bad}}

# `contrast`

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

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

# `darken`

```elixir
@spec darken(value(), number()) :: value()
```

The color `c` moved toward black by `t` in `0..1`, in OKLCH lightness (spec/08 §1.9).

    iex> Visualize.Color.color(:red) |> Visualize.Color.darken(0.3)
    {:darken, {:color, :red}, 0.3}

# `darker`

```elixir
@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`

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

Returns a desaturated copy (in HSL space)

# `grayscale`

```elixir
@spec grayscale(t()) :: t()
```

Returns a grayscale version of the color

# `hcl`

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

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

# `hsl`

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

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

# `intensity`

```elixir
@spec intensity(value(), number()) :: value()
```

How much of the color `c` is present, `t` in `0..1`; the rest is transparent. On its own
it is `c` at `t` of its alpha; in `blend/1` it is the color's weight (spec/08 §1.9).

    iex> Visualize.Color.color(:blue) |> Visualize.Color.intensity(0.65)
    {:intensity, {:color, :blue}, 0.65}

# `interpolate_hcl`

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

Interpolates between two colors in HCL space (perceptually uniform)

# `interpolate_hsl`

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

Interpolates between two colors in HSL space

# `interpolate_lab`

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

Interpolates between two colors in Lab space

# `interpolate_rgb`

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

Interpolates between two colors in RGB space

# `invert`

```elixir
@spec invert(t()) :: t()
```

Inverts the color

# `lab`

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

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

# `lighten`

```elixir
@spec lighten(value(), number()) :: value()
```

The color `c` moved toward white by `t` in `0..1`, in OKLCH lightness (spec/08 §1.9).

    iex> Visualize.Color.color(:red) |> Visualize.Color.lighten(0.3)
    {:lighten, {:color, :red}, 0.3}

# `luminance`

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

Returns the relative luminance (0-1)

# `mix`

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

Mixes two colors equally (in RGB space)

# `named`

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

Gets a named color's hex value

# `named_colors`

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

Returns all named color names

# `oklab`

```elixir
@spec oklab(number(), number(), number(), number()) :: t()
```

A color from OKLab, brought into sRGB by CSS Color 4's gamut mapping (spec/08 §1.9).
Channels are left unrounded.

# `oklch`

```elixir
@spec oklch(number(), number(), number(), number()) :: t()
```

A color from OKLCH (`h` in degrees), gamut-mapped as `oklab/4` is.

# `parse`

```elixir
@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.

# `ramp`

```elixir
@spec ramp(atom(), number()) :: value()
```

The color at `t` in `0..1` along the theme's ramp `name` (spec/08 §7.5).

    iex> Visualize.Color.ramp(:thermal, 0.7)
    {:ramp, :thermal, 0.7}

# `resolve`

```elixir
@spec resolve(value() | atom(), Visualize.Theme.t(), Visualize.Theme.mode()) ::
  String.t()
```

A color value resolved against a theme in `mode` (spec/08 §1.9, §7.3): a name, a series
or a ramp position exactly as `Visualize.Theme.resolve/3` resolves the slot, a literal as
written, and a transform as the literal it computes to, `#rrggbb` or `#rrggbbaa`.

    iex> theme = Visualize.Theme.default()
    iex> Visualize.Color.resolve(Visualize.Color.color(:axis), theme, :css)
    "var(--vis-axis, #666666)"
    iex> Visualize.Color.resolve({:intensity, "#ff0000", 0.5}, theme, :literal)
    "#ff000080"

# `rgb`

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

Creates a color from RGB values (0-255)

# `rotate`

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

Rotates the hue by the specified degrees

# `saturate`

```elixir
@spec saturate(t(), number()) :: t()
@spec saturate(value(), number()) :: value()
```

On a `%Visualize.Color{}`, a saturated copy: HSL saturation multiplied by `amount`
(default `1.2`).

On a color value (spec/08 §1.9), the transform `{:saturate, c, t}`: chroma moved toward
full in OKLCH by `t` in `0..1`, resolved against the theme at render.

    iex> Visualize.Color.color(:red) |> Visualize.Color.saturate(0.2)
    {:saturate, {:color, :red}, 0.2}

# `shade`

```elixir
@spec shade(value(), number()) :: value()
```

The step `n` of `c`'s scale, `n` in `0..1000` as designers number them (spec/08 §1.9):
500 is `c`, 0 near white and 1000 near black, on one OKLCH curve that keeps the hue and
eases the chroma toward the ends. A step the theme pins of a name is the pinned color.

    iex> Visualize.Color.color(:red) |> Visualize.Color.shade(100)
    {:shade, {:color, :red}, 100}

# `to_hcl`

```elixir
@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`

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

Converts to hex string

# `to_hsl`

```elixir
@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`

```elixir
@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_oklab`

```elixir
@spec to_oklab(t()) :: %{l: float(), a: float(), b: float(), opacity: number()}
```

The color in OKLab (spec/08 §1.9): `%{l, a, b, opacity}`, `l` in `0..1`.

    iex> %{l: l} = Visualize.Color.to_oklab(Visualize.Color.parse("#ffffff"))
    iex> Float.round(l, 4)
    1.0

# `to_oklch`

```elixir
@spec to_oklch(t()) :: %{l: float(), c: float(), h: float(), opacity: number()}
```

The color in OKLCH (spec/08 §1.9): `%{l, c, h, opacity}`, `h` in degrees `[0, 360)`.

# `to_rgb`

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

Converts to RGB tuple {r, g, b}

# `to_rgba`

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

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

# `to_string`

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

Converts to CSS string

# `value?`

```elixir
@spec value?(term()) :: boolean()
```

Whether a term is a color value (spec/08 §1.9), every operand of a transform included.

    iex> Visualize.Color.value?({:alpha, {:color, :red}, 0.5})
    true
    iex> Visualize.Color.value?(:red)
    false

# `with_opacity`

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

Returns a copy with the specified opacity

---

*Consult [api-reference.md](api-reference.md) for complete listing*
