# Make a theme

> Write a theme's dark and light looks, its colour tokens and the forms a colour takes, and the rules every look must meet.

Page: https://songstress.app/docs/develop/themes/

A theme sets the colours the apps draw. Songstress's own look is the only one an app carries: every other theme is
an extension's, which an admin installs, and each member picks from the themes installed there. A theme is data: it
needs no entry and no permission. Start one with `npm create songstress-extension@latest -- --theme`.

## Declaring a theme [#declaring-a-theme]

Up to 20 themes an extension, each with its looks written in the manifest, or in a theme file whose `path` it names,
never both:

```json title="songstress.json"
"contributes": {
  "themes": [
    { "id": "mocha", "label": "Catppuccin Mocha", "appearance": "dark", "dark": { "colors": { "background": "#1e1e2e", "text": "#cdd6f4", "accent": "#cba6f7" } } },
    { "id": "nord", "label": "Nord", "appearance": "both", "path": "themes/nord.json" }
  ]
}
```

One theme the picker lists: a dark look, a light one, or both, written here or in a theme file.

| Field | Type | Description |
|---|---|---|
| `id` | `string` | The theme's name inside the extension. |
| `label` | `string` | The theme's name, as the picker shows it. |
| `appearance` | `"both" \| "dark" \| "light"` | The looks the theme has: a dark one, a light one, or both. They are written here or in its file, never both. |
| `dark?` | `ThemeLook \| undefined` | The theme's dark look, written here. |
| `light?` | `ThemeLook \| undefined` | The theme's light look, written here. |
| `path?` | `string \| undefined` | A theme file holding the theme's looks instead, from the package root. |

A theme file holds `dark`, `light` or both, written as the manifest would, and exactly the looks its theme's
`appearance` names. It is at most 16 KiB, read as strictly as the manifest is:

```json title="themes/nord.json"
{
  "$schema": "https://songstress.app/schema/theme/v1.json",
  "dark": { "colors": { "background": "#2e3440", "text": "#eceff4", "accent": "#88c0d0", "border": "rgb(236 239 244 / 12%)" } },
  "light": { "colors": { "background": "#eceff4", "text": "#2e3440", "accent": "hsl(212, 42%, 40%)" } }
}
```

## Looks [#looks]

A phone in dark mode shows a theme's dark look, and one in light mode its light look; Android shows the dark look when
there is one. The web and desktop apps show the appearance their person picks. A person may take the dark look of one
theme and the light look of another.

A theme's look, dark or light: its colours, a seed for what they leave out, and the glow behind the stage.

| Field | Type | Description |
|---|---|---|
| `seed?` | `string \| undefined` | One colour the look grows from where it names no other: the accent, with the stage and the text tinted by its hue. |
| `colors?` | `ThemeColors \| undefined` | The look's colour tokens. What it leaves out, an app fills in. |
| `glow?` | `string[] \| undefined` | Up to three colours for the ambient light behind the stage of the web and desktop apps. |

A look of one `seed` is a whole theme: the seed is the accent, and the stage and the text take a little of its hue.

## The tokens [#the-tokens]

Colour tokens by name (SPEC.md, section 12). What a look leaves out, an app fills in.

| Field | Type | Description |
|---|---|---|
| `background?` | `string \| undefined` | The stage behind every screen: lists, a tab's root, the window. |
| `surface?` | `string \| undefined` | Grouped rows, cards and sheets on the stage, and Android's tab bar. |
| `surface.raised?` | `string \| undefined` | What sits on a surface: menus, popovers, the disc of a pull to refresh. |
| `surface.hover?` | `string \| undefined` | The wash under a pressed or hovered row, and behind secondary controls. It may be translucent. |
| `sidebar?` | `string \| undefined` | The sidebar of the web and desktop apps. |
| `border?` | `string \| undefined` | Hairlines between rows and around cards and fields. It may be translucent. |
| `text?` | `string \| undefined` | Words: titles, rows, body copy. |
| `text.muted?` | `string \| undefined` | The second voice: an artist under a title, a caption. |
| `text.faint?` | `string \| undefined` | Placeholders, chevrons, and captions that repeat what is beside them. |
| `accent?` | `string \| undefined` | The theme's colour in Songstress Red's place: icons, active states, progress, links, the tab bar's tint. |
| `accent.fill?` | `string \| undefined` | A filled accent that carries a label: Play, a primary button, a selected segment, a badge. |
| `accent.fill.pressed?` | `string \| undefined` | A filled accent under a press or the pointer, and the far end of Play's sweep. |
| `accent.onFill?` | `string \| undefined` | The label on a filled accent, at rest and pressed. |
| `success?` | `string \| undefined` | Lossless audio, done, connected. |
| `warning?` | `string \| undefined` | Lossy audio, something to look at. |
| `danger?` | `string \| undefined` | Delete, Sign Out, errors, a menu's destructive items. |
| `info?` | `string \| undefined` | Work in progress: running, importing. |

