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

Lengths with their units (spec/08 §13, spec/14 §3.9, #651, D-174).

A unit value is `{n, unit}`: data, stored and compared as a value. The **absolute**
screen units convert to user units — CSS pixels — at CSS's fixed ratios, 96 px = 1 in =
2.54 cm = 25.4 mm = 72 pt and 1 pc = 12 pt; the **relative** ones, `:em` and `:pct`, are
measured against a font size and a box the caller gives (`resolve/2`). A bare number is
user units, as it always was.

    iex> import Visualize.Units
    iex> pt(12)
    {12, :pt}
    iex> Visualize.Units.to_px(pt(12))
    16.0
    iex> Visualize.Units.resolve(em(1.5), %{em: 12})
    18.0
    iex> Visualize.Units.resolve(pct(50), %{box: 30})
    15.0

The world lengths a scene measures its ground in (#620) — `:mm` … `:mi` — are a table of
their own, in meters (`to_meters/2`, `convert_length/3`): a world length reaches the
screen only through a scale a scene declares.

# `length_unit`

```elixir
@type length_unit() :: :mm | :cm | :m | :km | :in | :ft | :yd | :mi
```

A world length unit (spec/08 §13.3).

# `t`

```elixir
@type t() :: {number(), unit()}
```

A length with its unit.

# `unit`

```elixir
@type unit() :: :px | :pt | :pc | :in | :cm | :mm | :em | :pct
```

A screen unit (spec/08 §13.1, §13.2).

# `absolute_units`

```elixir
@spec absolute_units() :: [unit(), ...]
```

The absolute screen units (spec/08 §13.1).

# `cm`

```elixir
@spec cm(number()) :: t()
```

`{n, :cm}`.

# `convert`

```elixir
@spec convert(number(), unit(), unit()) :: float()
```

`n` of one absolute unit in another.

    iex> Visualize.Units.convert(1, :in, :pt)
    72.0

# `convert_length`

```elixir
@spec convert_length(number(), length_unit(), length_unit()) :: float()
```

A world length in another world unit.

    iex> Visualize.Units.convert_length(2500, :m, :km)
    2.5

# `em`

```elixir
@spec em(number()) :: t()
```

`{n, :em}`: `n` times a font size, the one the context gives (spec/14 §3.9).

# `inch`

```elixir
@spec inch(number()) :: t()
```

`{n, :in}`: an inch, 96 user units. Named `inch` because `in` is an Elixir operator.

# `length_units`

```elixir
@spec length_units() :: [length_unit(), ...]
```

The world length units (spec/08 §13.3).

# `mm`

```elixir
@spec mm(number()) :: t()
```

`{n, :mm}`.

# `pc`

```elixir
@spec pc(number()) :: t()
```

`{n, :pc}`: a pica, 12 points.

# `pct`

```elixir
@spec pct(number()) :: t()
```

`{n, :pct}`: `n` percent of a box's length, the one the context gives (spec/14 §3.9).

# `pt`

```elixir
@spec pt(number()) :: t()
```

`{n, :pt}`: a point, 1/72 of an inch.

# `px`

```elixir
@spec px(number()) :: t()
```

`{n, :px}`.

# `resolve`

```elixir
@spec resolve(number() | t(), %{optional(:em) =&gt; number(), optional(:box) =&gt; number()}) ::
  number()
```

A size in user units: a number as it is, an absolute unit value by `to_px/1`, an `em`
against the context's `:em` (a font size) and a `pct` against its `:box` (a length).
Raises `ArgumentError` naming the unit when the context lacks its measure.

# `to_meters`

```elixir
@spec to_meters(number(), length_unit()) :: number()
```

A world length in meters.

    iex> Visualize.Units.to_meters(1, :ft)
    0.3048

# `to_px`

```elixir
@spec to_px(number() | t()) :: number()
```

A number as it is, an absolute unit value in user units; `ArgumentError` for a relative
one, which has nothing to be measured against here.

# `unit?`

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

Whether a term is a unit value.

    iex> Visualize.Units.unit?({3, :mm})
    true
    iex> Visualize.Units.unit?({3, :furlong})
    false

# `units`

```elixir
@spec units() :: [unit(), ...]
```

The screen units, absolute then relative.

---

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