# Color

A token names a role; the user's theme picks the colour. Every surface, line and text reads one.

API [Theme](/ui/storybook/?path=/docs/ui-foundations-theme--docs) [Terminal](/ui/storybook/?path=/docs/ui-foundations-terminal--docs) [Badge](/ui/storybook/?path=/docs/ui-primitives-badge--docs) [Button](/ui/storybook/?path=/docs/ui-primitives-button--docs) [Card](/ui/storybook/?path=/docs/ui-primitives-card--docs) [DropdownMenu](/ui/storybook/?path=/docs/ui-primitives-dropdownmenu--docs) [InlineCode](/ui/storybook/?path=/docs/ui-primitives-inlinecode--docs) [Pane](/ui/storybook/?path=/docs/ui-primitives-pane--docs)

**Draw with the tokens, never with a colour.** `bg-card`, `text-muted-foreground` and `border` say what a thing is; the theme the user picked says what colour that is, in light, in dark, and in each of the palettes they can pick.

```tsx
import { CI_RUN } from "@orz/fixtures/ci-run.ts";
import { Badge } from "@orz/ui/components/badge";
import { Button } from "@orz/ui/components/button";
import {
  Card,
  CardContent,
  CardFooter,
  CardHeader,
  CardTitle,
} from "@orz/ui/components/card";
import { InlineCode } from "@orz/ui/components/inline-code";
import { List, ListItem } from "@orz/ui/components/list";
import { CircleCheckIcon, CircleXIcon, GitMergeIcon } from "lucide-react";

const failed = CI_RUN.jobs.filter((job) => job.status === "failed").length;

export function MergeBox() {
  return (
    <Card intent="danger" role="region" aria-labelledby="merge-box-status">
      <CardHeader icon={<CircleXIcon />}>
        <CardTitle>
          <h3 id="merge-box-status">Some checks were not successful</h3>
        </CardTitle>
      </CardHeader>
      <CardContent>
        <p className="text-muted-foreground text-ui">
          {failed} failing and {CI_RUN.jobs.length - failed} successful checks
          on <InlineCode>{CI_RUN.commit}</InlineCode>
        </p>
      </CardContent>
      <CardContent>
        <List purpose="collection">
          <ul>
            {CI_RUN.jobs.map((job) => (
              <ListItem key={job.id} asChild>
                <li className="flex min-h-row items-center gap-3 py-1.5">
                  <span className="min-w-0 flex-1 truncate text-ui">
                    {CI_RUN.workflow} / {job.name}
                  </span>
                  <span className="text-muted-foreground text-ui-sm tabular-nums">
                    {job.duration}s
                  </span>
                  {job.status === "failed" ? (
                    <Badge intent="danger">
                      <CircleXIcon /> Failed
                    </Badge>
                  ) : (
                    <Badge intent="success">
                      <CircleCheckIcon /> Passed
                    </Badge>
                  )}
                </li>
              </ListItem>
            ))}
          </ul>
        </List>
      </CardContent>
      <CardFooter className="flex-wrap gap-3">
        <Button disabled>
          <GitMergeIcon />
          Merge pull request
        </Button>
        <p className="text-muted-foreground text-ui">
          Merging is blocked until every check passes.
        </p>
      </CardFooter>
    </Card>
  );
}
```

A merge box like the pull request demo's, its card in the `danger` intent. Nothing in it names a colour: the card, the failed check and the passed ones all read a token. Pick another theme from the top bar and the box follows, as does every swatch on this page, values and contrast included.

## Surfaces and their text

Each surface comes with the foreground meant for text on it. Put text on a surface in that surface's foreground, or in `muted-foreground` for the secondary line. The number on each swatch is the contrast of the pair in the theme you are looking at.

Aa

`background / foreground`

Aa

`background / muted-foreground`

Aa

`card / card-foreground`

Aa

`popover / popover-foreground`

Aa

`muted / muted-foreground`

Aa