Only `border` and `surface.hover` may be translucent. The tokens are few on purpose: each is a role many parts of the
apps share, never one control's.

## Colours [#colours]

A colour is written in any of these forms, as CSS writes them:

- hex: `#RGB`, `#RGBA`, `#RRGGBB` or `#RRGGBBAA`, in either case;
- `rgb()` and `rgba()`: three channels, each 0 to 255 or 0% to 100%, and an optional alpha, between commas
  (`rgb(108, 182, 255)`) or between spaces with the alpha after a slash (`rgb(108 182 255 / 50%)`);
- `hsl()` and `hsla()`: a hue in degrees or as an angle (`deg`, `grad`, `rad`, `turn`), a saturation and a lightness
  from 0% to 100%, and an optional alpha, written as `rgb()`'s are.

An alpha is 0 to 1 or 0% to 100%. A function's name is lowercase, and a colour is at most 64 characters. A name
(`red`), `currentColor`, `var()`, a gradient and other colour spaces (`oklch()`) aren't colours a theme writes.

Each colour is read into one form, `#rrggbb`, or `#rrggbbaa` below full alpha. A translucent colour anywhere but
`border` and `surface.hover` is refused (`THEME_COLOR_ALPHA`), as a colour in no form is (`THEME_COLOR`).
`songstress/theme` reads them as the apps do:

```ts title="check-colours.ts"
import { normalizeLook, parseColor } from 'songstress/theme'

console.log(parseColor('hsl(212, 42%, 40%)'))
console.log(normalizeLook({ colors: { background: '#2e3440', text: '#eceff4', accent: '#88c0d0' } }))
```

## What a look leaves out [#what-a-look-leaves-out]

An app fills every token a look leaves out, from the look's own colours where it can:

- the seed, for the accent and the tint of the stage and the text;
- `surface` from the background with 8% of the text in it, and `sidebar` as the surface;
- `surface.raised` from the surface with 6% of the text, `border` with 12%, and `surface.hover` as the text at 8%;
- `text.muted` and `text.faint` from the text, 30% and 50% of the way to the background;
- `accent.fill` from the accent; `accent.onFill` as white or near black, whichever reads; and
  `accent.fill.pressed` from the fill, moved 16% away from its label.

Anything else comes from Songstress's own look of the same appearance. So a look of three colours is a whole theme.

## The rules [#the-rules]

A look is shown only if it meets these, once it is filled. Contrast is WCAG 2's ratio.

- The background's relative luminance is under 0.10 in a dark look and over 0.55 in a light one.
- `text` on `background`, `surface`, `surface.raised` and `sidebar`: 4.5:1.
- `text.muted` on `background` and `surface`: 4.5:1.
- `text.faint` on `background`: 3:1. On `surface`: 3:1, and 2.9:1 in a light look.
- `accent` on `background`: 4.5:1. On `surface`: 3:1.
- `success` and `warning` on `background` and `surface`: 4.5:1, and 1.9:1 in a light look.
- `danger` and `info` on `background` and `surface`: 4.5:1.
- `accent.onFill` on `accent.fill` and on `accent.fill.pressed`: 4.5:1.
- On the full player's floor, over black artwork and over white: `text` and `text.muted` 4.5:1, `accent` 3:1.

4.5:1 is a floor, not a target: words read best nearer 7:1. An app holds a theme to the rules before it applies it,
and a look that fails isn't shown. `songstress check` runs the apps' own code and reports a failing look as
`THEME_CONTRAST` or `THEME_LUMINANCE`, with the ratio it reaches.

## What no theme reaches [#what-no-theme-reaches]

The logo and the app's icon, Wrapped's stories and share cards, the pages a public link opens, the first run, and
supporters' medals keep Songstress's own look. So do what the system draws (the splash screen, widgets, the watch and
car screens, system menus and pickers) and pictures. The full player and a page's header over its artwork are themed
like every other screen.

Themes came with API 0.1.3, so a manifest with one asks for `"songstress": "^0.1.3"`. A token is never renamed and
never changes its meaning; a new one comes with a new minor version.
