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

The scale facade: constructors, the validating builder `new/2`, and dispatch of the
common operations to the module the scale struct names (spec/06 §2, §3).

Scales map values from a data domain to a visual range. The kinds (spec/06 §2.1):

- **continuous:** `:linear`, `:log`, `:power`, `:sqrt`, `:symlog`, `:time`, `:radial`;
- **discrete:** `:ordinal`, `:band`;
- **discretizing:** `:quantile`, `:quantize`, `:threshold`;
- **color:** `:sequential`, `:diverging`.

Every operation is total over every scale this module constructs (spec/06 §1.2): a
refusable one answers `{:ok, scale}` or `{:error, faults}` and has a `!` twin raising
`Macaw.Draw.Error`; `apply/2` answers `nil` for a value it cannot place; an argument of
the wrong type is `{:error, faults}`. A kind, and an option's key, is an atom or its
string, read by spelling and never made into an atom (spec/06 §1.3).

    {:ok, scale} = Macaw.Draw.Scale.new("linear", domain: [0, 100], range: [0, 500])
    Macaw.Draw.Scale.apply(scale, 50)
    #=> 250.0

# `error`

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

A refusal: faults with their paths.

# `kind`

```elixir
@type kind() :: atom() | String.t()
```

A kind, `kinds/0`'s, or its string.

# `t`

```elixir
@type t() :: struct()
```

A scale struct of any kind.

# `apply`

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

The range value of a domain value; total, `nil` for a value the scale cannot place.

# `apply_column`

```elixir
@spec apply_column(t(), term()) :: binary() | error()
```

A packed column mapped through the scale in one pass, to a **pixel binary** (spec/06
§3.3): float64 little-endian, one slot for each of the column's, each slot exactly
`apply/2` of that slot's value as a float, and NaN where `apply/2` answers no number or
the value is missing. No list is built and no term made a row on a linear or time scale.

