# `Macaw.Draw.Scale.Color`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/scale/color.ex#L1)

Sequential and diverging color scales, and the d3 schemes (spec/06 §14).

A scale maps a number to a literal `%Macaw.Layers.Color{}`, never a string (macaw_layers
spec/04 §1.1): its interpolator is a scheme's stops, a list of literal colors, or a
1-arity function of `t ∈ [0, 1]` answering one. A sequential scale's domain is
`[d0, d1]`; a diverging one's `[d0, d1, d2]`, `d1` the pivot at `t = 0.5`. Clamping is
on by default.

    {:ok, scale} = Macaw.Draw.Scale.Color.sequential("blues")
    scale |> Macaw.Draw.Scale.Color.apply(1) |> Macaw.Layers.Color.to_hex()
    #=> "#084594"

Stops interpolate per channel to an 8-bit color, opacity included, the answer equal to
visualize's for the same stops and `t`. A categorical scheme is a stop list like any
other: to pick its colors discretely, give `scheme/1` as an `Ordinal` scale's range.

# `error`

```elixir
@type error() :: {:error, [Macaw.Base.Fault.t()]}
```

A refusal: faults with their paths.

# `interpolator`

```elixir
@type interpolator() ::
  [Macaw.Layers.Color.t()] | (number() -&gt; Macaw.Layers.Color.t())
```

An interpolator as a scale holds it: literal stops, or a function of `t`.

# `t`

```elixir
@type t() :: %Macaw.Draw.Scale.Color{
  clamp?: boolean(),
  domain: [number()],
  interpolator: interpolator(),
  type: :sequential | :diverging
}
```

# `apply`

```elixir
@spec apply(t(), term()) :: Macaw.Layers.Color.t() | nil | error()
```

The literal color of a number (spec/06 §14.3): sequential `t = normalize(v, d0, d1)`, the
interpolator's middle in a collapsed domain; diverging by the half the value lies in,
a value beyond a collapsed half at that half's end; `t` clamped when the scale clamps.
`nil` for a value that is no number, and for a function interpolator's answer that is
no literal color.

# `bandwidth`

```elixir
@spec bandwidth(t()) :: 0 | error()
```

`0`: a color scale has no bandwidth.

# `clamp`

```elixir
@spec clamp(t(), boolean()) :: t() | error()
```

Sets clamping of `t` to `[0, 1]` (on by default); answers the struct.

# `diverging`

```elixir
@spec diverging(term()) :: {:ok, t()} | error()
```

A diverging scale over `[0, 0.5, 1]` from an interpolator, as `sequential/1` takes.

# `diverging!`

```elixir
@spec diverging!(term()) :: t()
```

`diverging/1`, answering the scale or raising `Macaw.Draw.Error`.

# `domain`

```elixir
@spec domain(t(), term()) :: {:ok, t()} | error()
```

Sets the domain, two numbers for a sequential scale and three for a diverging one:
`{:ok, scale}`, or `{:error, faults}`, another length `{[], {:type, :domain, type}}`.

# `domain!`

```elixir
@spec domain!(t(), term()) :: t()
```

`domain/2`, answering the scale or raising `Macaw.Draw.Error`.

# `invert`

```elixir
@spec invert(t(), term()) :: nil | error()
```

`nil`: a color scale is not invertible.

# `nice`

```elixir
@spec nice(t()) :: t() | error()
```

Identity.

# `padding`

```elixir
@spec padding(t(), number()) :: t() | error()
```

Identity: a color scale has no padding.

# `range`

```elixir
@spec range(t(), term()) :: {:ok, t()} | error()
```

Replaces the interpolator, as `sequential/1` takes one: `{:ok, scale}` or `{:error, faults}`.

# `range!`

```elixir
@spec range!(t(), term()) :: t()
```

`range/2`, answering the scale or raising `Macaw.Draw.Error`.

# `scheme`

```elixir
@spec scheme(term()) :: [Macaw.Layers.Color.t()] | nil
```

A scheme's stops as literal colors, by its name, an atom or its string; `nil` for a
name no scheme has.

# `schemes`

```elixir
@spec schemes() :: [atom()]
```

The 25 scheme names, in the order of spec/06 §14.4.

# `sequential`

```elixir
@spec sequential(term()) :: {:ok, t()} | error()
```

A sequential scale over `[0, 1]` from an interpolator: a scheme's name (atom or
string), a list of literal colors, or a 1-arity function. `{:ok, scale}` or
`{:error, faults}` (spec/06 §14.2).

# `sequential!`

```elixir
@spec sequential!(term()) :: t()
```

`sequential/1`, answering the scale or raising `Macaw.Draw.Error`.

# `ticks`

```elixir
@spec ticks(t(), number()) :: [number()] | error()
```

`Macaw.Draw.Scale.Linear.ticks/2` over the domain's extent, `[d0, d1]` or `[d0, d2]`.

---

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