`sidebar / sidebar-foreground`

Aa

`primary / primary-foreground`

Aa

`secondary / secondary-foreground`

Aa

`accent / accent-foreground`

| The thing                                                                                          | Surface                                               | Text                                                     |
| -------------------------------------------------------------------------------------------------- | ----------------------------------------------------- | -------------------------------------------------------- |
| The page                                                                                           | `bg-background`                                       | `text-foreground`                                        |
| A secondary line, a timestamp, a hint                                                              | —                                                     | `text-muted-foreground`                                  |
| A card, a panel set off from the page                                                              | `bg-card`                                             | `text-card-foreground`                                   |
| A menu, popover, dialog or sheet                                                                   | `bg-popover`                                          | `text-popover-foreground`                                |
| A well, a quiet fill                                                                               | `bg-muted`                                            | `text-muted-foreground` or `text-foreground`             |
| A name from code in a sentence: an `InlineCode`                                                    | `bg-code`, the ink laid thin over whatever it sits on | `text-code-ink`, the line's own colour with a little ink |
| A sidebar set beside the rest on one sheet, as an app's is below `md`                              | `bg-sidebar`                                          | `text-sidebar-foreground`                                |
| The one main action in view                                                                        | `bg-primary`                                          | `text-primary-foreground`                                |
| What the primary marks as on: a box ticked, a radio chosen, a switch on, a slider's run            | `bg-primary`                                          | `text-primary-foreground`                                |
| What the primary marks as current: a nav's current place                                           | `bg-tint`, the primary's tint                         | `text-tint-ink`                                          |
| A second action beside it: a `secondary` button                                                    | `bg-tint`, edged in `border-tint-edge`                | `text-tint-ink`                                          |
| A key: an outline button, a select, a toggle, a chosen segment                                     | `bg-key`, edged in `border-key-edge`                  | `text-foreground`                                        |
| A hovered, highlighted or open row, with a `state-cue` tick on its leading edge                    | `bg-accent`                                           | `text-accent-foreground`                                 |
| A list box's or a table's picked row: no fill, a `pick-cue` bar of the primary on its leading edge | —                                                     | `text-foreground`                                        |
| Something the user has set that is in force: a `Badge variant="marker"`                            | `bg-marker`                                           | `text-primary`                                           |

The marker's colours mark one thing: something the user has set that is in force, such as the count a toolbar's `⋯` shows of the filters in use inside it. That is a `Badge variant="marker"`, edged in a tint of the accent, so it reads beside the one main action without competing with its fill.

