# Colors and gradients

> Every color syntax vidmake accepts (hex, `rgb()`, `hsl()`, `oklch()`, `oklab()`, names), theme colors, linear and radial gradients with all their options, animated gradient angles, and gradients on text and strokes.

Anywhere vidmake asks for a color you can write it the way you would in CSS. Fields that take a
**paint** (`fill`, `stroke`, text `background`, a span's `fill`, captions `highlight`, and the
composition `background`) also accept a [gradient](#gradients).

## Color syntax

| syntax | example | notes |
|---|---|---|
| `#rgb`, `#rgba` | `"#fa0"`, `"#fa08"` | short hex, each digit doubled |
| `#rrggbb`, `#rrggbbaa` | `"#c92a26"`, `"#161412cc"` | the last pair is alpha (`cc` ≈ 80%) |
| `rgb()`, `rgba()` | `"rgb(201, 42, 38)"`, `"rgba(0, 0, 0, 0.5)"` | 0..255 or percentages; alpha 0..1 |
| `hsl()`, `hsla()` | `"hsl(2, 68%, 47%)"`, `"hsla(2, 68%, 47%, 0.5)"` | hue in degrees; saturation and lightness **with `%`** |
| `oklch()` | `"oklch(62% 0.19 260)"`, `"oklch(0.62 0.19 260 / 50%)"` | lightness (`%` or 0..1), chroma, hue in degrees, optional `/ alpha`. Perceptually even: steps in lightness look even, and hues at the same lightness look equally bright |
| `oklab()` | `"oklab(70% 0.1 -0.1)"` | lightness, then the a (green–red) and b (blue–yellow) axes, optional `/ alpha` |
| CSS names | `"white"`, `"teal"`, `"gold"`, `"cornflowerblue"` | the classic SVG/CSS named colors |
| `transparent`, `none` | `"transparent"` | fully transparent |
| array | `[201, 42, 38]`, `[1, 0.5, 0, 0.8]` | `[r, g, b]` or `[r, g, b, a]`, see below |

Arrays use 0..255 for the color channels when any of r, g or b is above 1, and 0..1 otherwise; alpha
is always 0..1 and defaults to 1. So `[255, 128, 0]` and `[1, 0.5, 0]` are the same orange, but
`[1, 1, 1]` is white, not almost-black.

`"fill": null` (or `"stroke": null`) turns that paint off. Shapes and text default to a white fill and
no stroke; `arc` and `line` default to a white stroke instead (an `arc` with `"closed": true` is filled).

> [!WARNING]
> In a constant color string, `hsl()` follows CSS and needs `%` signs: `"hsl(40, 50%, 70%)"`. Without
> them (`"hsl(40, 50, 70)"`) the values are read as fractions over 100% and you get white. The
> [expression](https://vidmake.app/docs/expressions.md#strings-and-colors) function `hsl()` is different: it takes plain
> numbers, `"=hsl(40, 50, 70)"`.

`oklch()` and `oklab()` are converted to sRGB; colors outside it are clipped. Some other modern CSS forms are not
supported: space-separated `rgb(255 0 0 / 50%)` and CSS Color 4 names like `rebeccapurple`. `validate` reports an
invalid color with the field it's in.

### Theme colors

A color used in more than one place belongs in the project's [theme](https://vidmake.app/docs/theme.md): `"fill": "$color.accent"`.
Theme colors can alias each other with an alpha, `"{color.ink}/60%"`, and change per [mode](https://vidmake.app/docs/theme.md#modes).

## Animated and computed colors

Colors animate like numbers, with [keyframes or a tween](https://vidmake.app/docs/animation.md#animating-colors), blending
red, green, blue and alpha linearly:

```json
"fill": [[0, "#c92a26"], ["1s", "#4a4640", "easeInOut"]]
"stroke": { "from": "#d8d1c1", "to": "#161412", "duration": "2s", "repeat": "pingpong" }
```

An [expression](https://vidmake.app/docs/expressions.md#strings-and-colors) can compute a color per frame or per copy:

```json
"fill": "=hsl(i * 30 + t * 60, 60, 70)"
"fill": "=pick(beats, ['#161412', '#1f5fb0', '#c92a26'])"
"fill": "=lerp('#e9e4d8', '#4a4640', p)"
"fill": "=rgb(255, 128 + 127 * sin(t), 0)"
```

## Gradients

A gradient is an object with a `type`. Its coordinates are relative to the **layer's own box**, so the
same gradient fits a small button and a full-screen background alike.

```json
{ "type": "linear", "angle": 90, "stops": ["#c92a26", "#1f5fb0"] }
{ "type": "linear", "angle": 160, "stops": [[0, "#e9e4d8"], [0.55, "#c92a26"], [1, "#161412"]] }
{ "type": "radial", "center": [0.3, 0.25], "radius": 0.9, "stops": ["#fbf9f4", "#1f5fb0", "#161412"] }
```

**Demo: Gradient paints: two stops, positioned stops, radial, and an animated angle** (rendered video: https://vidmake.app/media/colors-gradients.mp4)

```json
{
  "fonts": [{ "family": "JetBrains Mono", "path": "fonts/JetBrainsMono.ttf" }],
  "compositions": [{
    "id": "gradients", "description": "Gradient paints: two stops, positioned stops, radial, and an animated angle",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "two", "type": "rect", "x": 220, "y": 330, "width": 240, "height": 300, "cornerRadius": 4,
        "fill": { "type": "linear", "angle": 90, "stops": ["#c92a26", "#1f5fb0"] } },
      { "id": "stops", "type": "rect", "x": 500, "y": 330, "width": 240, "height": 300, "cornerRadius": 4,
        "fill": { "type": "linear", "angle": 160, "stops": [[0, "#e9e4d8"], [0.55, "#c92a26"], [1, "#161412"]] } },
      { "id": "radial", "type": "rect", "x": 780, "y": 330, "width": 240, "height": 300, "cornerRadius": 4,
        "fill": { "type": "radial", "center": [0.3, 0.25], "radius": 0.9, "stops": ["#fbf9f4", "#1f5fb0", "#161412"] } },
      { "id": "spin", "type": "rect", "x": 1060, "y": 330, "width": 240, "height": 300, "cornerRadius": 4,
        "fill": { "type": "linear", "angle": { "from": 0, "to": 360, "duration": "4s", "ease": "linear" },
                  "stops": ["#161412", "#e9e4d8", "#c92a26"] } },
      { "id": "label", "type": "text", "text": "${pick(i, ['linear 90', 'positioned stops', 'radial', 'animated angle'])}",
        "font": "JetBrains Mono", "fontSize": 15, "letterSpacing": 2, "uppercase": true, "fill": "#8a857a", "x": 640, "y": 540,
        "repeat": { "count": 4, "layout": "row", "spacing": 280 } }
    ]
  }]
}
```

### Linear

| field | default | notes |
|---|---|---|
| `type` | | `"linear"` |
| `angle` | 90 | direction in degrees, like CSS: 0 = towards the top, 90 = towards the right, 180 = down. **Animatable** |
| `stops` | required | at least two, see [stops](#stops) |

The gradient runs through the center of the box and is sized so the first and last stops land exactly
on the box's corners (or edges) for any angle, as in CSS. Beyond the ends the edge colors continue.

### Radial

| field | default | notes |
|---|---|---|
| `type` | | `"radial"` |
| `center` | `[0.5, 0.5]` | center as fractions of the box: `[0, 0]` is the top-left corner |
| `radius` | 0.5 | radius as a fraction of the box's **larger** side: 0.5 reaches the middle of the longer edges, about 0.7 reaches the corners of a square |
| `stops` | required | from the center outwards |

### Stops

`stops` is a list of at least two entries. Each entry is either:

- a color, and the stops are spread evenly: `["#000", "#888", "#fff"]` puts them at 0, 0.5 and 1;
- `[offset, color]`, with `offset` in 0..1: `[[0, "#000"], [0.7, "#123"], [1, "#fff"]]`;
- `{ "offset": 0.7, "color": "#123" }` (without `offset`, the stop gets its evenly spaced position).

Colors in stops can have alpha, which is how you fade something out: `["#c92a26", "#c92a2600"]`.

### Animating gradients

A linear gradient's `angle` takes any [animation form](https://vidmake.app/docs/animation.md): keyframes, a tween, a spring
or an expression. That's enough for a slowly turning sheen or a light sweep:

```json
"fill": { "type": "linear", "angle": { "from": 0, "to": 360, "duration": "4s", "ease": "linear" },
          "stops": ["#161412", "#e9e4d8", "#c92a26"] }
```

The stops and a radial gradient's `center` and `radius` are fixed. To move a radial glow, move or scale
the layer that carries it, or put the gradient on a larger layer and animate that layer's position
behind a [mask](https://vidmake.app/docs/masks.md).

> [!NOTE]
> The gradient belongs to the layer's box, so it turns with the layer: rotating a rect with a
> horizontal gradient rotates the gradient too. To keep the light direction fixed while the shape spins,
> counter-animate the `angle`.

## Gradients on text and strokes

A gradient works anywhere a paint does. On a text `fill` it spans the text box (including `padding`),
on a `stroke` it follows the outline, and in a [rich-text](https://vidmake.app/docs/text.md#rich-text) span it colors just that span:

**Demo: Gradients on text fills, on strokes, and on a single rich-text span** (rendered video: https://vidmake.app/media/colors-text.mp4)

```json
{
  "fonts": [{ "family": "Schibsted Grotesk", "path": "fonts/SchibstedGrotesk.ttf" }],
  "compositions": [{
    "id": "text", "description": "Gradients on text fills, on strokes, and on a single rich-text span",
    "width": 1280, "height": 720, "fps": 30, "duration": "4s", "background": "#f4f1ea",
    "layers": [
      { "id": "ring", "type": "arc", "x": 640, "y": 330, "width": 620, "strokeWidth": 10, "rotation": "=t * 90",
        "endAngle": { "keys": [[0, 0], ["2s", 360]], "ease": "easeInOutCubic", "repeat": "pingpong" },
        "stroke": { "type": "linear", "angle": 0, "stops": ["#1f5fb0", "#c92a26", "#f4f1ea"] } },
      { "id": "title", "type": "text", "text": "Spot color", "font": "Schibsted Grotesk", "fontWeight": 860, "fontSize": 100,
        "letterSpacing": -4, "y": 310,
        "fill": { "type": "linear", "angle": { "from": 60, "to": 300, "duration": "4s", "ease": "linear" },
                  "stops": ["#161412", "#1f5fb0", "#c92a26"] } },
      { "id": "line", "type": "text", "font": "Schibsted Grotesk", "fontWeight": 500, "fontSize": 32, "fill": "#4a4640", "y": 470,
        "text": ["fills, strokes and ", { "text": "single spans", "fontWeight": 800, "fill": { "type": "linear", "angle": 90, "stops": ["#1f5fb0", "#c92a26"] } }] }
    ]
  }]
}
```

```json
"fill": { "type": "linear", "angle": 60, "stops": ["#161412", "#1f5fb0", "#c92a26"] }
"stroke": { "type": "linear", "angle": 0, "stops": ["#1f5fb0", "#c92a26", "#f4f1ea"] }
"text": ["fills, strokes and ", { "text": "single spans", "fill": { "type": "linear", "angle": 90, "stops": ["#1f5fb0", "#c92a26"] } }]
```

## Backgrounds

The composition's `background` is a paint over the whole frame. Leave it out for a transparent
background: `.webm`, `.mov`, `.png` and `.gif` output keep the alpha channel (see
[Rendering](https://vidmake.app/docs/rendering.md)).

```json
"background": { "type": "radial", "center": [0.5, 0.4], "radius": 0.9, "stops": ["#fbf9f4", "#e9e4d8"] }
```

For a background that animates, use a full-size `rect` as the first layer instead: a `rect` without
`width` and `height` fills its parent, and its `fill` can be keyframed, tweened or computed.

## Where colors are used

| field | takes |
|---|---|
| `fill`, `stroke` | paint: color or gradient |
| text `background`, span `fill` | paint |
| captions `highlight` | paint |
| composition `background` | paint |
| `shadow.color`, `glow.color` | color (animatable) |
| particle `emitter.colors` | a list of plain colors |
