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

The protocol every scale module implements, and `normalize/3` (spec/06 §2.3).

`Macaw.Draw.Scale` dispatches each of these operations to the module the scale struct
names (`scale.__struct__`). A module that declares this behavior and omits a callback
fails to compile under `--warnings-as-errors`, which is what keeps the facade total: no
scale reaches `Macaw.Draw.Scale.apply/2` without an `apply/2` of its own.

Operations that do not apply to a scale kind are still defined and behave as identity
(`nice/1`, `padding/2`, `clamp/2`), answer `nil` (`invert/2`) or answer `0`
(`bandwidth/1`). The discretizing scales answer their thresholds from `ticks/2`.

Every callback answers `{:error, faults}` for an argument of the wrong type (spec/06
§1.2); a refusable setter answers `{:ok, scale}` or `{:error, faults}` for one of the
right type, and a setter that cannot refuse answers the struct.

# `error`

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

A refusal: faults with their paths.

# `scale`

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

Any scale struct.

# `apply`

```elixir
@callback apply(scale(), term()) :: term() | nil
```

Maps a domain value to a range value; total, `nil` for a value it cannot place.

# `bandwidth`

```elixir
@callback bandwidth(scale()) :: number() | error()
```

The band width; `0` for every scale but `Band`.

# `clamp`

```elixir
@callback clamp(scale(), boolean()) :: scale() | error()
```

Enables or disables clamping; identity where the scale never extrapolates.

# `domain`

```elixir
@callback domain(scale(), term()) :: {:ok, scale()} | error() | scale()
```

Sets the input domain; the argument's shape is the module's own.

# `invert`

```elixir
@callback invert(scale(), term()) :: term() | nil
```

Maps a range value back to the domain; `nil` where the scale is not invertible.

# `nice`

```elixir
@callback nice(scale()) :: scale() | error()
```

Extends the domain outward to round values; identity where that has no meaning.

# `padding`

```elixir
@callback padding(scale(), number()) :: {:ok, scale()} | error() | scale()
```

Sets band padding; identity for every scale but `Band`.

# `range`

```elixir
@callback range(scale(), term()) :: {:ok, scale()} | error() | scale()
```

Sets the output range; the argument's shape is the module's own.

# `ticks`

```elixir
@callback ticks(scale(), number()) :: [term()] | error()
```

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

# `normalize`

```elixir
@spec normalize(number(), number(), number()) :: float() | error()
```

The position of `value` between `a` and `b` as a fraction, `0.5` when `a == b`.

d3-scale's `normalize`: a collapsed domain maps every value to the middle of the range
and a collapsed range inverts to the middle of the domain, instead of dividing by zero
(visualize D-49). Every continuous scale's `apply/2` and `invert/2` compute it. A
non-number in any position is a type fault at its path.

    iex> Macaw.Draw.Scale.Behaviour.normalize(5, 0, 10)
    0.5
    iex> Macaw.Draw.Scale.Behaviour.normalize(3, 7, 7)
    0.5

---

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