Skip to content

Colour and contrast

One hue makes the palettes. Colours are used by role, and every role pairing is checked in light and dark mode.

Palettes

Your brand hue generates two palettes in OKLCH, and four status palettes come with them, each with the steps 50 to 950:

  • brand: your hue at your chroma (default 0.16)
  • neutral: greys with a faint tint of the brand hue
  • danger, success, warning and info: red, green, amber and blue, whatever the brand, so a status reads the same on every site

Each step has the same lightness in every palette, so contrast doesn't depend on the hue: brand.600 passes AA on white whether your brand is blue, green or orange. Colours that can't be shown on an ordinary screen are pulled back until they can.

Roles

Markup never names a palette step. It names a role, and the config says which step the role is:

RoleDefaultFor
surfaceneutral.50the page background
surface-raisedneutral.100cards, code blocks, panels
borderneutral.200dividers and panel edges
border-fieldneutral.500form field edges
textneutral.950body text and headings
text-mutedneutral.700secondary text
text-linkbrand.700links
text-dangerdanger.700error messages, invalid fields
accentbrand.600primary buttons, focus rings
on-accentwhitetext on accent

Status roles

Each status has a tinted surface and a text colour. The alert and badge components use them, and so can you (s="bg-surface-success text-success", or var(--k-text-success) in your CSS). Apart from text-danger, which form fields use, they reach the stylesheet only when something uses them.

StatusTextSurface
dangertext-danger danger.700surface-danger danger.100
successtext-success success.700surface-success success.100
warningtext-warning warning.700surface-warning warning.100
infotext-info info.700surface-info info.100

Change one, or add your own, in the config:

color: {
  "text-muted": "neutral.600",
  highlight: "brand.100",
  "on-highlight": "neutral.950",
}

Dark mode

A role written as one step is mirrored for dark mode: 50 becomes 950, 600 becomes 400, white becomes black. Dark mode follows the reader's system setting, and data-theme="light" or "dark" on <html> forces one.

Where the mirror isn't what you want, give both:

color: {
  accent: { light: "brand.600", dark: "brand.300" },
}

A theme switch

To let people pick light or dark on your site, set data-theme from a button and remember it. This is the switch in this site's footer. The first script goes in <head>, so the page paints in the right theme:

<script>
  const theme = localStorage.getItem("theme");
  if (theme) document.documentElement.dataset.theme = theme;
</script>

The button names the theme it switches to, so it reads Light theme while the page is dark:

<button type="button" id="theme" s="button">Dark theme</button>
<script>
  const button = document.getElementById("theme");
  const root = document.documentElement;
  const isDark = () => (root.dataset.theme ?? (matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light")) === "dark";
  const show = () => (button.textContent = isDark() ? "Light theme" : "Dark theme");
  button.addEventListener("click", () => {
    root.dataset.theme = isDark() ? "light" : "dark";
    localStorage.setItem("theme", root.dataset.theme);
    show();
  });
  matchMedia("(prefers-color-scheme: dark)").addEventListener("change", show);
  show();
</script>

Nothing else changes: both themes are already in the stylesheet, and both are checked.

What is checked

In the config, both modes:

  • every on-X role on its X role, e.g. on-accent on accent
  • every text… role on every surface… role
  • every role whose name starts with border- (border-field and your own) on every surface, at 3:1 (WCAG 1.4.11, for things that aren't text). The plain border role isn't: it's for dividers and card edges, which WCAG doesn't ask to stand out, so keep form fields and controls on border-field.
  • every component's text on its background, and for interactive ones, on its hover and active shades too

In markup, wherever an element changes its text colour or background, against the background it actually sits on, inherited through the page. Large text (24px, or 18.66px bold) needs 3:1 instead of 4.5:1, and text that shrinks on phones is checked at its phone size.

✗ kerf.config.js  text-muted on surface — contrast light 2.3:1 (neutral.400 on neutral.50), dark 2.9:1 (neutral.600 on neutral.950), needs 4.5:1 → try "text-muted": "neutral.600"

The level is WCAG AA by default; set rules: { contrast: "AAA" } for AAA (7:1, or 4.5:1 for large text). Borders stay at 3:1, which is all WCAG asks of them.

The default roles are tuned for AA, so AAA on its own fails the config, with a suggestion for each role. With the default palettes, these roles pass AAA everywhere, the built-in components included:

rules: { contrast: "AAA" },
color: {
  "text-muted": "neutral.800",
  "text-link": "brand.800",
  accent: "brand.700",
  "surface-raised": { light: "neutral.100", dark: "neutral.950" },
  "text-danger": "danger.800",
  "text-success": "success.800",
  "text-warning": "warning.800",
  "text-info": "info.800",
},