The column is a buffer of macaw_data's encoding table (its spec/01 §1.1.6): `{form,
data}` with every slot present, `{form, data, validity}` with its bitmap (least
significant bit first, a set bit present), or a piece `{{form, data, validity}, offset,
count}` as `Macaw.Data.Table.packed/2` and `window/4` answer, its run the slots `offset`
to `offset + count - 1`. `form` is `:int64` or `:float64`; a time scale reads int64
slots as integer microseconds, and places no float. Anything else is `{:error, [{[1],
{:type, :column}}]}`.

    iex> scale = Macaw.Draw.Scale.new!(:linear, domain: [0, 10], range: [0, 100])
    iex> pixels = Macaw.Draw.Scale.apply_column(scale, {:int64, <<5::signed-little-64, 10::signed-little-64>>})
    iex> for <<x::float-little-64 <- pixels>>, do: x
    [50.0, 100.0]

# `band`

```elixir
@spec band() :: Macaw.Draw.Scale.Band.t()
```

A band scale with no categories over `[0, 1]` (spec/06 §10).

# `bandwidth`

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

A band's width; `0` on every other scale.

# `clamp`

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

Sets clamping; identity where it does not apply.

# `diverging`

```elixir
@spec diverging(term()) :: {:ok, Macaw.Draw.Scale.Color.t()} | error()
```

A diverging color scale: `Macaw.Draw.Scale.Color.diverging/1` (spec/06 §14).

# `diverging!`

```elixir
@spec diverging!(term()) :: Macaw.Draw.Scale.Color.t()
```

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

# `domain`

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

Sets the domain: the module's `domain/2`, refusable as the module's is.

# `domain!`

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

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

# `invert`

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

The domain value of a range value: continuous scales only, `nil` on the rest.

# `kind`

```elixir
@spec kind(t()) :: atom() | error()
```

The kind of a scale struct, an atom of `kinds/0`: `:sqrt` for a power scale of exponent
0.5.

# `kinds`

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

The kinds, in the order of spec/06 §2.1.

# `linear`

```elixir
@spec linear() :: Macaw.Draw.Scale.Linear.t()
```

A linear scale: domain `[0, 1]`, range `[0, 1]` (spec/06 §4).

# `log`

```elixir
@spec log() :: Macaw.Draw.Scale.Log.t()
```

A log scale of base 10 (spec/06 §5).

# `log`

```elixir
@spec log(term()) :: {:ok, Macaw.Draw.Scale.Log.t()} | error()
```

A log scale of `base`: `{:ok, scale}` or `{:error, [{[], {:log_base, base}}]}`.

# `log!`

```elixir
@spec log!(term()) :: Macaw.Draw.Scale.Log.t()
```

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

# `new`

```elixir
@spec new(kind(), keyword() | map()) :: {:ok, t()} | error()
```

A scale of `kind` built from options, every option checked at once (spec/06 §3.2):
`{:ok, scale}`, or `{:error, faults}` with every fault at its key, ordered by path.

`kind` is an atom of `kinds/0` or its string; `opts` a keyword list or a map whose keys
are atoms or strings. An option the kind does not take is `{[key], :unknown_key}`; an
unknown kind `{[], {:unknown, :kind, spelling}}`; options that are no keyword list or
map `{[], {:type, :options}}`.

# `new!`

```elixir
@spec new!(kind(), keyword() | map()) :: t()
```

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

# `nice`

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

The domain extended to round values; identity where that does not apply.

# `ordinal`

```elixir
@spec ordinal() :: Macaw.Draw.Scale.Ordinal.t()
```

An ordinal scale with no domain and no range (spec/06 §9).

# `padding`

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

Sets band padding: the module's `padding/2`, identity on every scale but a band.

# `padding!`

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

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

# `power`

```elixir
@spec power() :: Macaw.Draw.Scale.Power.t()
```

A power scale of exponent 1 (spec/06 §6).

# `quantile`

```elixir
@spec quantile() :: Macaw.Draw.Scale.Quantile.t()
```

A quantile scale with no samples (spec/06 §11).

# `quantize`

```elixir
@spec quantize() :: Macaw.Draw.Scale.Quantize.t()
```

A quantize scale over `[0, 1]` with no range (spec/06 §12).

# `radial`

```elixir
@spec radial() :: Macaw.Draw.Scale.Radial.t()
```

A radial scale: domain `[0, 1]`, range `[0, 2π]` (spec/06 §15).

# `range`

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

Sets the range: the module's `range/2`, refusable as the module's is.

# `range!`

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

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

# `sequential`

```elixir
@spec sequential(term()) :: {:ok, Macaw.Draw.Scale.Color.t()} | error()
```

A sequential color scale: `Macaw.Draw.Scale.Color.sequential/1` (spec/06 §14).

# `sequential!`

```elixir
@spec sequential!(term()) :: Macaw.Draw.Scale.Color.t()
```

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

# `sqrt`

```elixir
@spec sqrt() :: Macaw.Draw.Scale.Power.t()
```

A square-root scale, a power scale of exponent 0.5 (spec/06 §6).

# `symlog`

```elixir
@spec symlog() :: Macaw.Draw.Scale.Symlog.t()
```

A symmetric-log scale (spec/06 §7).

# `threshold`

```elixir
@spec threshold() :: Macaw.Draw.Scale.Threshold.t()
```

A threshold scale with no thresholds (spec/06 §13).

# `ticks`

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

`ticks(scale, 10)`.

# `ticks`

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

Representative domain values; `count` is a hint (spec/06 §1.4).

# `time`

```elixir
@spec time() :: Macaw.Draw.Scale.Time.t()
```

A time scale with no domain (spec/06 §8).

# `zone`

```elixir
@spec zone(t(), term()) :: {:ok, Macaw.Draw.Scale.Time.t()} | error()
```

Sets a time scale's display zone: `Macaw.Draw.Scale.Time.zone/2` (spec/06 §8.4). Not part
of the protocol: any other scale is `{:error, [{[], {:not_time_scale, kind}}]}`.

# `zone!`

```elixir
@spec zone!(t(), term()) :: Macaw.Draw.Scale.Time.t()
```

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

---

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