# Theme and fonts


A theme is a set of named colors and two font families. The library's [UI components](/docs/ui/) take all their colors from it, the font helpers take their families from it, and scenes that use its names instead of hex values look like they belong together. There is one, `theme`, and a project can change or replace it.

## The theme

```odin
Theme :: struct {
	// night (dark) surfaces and text
	night, night2: Color,
	nt, nm, ns:    Color, // text, muted, subtle
	nb:            Color, // hairline
	na:            Color, // accent on night
	// paper (light) surfaces and text
	bg, sf, sf2, sf3: Color,
	bd, bds:          Color, // hairline, stronger hairline
	ink, mut, sub:    Color,
	red, red_soft:    Color,
	ok, ok_soft:      Color,
	warn, warn_soft:  Color,
	sans, mono:       cstring,
}

LIERMANN :: Theme{…}
theme := LIERMANN
```

The colors come in two sets, for the two kinds of surface a scene is drawn on: night, a near-black, and paper, a warm off-white. `LIERMANN` is the default: paper surfaces, hairline borders, mono labels and one red.

```odin
theme_palette :: proc(s: fx.Shot) {
	th := fx.theme
	swatches := [?]struct {name: string, c: fx.Color} {
		{"night", th.night}, {"night2", th.night2}, {"nt", th.nt}, {"nm", th.nm}, {"ns", th.ns}, {"nb", th.nb},
		{"na", th.na}, // the first seven are for night, the rest for paper
		{"bg", th.bg}, {"sf", th.sf}, {"sf2", th.sf2}, {"sf3", th.sf3}, {"bd", th.bd}, {"bds", th.bds},
		{"ink", th.ink}, {"mut", th.mut}, {"sub", th.sub}, {"red", th.red}, {"red_soft", th.red_soft},
		{"ok", th.ok}, {"ok_soft", th.ok_soft}, {"warn", th.warn}, {"warn_soft", th.warn_soft},
	}
	fx.rect({0, 0, 660, 1080}, th.night)
	fx.rect({660, 0, 1260, 1080}, th.bg)
	for sw, i in swatches {
		night := i < 7
		j := night ? i : i - 7
		r := fx.Rect{70 + f32(j % 2) * 280, 60 + f32(j / 2) * 250, 240, 150}
		if !night do r = {740 + f32(j % 4) * 285, 60 + f32(j / 4) * 250, 240, 150}
		k := fx.stagger(s.lt, i, 0.2, 0.04, 0.5)
		fx.rrect(fx.inset(r, 30 * (1 - k)), 20, fx.alpha(sw.c, k)) // each on the surface it is meant for
		fx.border(r, night ? th.nb : th.bd, 2, 20)
		fx.label(sw.name, {r.x, r.y + r.h + 14}, fx.mono(32), night ? th.nt : th.ink)
	}
}
```

On night:

| Field | `LIERMANN` | Use |
| --- | --- | --- |
| `night` | `0xff111110` | The dark surface. |
| `night2` | `0xff1a1a15` | A raised surface on it: cards, panels. |
| `nt` | `0xfff2f0e6` | Text. |
| `nm` | `0xb8f2f0e6` | Muted text: the text color at 72%. |
| `ns` | `0x7af2f0e6` | Subtle text and marks, 48%. |
| `nb` | `0x24f2f0e6` | Hairlines and borders, 14%. |
| `na` | `0xffdd5b3e` | The accent. |

On paper:

| Field | `LIERMANN` | Use |
| --- | --- | --- |
| `bg` | `0xfff3f2ec` | The page. |
| `sf` | `0xfffbfaf7` | A surface on it: windows, cards. |
| `sf2` | `0xffedebe3` | A recessed surface: a window's title bar, an avatar. |
| `sf3` | `0xffe4e2d8` | A darker one still: the track of a meter. |
| `bd` | `0x2116160f` | Hairlines and borders: ink at 13%. |
| `bds` | `0x3d16160f` | A stronger hairline, 24%. |
| `ink` | `0xff16160f` | Text. |
| `mut` | `0xbd16160f` | Muted text, 74%. |
| `sub` | `0x8f16160f` | Subtle text, 56%. |
| `red` | `0xffb4321f` | The accent on paper, and the `.Red` tone of pills and buttons. |
| `red_soft` | `0x14b4321f` | Its wash, 8%: the background of a red pill. |
| `ok` | `0xff2f6b47` | The `.Ok` tone. |
| `ok_soft` | `0x1a2f6b47` | Its wash, 10%. |
| `warn` | `0xff8a6410` | The `.Warn` tone. |
| `warn_soft` | `0x1a8a6410` | Its wash, 10%. |

The muted, subtle, hairline and soft colors are translucent versions of the text or accent color, so they sit correctly on any of their set's surfaces. That is also why fading one of them is done with [`alpha`](/docs/color/#colors), which multiplies the alpha they have, and not with `with_alpha`.