```tsx
import { INBOX_PEOPLE, INBOX_THREADS } from "@orz/fixtures/inbox.ts";
import { Button } from "@orz/ui/components/button";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@orz/ui/components/dropdown-menu";
import { Frame } from "@orz/ui/components/frame";
import { InlineCode } from "@orz/ui/components/inline-code";
import { List, ListItem } from "@orz/ui/components/list";
import {
  NavList,
  NavListCount,
  NavListFrame,
  NavListItem,
  NavListLink,
} from "@orz/ui/components/nav-list";
import { Pane } from "@orz/ui/components/pane";
import { Timestamp } from "@orz/ui/components/timestamp";
import {
  Tooltip,
  TooltipContent,
  TooltipProvider,
  TooltipTrigger,
} from "@orz/ui/components/tooltip";
import {
  EllipsisIcon,
  FileTextIcon,
  ForwardIcon,
  InboxIcon,
  LinkIcon,
  SendIcon,
  SquarePenIcon,
  StarIcon,
} from "lucide-react";
import { useState } from "react";
import { toast } from "sonner";

const FOLDERS = [
  { title: "Inbox", Icon: InboxIcon, count: 3 },
  { title: "Starred", Icon: StarIcon },
  { title: "Sent", Icon: SendIcon },
  { title: "Drafts", Icon: FileTextIcon, count: 1 },
];

const ROWS = ["touch-sizes", "contrast-audit", "flaky-retry"].flatMap((id) => {
  const thread = INBOX_THREADS.find((each) => each.id === id);
  const last = thread?.messages.at(-1);
  return thread && last ? [{ thread, last }] : [];
});

export function MailSurfaces() {
  const [folder, setFolder] = useState("Inbox");
  const [open, setOpen] = useState("touch-sizes");
  return (
    <TooltipProvider>
      <Frame asChild>
        <Pane purpose="app" className="grid sm:grid-cols-[10rem_1fr]">
          <Pane
            purpose="sidebar"
            edge="end"
            className="grid content-start gap-3 p-2 touch:pt-hit-sm max-sm:hidden"
          >
            <Button size="sm" className="justify-start">
              <SquarePenIcon />
              Compose
            </Button>
            <NavListFrame asChild>
              <nav aria-label="Folders">
                <NavList>
                  {FOLDERS.map(({ title, Icon, count }) => (
                    <NavListItem key={title}>
                      <NavListLink current={title === folder} asChild>
                        <button type="button" onClick={() => setFolder(title)}>
                          <Icon />
                          {title}
                          {count && <NavListCount>{count}</NavListCount>}
                        </button>
                      </NavListLink>
                    </NavListItem>
                  ))}
                </NavList>
              </nav>
            </NavListFrame>
          </Pane>
          <Pane purpose="panel" className="min-w-0">
            <Pane
              purpose="toolbar"
              edge="bottom"
              className="flex items-center gap-2 px-3 py-3"
            >
              <span className="font-medium text-ui">{folder}</span>
            </Pane>
            <List purpose="listbox">
              <ul aria-label="Conversations">
                {ROWS.map(({ thread, last }) => (
                  <ListItem
                    key={thread.id}
                    current={thread.id === open}
                    asChild
                  >
                    <li className="flex items-start">
                      <button
                        type="button"
                        aria-current={thread.id === open ? "true" : undefined}
                        onClick={() => setOpen(thread.id)}
                        className="grid min-w-0 flex-1 grid-cols-[1fr_auto] gap-x-2 px-3 py-2.5 text-left"
                      >
                        <span className="truncate font-medium text-ui">
                          {INBOX_PEOPLE[last.from] ?? last.from}
                        </span>
                        <Timestamp
                          value={last.at}
                          format="date"
                          className="text-muted-foreground text-ui-sm"
                        />
                        <span className="col-span-2 truncate text-ui">
                          {thread.subject}
                        </span>
                        <span className="col-span-2 truncate text-muted-foreground text-ui-sm">
                          {last.body
                            .split("`")
                            .map((part, index) =>
                              index % 2 === 1 ? (
                                <InlineCode key={part}>{part}</InlineCode>
                              ) : (
                                part
                              ),
                            )}
                        </span>
                      </button>
                      {thread.id === open && (
                        <DropdownMenu>
                          <Tooltip>
                            <TooltipTrigger asChild>
                              <DropdownMenuTrigger asChild>
                                <Button
                                  variant="ghost"
                                  size="icon-xs"
                                  className="mt-2 mr-2 touch:mr-hit-xs"
                                  aria-label={`More actions for “${thread.subject}”`}
                                >
                                  <EllipsisIcon />
                                </Button>
                              </DropdownMenuTrigger>
                            </TooltipTrigger>
                            <TooltipContent>More actions</TooltipContent>
                          </Tooltip>
                          <DropdownMenuContent align="end" className="w-44">
                            <DropdownMenuItem>
                              <ForwardIcon />
                              Forward
                            </DropdownMenuItem>
                            <DropdownMenuItem
                              onSelect={() => toast("Link copied")}
                            >
                              <LinkIcon />
                              Copy link
                            </DropdownMenuItem>
                          </DropdownMenuContent>
                        </DropdownMenu>
                      )}
                    </li>
                  </ListItem>
                ))}
              </ul>
            </List>
          </Pane>
        </Pane>
      </Frame>
    </TooltipProvider>
  );
}
```

The Mail demo in miniature, on the surfaces in the table: the folders on the app's ground with no fill of their own (on `sidebar` below `md`, where the app is one sheet), **Compose** on `primary`, the current folder in the primary's tint, the open conversation on `accent`, names and dates on the page's `foreground` and `muted-foreground`, a code span in `bg-code`, and **More actions** opening on `popover`. The merge box above is the card. On a phone the folders hide, as they do in the demo.

The components already make these choices: a `Card` is `bg-card`, a `Button` is `bg-primary`, and a `Pane` decides when a sidebar is `bg-sidebar` ([Surfaces](/ui/guidelines/foundations/surfaces/index.md#panes)). Reach for the tokens yourself for the surfaces you build.

## Selected text

Aa

`selection / selection-foreground`

Text the user selects is drawn in `selection-foreground` on `selection`, on every surface: the page, a card, a menu, a field and an editor alike. The base layer sets it once, so leave `::selection` alone in what you build; a coloured span, a link or a muted line turns `selection-foreground` while selected, so the selection reads as one run. The fill stands off the page, the cards, the popovers, `muted` and the sidebar, and its text keeps 4.5:1 on it. A terminal is the exception: an ANSI log and a cast player select in `terminal-selection` and keep their ANSI colours, as a terminal does.

## Lines and focus

`border`

`input`

`ring`

`border` separates; `input` outlines a field; `ring` draws focus. `input` is the darker of the two lines: it is the edge an empty field, an unchecked checkbox and a switch's off track are found by, so it keeps 3:1 against the page, a card and a popover, where `border` only has to be seen beside what it divides. The themes used as published keep their own edges, a few of them lighter; [Accessibility](/ui/guidelines/content/accessibility/index.md#contrast) lists them, and its contrast table measures the theme you are in. Outline a control you build in `input`, and divide in `border`. The base layer gives every element `border-border`, so a bare `border` class is already the right colour. A line is the quietest way to separate two things, and space is quieter still: prefer a gap to a rule where the grouping reads without one.

## Meaning

`destructive` is for what deletes, removes or fails, and for nothing else; a red that means "important" teaches people to ignore the red that means "gone".

`destructive`

`success`

`warning`

`diff-addition`

`diff-deletion`

`chart-1`

`chart-2`

`chart-3`

`chart-4`

`chart-5`

- `success` is for what finished as it should: a passed run, a saved change, a check that holds. `warning` is for what wants the reader before it can go on: a question waiting on their answer, or what still works but needs a look, such as stale data, a value that passes only for large text or an input the app guessed at. Like `destructive`, each is drawn as text, an icon or a border, or as a tint of itself (`Badge intent="success"`). A frame or a card in an `intent` is edged in it, its lid or header takes the tint and what it holds a fainter wash, under words in the colour's ink ([Surfaces](/ui/guidelines/foundations/surfaces/index.md)). The one solid fill of a status colour is a button's whose `intent` makes a verdict the action its view is for, as [Actions & buttons](/ui/guidelines/patterns/actions-and-buttons/index.md#a-verdict-in-colour) describes.
- `diff-addition` and `diff-deletion` are the added and removed lines of a diff, the colours a reader already knows from code review. A diff uses them and nothing else does; a passed run is `success`.
- `chart-1` to `chart-5` are series in a chart, in order, each held to 3:1 against the page. They tell series apart; they carry no meaning of their own.
- There is no info token. Neutral news needs no colour: write it in `foreground` or `muted-foreground`, with an icon if it has to stand out.

**Colour is never the only signal.** A state shown only as a red or green dot is invisible to a colour-blind reader and to anyone in a palette where the two are close ([WCAG 1.4.1](https://www.w3.org/WAI/WCAG22/Understanding/use-of-color.html)). Pair it with a word, an icon, or both.

**Do**

Say the state, and let the colour back it up.

**Don't**

Show a state as a colour alone, and in a hard-coded red that no theme can change.

### Tags

`tag-red`

`tag-orange`

`tag-yellow`

`tag-green`

`tag-teal`

`tag-blue`

`tag-purple`

`tag-pink`

`tag-gray`

A tag, the label a user puts on a task or a conversation, takes one of 9 named colours: `Badge variant="marker"` with a `color` writes its name in the colour, on a light fill of its own colour. Like the chart series, the colours tell tags apart and say nothing themselves: a red tag is not an error, and the accent still marks only where the user is and what they can do. Every theme keeps each colour's hue, so a `blue` tag is blue whichever theme the user picks, and sets its lightness so the name holds 4.5:1 on its own fill. Where tags are set among an item's own words, a card's on a board, `variant="dot"` draws each as a dot of its colour before its name, so the item's title still leads.

```tsx
import { TASK_LABEL_COLORS, TASK_LABELS } from "@orz/fixtures/tasks.ts";
import { autoTagColor, Badge } from "@orz/ui/components/badge";

