# Theme

> The project's `theme` holds its design tokens (colors, spacing, type styles, motion and layer styles) for every composition, written `"$color.accent"`. Aliases, modes such as `--theme dark`, token files with `extends`, W3C design-token (DTCG) files, `vidmake theme` and migrating from 0.5 `tokens`.

Props belong to one composition. A brand's colors, type, spacing and motion belong to the whole project and to
every component in it. They go in the project's `theme`, and every composition reads them as `"$name"`,
[components](https://vidmake.app/docs/components.md) included, without forwarding anything.

```json title="project.json"
{
  "theme": {
    "color":  { "ink": "#161412", "paper": "#f4f1ea", "accent": "oklch(54% 0.19 28)", "muted": "{color.ink}/60%" },
    "space":  { "s": 16, "m": 32, "l": 64 },
    "type":   { "display": { "font": "Schibsted Grotesk", "size": 96, "weight": 860, "tracking": -0.045, "leading": 0.95 },
                "label":   { "font": "JetBrains Mono", "size": 16, "weight": 500, "tracking": 0.12, "uppercase": true } },
    "motion": { "house": { "spring": 0.15, "duration": "0.5s" } },
    "styles": { "card": { "fill": "$paper", "cornerRadius": 4 } },
    "modes":  { "dark": { "color": { "ink": "#efebe2", "paper": "#131210" } } }
  },
  "compositions": [ ... ]
}
```

Groups are free-form: name them as you like (`color`, `space`, `radius`, `brand`...). Four have a meaning:

| group | holds |
|---|---|
| `type` | type styles, applied with `style` (see [Type styles](#type-styles)) |
| `motion` | named [motions](https://vidmake.app/docs/easing.md#motion), usable as `"motion": "house"` |
| `styles` | layer styles, applied with `style` (see [Styles](#styles)) |
| `modes` | partial overrides applied with `--theme <mode>` (see [Modes](#modes)) |

**Demo: One theme, read by every composition: colors, type styles, a layer style and a house motion** (rendered video: https://vidmake.app/media/theme-basics.mp4)

```json
{
  "fonts": [
    { "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" },
    { "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }
  ],
  "theme": {
    "color": { "ground": "#f4f1ea", "card": "#fbf9f4", "text": "#161412", "mute": "#8a857a", "pencil": "#c92a26",
               "accent": "{color.pencil}", "line": "{color.text}/90%" },
    "type": { "display": { "font": "Schibsted Grotesk", "size": 92, "weight": 860, "tracking": -0.045 },
              "label": { "font": "JetBrains Mono", "size": 16, "weight": 500, "tracking": 0.16, "uppercase": true } },
    "motion": { "house": { "spring": 0.2, "duration": "0.6s" } },
    "styles": { "card": { "fill": "$color.card", "stroke": "$color.line", "strokeWidth": 2, "cornerRadius": 4 } },
    "modes": { "dark": { "color": { "ground": "#131210", "card": "#1d1b18", "text": "#efebe2", "pencil": "#f05a4e" } } }
  },
  "compositions": [
    {
      "id": "theme", "description": "One theme, read by every composition: colors, type styles, a layer style and a house motion",
      "width": 1280, "height": 720, "fps": 30, "duration": "3s", "background": "$color.ground",
      "layers": [
        { "id": "a", "type": "use", "ref": "card", "x": 350, "y": 360, "props": { "kicker": "Theme", "title": "Tokens" },
          "enter": { "from": { "opacity": 0, "y": 40 }, "motion": "house" } },
        { "id": "b", "type": "use", "ref": "card", "x": 930, "y": 360, "start": "0.2s", "props": { "kicker": "Props", "title": "Content" },
          "enter": { "from": { "opacity": 0, "y": 40 }, "motion": "house" } }
      ]
    },
    {
      "id": "card", "width": 500, "height": 340, "duration": "3s", "props": { "kicker": "Kicker", "title": "Card" },
      "layers": [
        { "id": "bg", "type": "rect", "width": 496, "height": 336, "style": "$styles.card" },
        { "id": "kicker", "type": "text", "text": "{{kicker}}", "style": "$type.label", "fill": "$color.mute", "y": 100 },
        { "id": "title", "type": "text", "text": "{{title}}", "style": "$type.display", "fill": "$color.text", "y": 175 },
        { "id": "rule", "type": "rect", "width": 120, "height": 4, "y": 250, "fill": "$accent" }
      ]
    }
  ]
}
```

The two cards are the same `card` composition placed with `use` layers. It reads `$color.card`, `$type.label`,
`$type.display` and `$styles.card` straight from the theme; only the words come in as [props](https://vidmake.app/docs/props.md).

## References

A value that is exactly `"$color.accent"` becomes the token's value. A number stays a number and a style
stays an object:

```json
{ "id": "rule", "type": "rect", "width": "$space.l", "height": 4, "fill": "$color.accent" }
```

- **Short names.** A leaf name alone works when only one token has it: `"$accent"` for `$color.accent`. When two
  groups share a leaf, `validate` lists the matches and asks for the full path.
- **Expressions.** In an `=` expression the value is spliced in: `"=lerp($ink, $accent, p)"`,
  `"x": "=$space.l * i"`.
- **Text.** Only inside `${...}`: `"Price $5"` stays literal, `"${$space.l}px"` prints `64px`.
- **Errors.** An unknown token is an error with a suggestion:
  ``unknown theme token `$color.acent` — did you mean `$color.accent`?``

## Aliases

A token may point at another one, with the W3C syntax `"{color.ink}"` or as `"$color.ink"`. Add `/60%` to get the
same color at 60% alpha:

```json
"color": { "text": "#161412", "pencil": "#c92a26", "accent": "{color.pencil}", "line": "{color.text}/90%" }
```

Aliases resolve after [modes](#modes) apply, so a mode that changes `pencil` changes `accent` too, and one that
changes `text` changes `line`. A missing
target or a cycle is an error that names the chain (`theme alias cycle: a → b → a`).

## Styles

`style` on a layer merges theme fields *under* the layer's own fields: the layer always wins. A list applies in
order, later entries over earlier ones:

```json
{ "type": "text", "text": "Ship it", "style": "$type.display", "fill": "$accent" }
{ "type": "rect", "width": 480, "height": 300, "style": ["$styles.card", { "cornerRadius": 0 }] }
```

A style in `styles` is a set of layer fields: `{ "fill": "$paper", "cornerRadius": 4, "shadow": { ... } }`.

### Type styles

Type styles in `type` use design words, which become text fields:

| key | layer field |
|---|---|
| `font` | `font` |
| `size` | `fontSize` |
| `weight` | `fontWeight` |
| `leading` | `lineHeight` |
| `tracking` | `letterSpacing`, in **em** of the size: `-0.02` on 96 px is −1.92 px |
| `uppercase`, `italic` | the same |

Any other key is passed through as a layer field, so a type style can also set `fill` or `align`.

## Modes

`modes` holds partial overrides. `--theme dark` lays `modes.dark` over the theme before aliases resolve, on every
command that compiles: `render`, `still`, `storyboard`, `validate`, `inspect`, `timeline`, `trace` and `theme`.

```bash
vidmake render project.json -o out/dark.mp4 --theme dark
```

The demo above has a `dark` mode that changes four colors. The card's stroke is an alias of `text` and the rule
an alias of `pencil`, so both follow:

`vidmake still theme-basics.json -t 2s --theme dark` produces:

![vidmake still theme-basics.json -t 2s --theme dark](https://vidmake.app/media/still-29fc6f261390.jpg)

`--theme-file brand.json` lays a whole file over the theme instead, for a second brand or a client's colors. An
unknown mode is an error that lists the modes the theme has.

## Files

A theme can live in its own file, so one file themes every video you make for a brand:

```json
"theme": "brand/theme.json"
"theme": { "extends": "brand/tokens.json", "color": { "accent": "#c92a26" } }
```

`extends` (a path or a list of paths) loads files *underneath* the local tokens, so the project overrides the file.
Paths are relative to the project file.

### W3C design tokens

Files in the [W3C design-token format](https://www.designtokens.org/) (DTCG) work as a design tool exports them:
`$value` and `$type` are read (a group's `$type` applies to the tokens inside it), other `$` keys such as
`$description` are ignored. Typed values become vidmake values:

| `$type` | becomes |
|---|---|
| `color` | a color: a string, or `{ "colorSpace": "srgb" \| "hsl" \| "oklch" \| "oklab", "components": [...], "alpha" }` |
| `dimension` | px (`rem` is 16 px) |
| `duration` | a time |
| `cubicBezier` | an easing |
| `fontFamily`, `fontWeight`, `number` | the value |
| `typography` | a type style |
| `shadow` | a `shadow` object |
| `gradient` | gradient stops |
| `transition` | a motion |

A `transition` token outside `theme.motion` is used with a reference: `"motion": "$transition.quick"`.

## vidmake theme

`vidmake theme` lists every token with its resolved value and marks the ones nothing uses. Add `--theme <mode>`
to see a mode, or `--json` for a script:

```console
$ vidmake theme theme-basics.json
$color.ground  #f4f1ea
$color.card    #fbf9f4
$color.text    #161412
$color.mute    #8a857a
$color.pencil  #c92a26
$color.accent  #c92a26
$color.line    #161412e6
$type.display  {"font":"Schibsted Grotesk","size":92,"weight":860,"tracking":-0.045}
$type.label    {"font":"JetBrains Mono","size":16,"weight":500,"tracking":0.16,"uppercase":true}
$motion.house  {"spring":0.2,"duration":"0.6s"}
$styles.card   {"fill":"#fbf9f4","stroke":"#161412e6","strokeWidth":2,"cornerRadius":4}
modes: dark (apply with --theme <mode>)
```

## From 0.5

vidmake 0.5 had a top-level `tokens` object read as `{{color.accent}}`, and a top-level `easings` object. Both still
work with a warning. `{{name}}` is now for [props](https://vidmake.app/docs/props.md) only.

| 0.5 | now |
|---|---|
| `"tokens": { ... }` | `"theme": { ... }` |
| `"fill": "{{color.accent}}"` | `"fill": "$color.accent"` |
| `"easings": { "house": ... }` | `"theme": { "motion": { "house": ... } }` |

`vidmake migrate` rewrites a project in place (pretty-printed). `--check` only reports what it would change and
exits 1 if anything would:

```bash
vidmake migrate project.json --check
vidmake migrate project.json
```

```output
  moved `tokens` to `theme`
  composition `tokens`: 1 `{{token}}` reference(s) now `$token`
  composition `card`: 5 `{{token}}` reference(s) now `$token`
migrated project.json
```

> [!AGENT]
> Put every color, size and motion that appears more than once in the theme, then write `"$name"`. Check with
> `vidmake theme project.json`: an `(unused)` token is safe to delete.

## Next steps

- [Props](https://vidmake.app/docs/props.md) Per-composition inputs, overridden from the command line.
- [Easing and motion](https://vidmake.app/docs/easing.md#motion) Built-in motions and how to write your own.
- [Colors](https://vidmake.app/docs/colors.md) Every color syntax, including `oklch()`.
- [Components](https://vidmake.app/docs/components.md) Reusable compositions that read the same theme.