`sans` and `mono` are the family names the [font helpers](#fonts) use: `"SG"` and `"JBM"`.

### Changing it

`theme` is an ordinary variable, read every time something is drawn. Change it once, in `effects_video`, before any frame is drawn:

```odin
@(export)
effects_video :: proc() -> ^fx.Video {
	fx.theme.na = 0xff62e6d2  // one field …
	fx.theme.red = 0xff0f8f7d

	dusk := fx.LIERMANN       // … or a whole theme, starting from the default
	dusk.night, dusk.night2 = 0xff0d0b14, 0xff17142a
	dusk.nt, dusk.nm, dusk.ns, dusk.nb = 0xffeeeaff, 0xb8eeeaff, 0x7aeeeaff, 0x24eeeaff
	fx.theme = dusk

	return fx.video({name = "Spot", scenes = SCENES[:]})
}
```

Start from `LIERMANN` rather than an empty `fx.Theme{}`: a field left out of a new struct is 0, which as a color is fully transparent. Do not change the theme from inside a scene. Frames are drawn on several threads at once and in any order, and they all read the same variable.

Two things do not follow the theme. `backdrop` draws fixed night and paper gradients, so a theme with other surface colors needs its own background. And a video's `background` is black unless you set it (`fx.video({background = fx.theme.night, …})`).

A project is free to ignore the theme too: `projects/fable` defines its own handful of color constants and uses the theme only for its fonts.

## Fonts

```odin
sans :: proc(size: f32, weight: f32 = 400, tracking: f32 = 0) -> Font
mono :: proc(size: f32, weight: f32 = 400, tracking: f32 = 0) -> Font
caps :: proc(size: f32, tracking: f32 = 0.1, weight: f32 = 400) -> Font
```

Each returns a [`Font`](/docs/draw/#fonts) in one of the theme's two families: `size` in px, `weight` 100..900, `tracking` in em. `sans` is for headlines and running text, `mono` for labels, numbers and code. `caps` is the mono family in capitals with open letter spacing, the style of eyebrows and section labels; note that its second argument is the tracking, not the weight. The result is a plain struct, so set further fields on it when you need them:

```odin
f := fx.sans(72, 500, -0.02)
f.lh = 1.2
f.tabular = true
```

```odin
theme_fonts :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	th := fx.theme
	weight := knob("weight", 600, 300, 700)
	k := fx.ep(s.lt, 0.2, 1.0)
	// each line is set in the font its text names
	if fx.group({o = k, y = 30 * (1 - k)}) {
		fx.text(fmt.tprintf("fx.sans(150, %.0f, -0.03)", weight), 130, 110, fx.sans(150, weight, -0.03), th.nt)
		fx.text("fx.sans(64)", 130, 330, fx.sans(64), th.nm)
		fx.text("fx.mono(64)", 130, 500, fx.mono(64), th.nt)
		fx.text("fx.mono(64, 500)", 130, 600, fx.mono(64, 500), th.nt)
		fx.text("fx.caps(48)", 130, 790, fx.caps(48), th.na)
		fx.text("fx.caps(48, 0.3, 500)", 130, 880, fx.caps(48, 0.3, 500), th.na)
	}
}
```

Two families are bundled and registered by `video` for every project:

| Family | Typeface | Weights |
| --- | --- | --- |
| `"SG"` | Space Grotesk, a variable font | Any weight from 300 to 700. |
| `"JBM"` | JetBrains Mono, two static files | 400 and 500. Other weights use the nearer of the two. |

Both are the Latin subsets of their typefaces, enough for Western European languages. `assets/fonts` in the repository also has Space Grotesk's Latin Extended subset, which is not registered.

### Your own fonts

```odin
font :: proc(family: string, path: string) -> bool
```

`font` registers a font file under a family name of your choosing. The path is relative to the project directory. Call it once, in `effects_video`; it returns false and prints `fx: cannot load font` when the file cannot be read. `projects/fable` adds a serif this way, five static files under one name:

```odin
@(export)
effects_video :: proc() -> ^fx.Video {
	v := fx.video({name = "Fable 5.2", scenes = SCENES[:]})
	// Fraunces (SIL Open Font License), the storyteller's voice next to the machine's mono
	for f in ([?]string{"Light", "LightItalic", "Regular", "Italic", "SemiBoldItalic"}) {
		fx.font("FR", fmt.tprintf("assets/fonts/Fraunces144pt-%s.ttf", f))
	}
	return v
}

serif :: proc(size: f32, italic := true, weight: f32 = 300) -> fx.Font {
	return {family = "FR", size = size, weight = weight, italic = italic, tracking = -0.012, lh = 1.08}
}
```

How a family's files are chosen:

- Several files under one name make a family. `Font.weight` and `Font.italic` pick the closest file, so `serif(96)` above is Light Italic and `serif(96, false, 400)` is Regular.
- A variable font is one file, and `Font.weight` sets its weight axis directly.
- Asking for an italic the family does not have gets the upright face slanted by the text engine, and a weight far above the heaviest file gets it thickened. Neither looks like a drawn italic or bold: register the real files.
- A family name that was never registered does not fail. The text is drawn in a fallback font, so a typo in the name shows as the wrong typeface.

To make the theme's helpers, and with them every UI component, use your fonts, point the theme at them:

```odin
fx.font("Inter", "assets/fonts/Inter-Variable.woff2")
fx.theme.sans = "Inter" // fx.sans, captions, windows and tables now use it
```

`.ttf` and `.woff2` files both work.

## Icons

The library carries a set of 77 line icons, drawn on a 24-unit grid with round stroke ends. [`icon`](/docs/draw/#icons) draws one by name at any size and stroke width; [`icon_path`](/docs/draw/#icons) returns its path.

```odin
fx.icon("calendar", 160, 160, 96, fx.theme.nt)       // 96 px, top-left corner at {160, 160}
fx.icon("check", 300, 160, 96, fx.theme.na, 2.4)     // a heavier stroke, in 24ths of the size
```

```odin
theme_icons :: proc(s: fx.Shot) {
	fx.backdrop(.Night)
	for d, i in fx.ICON_DEFS { // every icon, down the columns in alphabetical order
		x, y := 50 + f32(i / 13) * 308, 22 + f32(i % 13) * 80
		k := fx.stagger(s.lt, i, 0.1, 0.012, 0.4)
		if fx.group({o = k, x = -20 * (1 - k)}) {
			fx.icon(d.name, x, y + 14, 52, fx.theme.nt, 1.6)
			fx.label(d.name, {x + 68, y + 40}, fx.mono(28), fx.theme.nm, {0, 0.5})
		}
	}
}
```

| | Names |
| --- | --- |
| A–C | `agent`, `arrow`, `backspace`, `bell`, `briefcase`, `broadcast`, `calendar`, `calls`, `chat`, `check`, `chevronDown`, `chevronLeft`, `chevronRight`, `circle`, `clip`, `close`, `contacts`, `copy` |
| D–H | `dashboard`, `doc`, `documents`, `edit`, `externalLink`, `eye`, `feed`, `flow`, `funnel`, `globe`, `grid`, `hang`, `hangup`, `history` |
| I–M | `info`, `insta`, `instagram`, `kanban`, `key`, `layers`, `linkedin`, `lock`, `mail`, `maximize`, `menu`, `mic`, `microphone`, `microphoneOff`, `moon` |
| P–S | `panelLeft`, `paperclip`, `pause`, `phone`, `pipeline`, `play`, `plus`, `refresh`, `screen`, `search`, `send`, `server`, `settings`, `shield`, `signOut`, `sliders`, `spark`, `sparkles`, `stop`, `sun` |
| T–W | `trash`, `upload`, `usage`, `users`, `video`, `videoOff`, `volume`, `volumeOff`, `whatsapp`, `workflows` |

Names are case-sensitive, and one that is not in the list draws nothing. All icons are stroked except `linkedin`, which is a filled shape and ignores the stroke width.

The set is the array `ICON_DEFS`, which is what the demo above loops over:

```odin
Icon_Def :: struct {
	name:   string,
	d:      cstring, // SVG path data on a 24×24 grid
	filled: bool,
}
```

For an icon that is not in the set, draw its SVG path yourself, the way `icon` does: scale a group by `size / 24` and stroke the path.

```odin
bolt := sk.path_from_svg("M13 2L4 14h7l-1 8 9-12h-7z")
defer sk.path_free(bolt)
if fx.group({x = 160, y = 160, s = 96.0 / 24}) {
	fx.draw_path(bolt, fx.stroke(fx.theme.nt, 1.6))
}
```

## Reference

| Name | What it is |
| --- | --- |
| `Theme` | Named colors for night and paper surfaces, and two font families. |
| `LIERMANN` | The default theme. |
| `theme` | The theme in use; a variable a project can change. |
| `sans(size, weight, tracking)` | A `Font` in the theme's sans family. |
| `mono(size, weight, tracking)` | A `Font` in the theme's mono family. |
| `caps(size, tracking, weight)` | The mono family in capitals, tracked out. |
| `font(family, path)` | Registers a font file under a family name. |
| `default_fonts()` | Registers the bundled `"SG"` and `"JBM"` families; `video` calls it. |
| `ICON_DEFS`, `Icon_Def` | The built-in icons: name, SVG path data, filled or stroked. |
| `icon(name, x, y, size, c, width)`, `icon_path(name)` | Drawing an icon, see [Drawing](/docs/draw/#icons). |