// The same labels as a forge's import might colour them.
const IMPORTED: Readonly<Record<string, string>> = {
  bug: "#d73a4a",
  ci: "#0e8a16",
  docs: "#0075ca",
  design: "#d876e3",
  perf: "#fbca04",
  release: "#5319e7",
};

const ROWS = [
  { how: 'color="red"', colorOf: (label: string) => TASK_LABEL_COLORS[label] },
  { how: "autoTagColor(id)", colorOf: (label: string) => autoTagColor(label) },
  { how: 'color="#d73a4a"', colorOf: (label: string) => IMPORTED[label] },
];

export function TagColors() {
  return (
    <dl className="grid grid-cols-[8.5rem_1fr] items-center gap-x-4 gap-y-3 text-ui">
      {ROWS.map(({ how, colorOf }) => (
        <div key={how} className="contents">
          <dt className="font-mono text-muted-foreground text-ui-sm">{how}</dt>
          <dd className="flex flex-wrap gap-1">
            {TASK_LABELS.map((label) => (
              <Badge key={label} variant="marker" color={colorOf(label)}>
                {label}
              </Badge>
            ))}
          </dd>
        </div>
      ))}
    </dl>
  );
}
```

- **The colour the app stores.** Where people pick a tag's colour, store what they picked and pass it to `color`: one of the names, or any colour value, such as one an import from a forge brings. A value is drawn at its own hue from the theme's colours, so GitHub's `#d73a4a` stays a red that reads in every theme.
- **A colour from the tag.** Where tags have no colour of their own, `autoTagColor(id)` picks one from the tag's id, the same one everywhere. Give it the id, not the name on screen, which a rename or a translation changes. `autoTagColor` never picks `gray`, which is left for a tag meant to have no colour at all.
- `autoTagColor`'s colours repeat soon, and a handful of tags will share one. Where every tag must look apart, store a colour for each.

