# `Visualize.Chart.Theme`
[🔗](https://github.com/dcoai/Visualize/blob/v0.8.3/lib/visualize/chart/theme.ex#L1)

A theme as a document: stored, shipped and read back as a value (spec/14 §9.3, D-139).

A theme document is the inline `:theme` node of a design (§2.5) — every field of
`Visualize.Theme`, its `ramps`, `colors` and style palette `styles` whole — stamped with
the schema version. It
has no schema of its own: the theme node is a node of the design schema, so it is
versioned by `Visualize.Chart.Migration.current/0` and read under the one policy every
document of this library keeps (§9.1, D-137).

  * the map form is `%{version: v, theme: node}` (`to_map/1`); the builder's store keeps
    the fragment `%{theme: node}` (`fragment/1`), a library entry of kind `:theme`, which
    reads as the same document at the current version
  * the JSON form is the stamped fragment document `{"$version": v, "theme": {…}}`
    (`to_json/1`), as `Visualize.Chart.Fragment.to_json/1` writes it

Reading one back runs, in order: the version gate, the `depth` and `items` limits
(`limits:`, §9.2), the migration of §9, per-kind validity — which reports a ramp, role or
palette entry that breaks its contract (spec/08 §7.5, §7.7) — and then builds the theme.
Every fault
is `{path, reason}` in the validator's shape, so `Visualize.Chart.Validator.format/1`
words all of them.

## Examples

    iex> theme = Visualize.Theme.new(name: :house, axis: "#555555")
    iex> Visualize.Chart.Theme.from_map(Visualize.Chart.Theme.to_map(theme))
    {:ok, theme}

    iex> Visualize.Chart.Theme.from_map(%{theme: :dark})
    {:ok, Visualize.Theme.dark()}

    iex> Visualize.Chart.Theme.from_map(%{version: 4, theme: %{}})
    {:error, [{[:version], {:newer_schema, 4, 3}}]}

# `fragment`

```elixir
@spec fragment(Visualize.Theme.t()) :: %{theme: map()}
```

`%{theme: node}`: what the builder's store keeps, a fragment of kind `:theme` (§19.8).

# `from_json`

```elixir
@spec from_json(String.t(), keyword()) ::
  {:ok, Visualize.Theme.t()}
  | {:error, [Visualize.Chart.Validator.error(), ...]}
```

The theme a JSON document holds, read as `from_map/2` reads a map; the version gate and
the limits run on the parsed document before a key of it is decoded, so no atom is made
from a document this library cannot read. A JSON fault and a missing Jason are errors in
the same `{path, reason}` shape.

    iex> {:ok, json} = Visualize.Chart.Theme.to_json(Visualize.Theme.dark())
    iex> Visualize.Chart.Theme.from_json(json)
    {:ok, Visualize.Theme.dark()}

# `from_map`

```elixir
@spec from_map(term(), keyword()) ::
  {:ok, Visualize.Theme.t()}
  | {:error, [Visualize.Chart.Validator.error(), ...]}
```

The theme a map document or a theme fragment holds, or every fault found (spec/14 §9.3).

The version gate first — a newer document is refused before anything of it is read —
then the `depth` and `items` limits over the document as given, the migration, per-kind
validity with the contracts, and the theme: a node that is a name is the built-in theme
of that name, any other name is `{[:theme], {:unknown_theme, name}}`, and a variable
anywhere is `{path, {:unresolved, :var, name}}`, since nothing applies a theme document.

## Options

  * `:limits` - the sanity limits of §9.2, as `Visualize.Chart.Limits.new/1` takes them

# `node`

```elixir
@spec node(Visualize.Theme.t()) :: map()
```

The theme as the `:theme` node of spec/14 §2.5: every field, `ramps`, `colors` and `styles`
whole.

# `to_json`

```elixir
@spec to_json(Visualize.Theme.t()) ::
  {:ok, String.t()} | {:error, {:missing_dependency, :jason}}
```

The theme's JSON document: `{:ok, json}`, stamped `"$version"` as every fragment document
is (spec/14 §9.3, §19.7), or `{:error, {:missing_dependency, :jason}}` without Jason.

# `to_map`

```elixir
@spec to_map(Visualize.Theme.t()) :: %{version: pos_integer(), theme: map()}
```

The theme's map document, `%{version: current, theme: node}` (spec/14 §9.3).

---

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