Skip to main content

resolveAccent

Resolves one accent colour into the roles it drives, and checks each against WCAG 2.2 AA with the same pairs and backdrops as checkThemeContrast.

This is a function exported by @mapcairn/theme, checked against version 0.2.0.

Signature​

import { resolveAccent } from "@mapcairn/theme";

function resolveAccent(accent: unknown, explicit?: Partial<Record<ThemeName, Partial<ThemeColours>>>, themes?: readonly ThemeName[]): AccentResolution;

Description​

The accent is used as given in a theme where it passes with its derived roles: the tint (the SDK's own lightness and chroma, in the accent's hue), the text on the accent (white or near black, whichever reads better) and the focus ring (the accent). Where it does not pass, its lightness moves to the nearest value that does, with the hue kept and the chroma cut to the sRGB gamut, and derived says so. A role the host set in explicit is never moved and is not in the result: it is checked, and a failure is refused with the nearest passing colour named. A host-set accent drives its own theme's derivation. When no lightness passes, the failure has no nearest and names the pair that fails.

Pure, and total for input of the declared types: the same input gives the same result. An explicit colour that cannot be read, or a role that is not one a host may set, is left out: checkThemeContrast reports it. Only the pairs that involve the four roles are judged here; the caller checks the whole theme.

Parameters​

  • accent (unknown): The brand colour: hex, rgb() or oklch(), opaque and inside sRGB.
  • explicit? (Partial<Record<ThemeName, Partial<ThemeColours>>>): The roles the host set for each theme. Left out, none.
  • themes? (readonly ThemeName[]): The themes to resolve; a name that is not light or dark is ignored. Left out, both; the runtime route passes the one that changed, so a theme change does not search the other.

Returns​

AccentResolution: The roles to use and how each was reached, or why it was refused.

See also​

Source: packages/theme/src/accent.ts:416