The name is the tag; the colour only helps the eye find it. Wherever a tag is picked, in a filter's menu, a sidebar or a `Select`, draw the tag itself, as the list does, so the colour that finds it in the list finds it where it is chosen.

## Tints

What is pressed or on in the primary is the primary itself, its words and marks reversed out of it: the primary `Button`, a box ticked, a radio chosen, a switch that is on and a slider's run. A tint of a primary with no hue is one more grey, and stands too close to off for on to read. A place that is current, which is read rather than pressed, is tinted instead: `bg-tint` under the words in `text-tint-ink`, the primary held dark enough on light, and light enough on dark, to read on the tint in every palette.

The `secondary` button is the one tint that is pressed. It is edged in the primary (`border-tint-edge`), which ranks it between the solid primary and a grey key, and it steps as a key does: more of the primary under the pointer (`bg-tint-hover`), more again held down (`bg-tint-held`; on dark, back toward the card). A button's or a segment's `intent` stands a status colour in for the primary, and every one of these looks follows it: the solid, the tint, its edge and its ink.

The grey keys, from an outline button to a chosen segment, are not quite grey: each is a step of lightness from the card, in the page's own hue. A palette whose cards are white and whose page is cream draws cream keys, which sit in the page rather than on it. The theme store writes the page's chroma and hue beside the tokens for them, so a theme of your own needs nothing more.

A token takes an opacity: `bg-primary/10`, `bg-destructive/20`. That is how a tint stays in the theme. Text on a tint of its own colour is where contrast fails first, so a component that sets `destructive` text on a `destructive` tint mixes the foreground in, `text-[color-mix(in_oklab,var(--destructive),var(--foreground)_65%)]`, as `Badge` does; a button in a status colour writes its words in that colour's `text-tint-ink`, which holds 4.5:1 on its tint and on a key in every built-in palette.

**Do**

Write `text-muted-foreground` for the secondary line. Each theme sets it for its own surfaces, and the swatches above show how it holds in the one you are in.

**Don't**

Write `text-gray-500`, `bg-white` or a hex value. It is right in one theme, and in the others nobody checks.

Do not reach for `dark:` to fix a colour either: a token already flips with the scheme, and `dark:` knows two schemes where the user has many themes.

## Contrast

Text needs 4.5:1 against what it sits on, and 3:1 for large text, 24px or 18.7px bold ([WCAG 1.4.3](https://www.w3.org/WAI/WCAG22/Understanding/contrast-minimum.html)). A control's outline, a focus ring and an icon that carries meaning need 3:1 ([WCAG 1.4.11](https://www.w3.org/WAI/WCAG22/Understanding/non-text-contrast.html)). The pairs above show the ratio in the theme you are in; a pair marked **under 4.5** holds secondary text only in that theme, so keep what matters in the surface's own foreground.

## Colours a style adds

A theme belongs to a [style](/ui/guidelines/foundations/styles/index.md), and a style may ask its themes for colours beyond the shared tokens: raymond draws its bevels from `3d-face`, `3d-highlight` and `3d-shadow`, and its title bar from `caption` and `caption-gradient`. Those are the style's to read. Draw your own surfaces from the shared tokens above, which every theme of every style supplies, and they hold when the user switches style.

## Terminal colours

A terminal, an ANSI log and a cast player draw from their own tokens: a background, foreground, cursor and selection, and the sixteen ANSI colours. They follow the UI theme unless the user picks a terminal theme of their own, so use them for terminal output only.

`terminal-background`

`terminal-foreground`

`terminal-cursor`

`terminal-selection`

`ansi-black`

`ansi-red`

`ansi-green`

`ansi-yellow`

`ansi-blue`

`ansi-magenta`

`ansi-cyan`

`ansi-white`

`ansi-bright-black`

`ansi-bright-red`

`ansi-bright-green`

`ansi-bright-yellow`

`ansi-bright-blue`

`ansi-bright-magenta`

`ansi-bright-cyan`

`ansi-bright-white`

## In the demos

- [Tasks](/ui/demos/tasks/index.md): Each label takes the colour stored for it: in the table and on the board, in a task's details, and where a label is picked, the Label filter's menu and New task's Select, so a label is found by its colour wherever it is chosen.
- [Mail](/ui/demos/mail/index.md): Each label takes the colour the mailbox stores for it: on a conversation in the list, above the open one, and where a label is picked, the sidebar, the rail's Labels menu and the conversation's Labels menu.
- [Pull request](/ui/demos/pull-request/index.md): A check's icon is a tick or a cross, named for a screen reader, so colour is never the only sign, and the open job says “failed in 7s” in words. The +/− counts take the diff colours, and the logs the terminal theme. The labels keep the colours the forge stores for them, each drawn at its hue in the theme's tag colours.
- [Repository](/ui/demos/repository/index.md): A changed file's name and letter take the status colour, and the letter says it without the colour: M for README.md, A for src/retry.ts, D for docs/old-guide.md, struck through.
