Colore JS

Search documentation

Find a function or jump to a section.
v2.0.1

audit

audit checks a named palette for contrast, gamut and colors that are difficult to distinguish. It tells you which pairs fail and suggests a replacement when Colore can find one. Color vision checks for protanopia, deuteranopia and tritanopia are on by default.

Example

import { audit } from 'colore-js';
import { audit as auditPalette } from 'colore-js/fn';

const colors = { text: '#777777', background: '#ffffff' };
const result = audit(colors);

result.passed;                      // false
result.checked;                     // 1
result.issues[0]?.suggestion?.value; // '#767676'
auditPalette(colors).issues[0]?.type; // 'contrast'

Syntax

audit(input: AuditInput, options?: AuditOptions): AuditResult

Parameters

NameTypeDescription
inputRecord<string, ColorInput | Color>Colors keyed by token or role name.
options.algorithm / options.levelContrastAlgorithm / ContrastLevelDefaults to WCAG 2 AA. APCA levels apply Colore’s Lc presets.
options.gamut'srgb' | 'p3'Gamut to check. Defaults to sRGB.
options.minDeltaE / options.minCvdDeltaEnumber / numberCIEDE2000 distance for normal and simulated vision. Defaults to 10 and 8.
options.checkColorBlind / options.cvdTypesboolean / ColorBlindnessType[]Color vision checks are on by default, with protanopia, deuteranopia and tritanopia.
options.pairs / options.autoPairsColorPair[] or foreground/background tuples / booleanSupply explicit pairs by name. Automatic pairing defaults to true.

Returns

AuditResult includes passed, the number of pairs checked, and issues. Contrast and configuration errors fail the audit. Gamut, distinction, and color vision findings are warnings. Issues identify the colors involved and include a measured value, threshold or suggestion when available.

Throws

A ColorError for invalid colors or options. An explicit pair naming a missing color appears as a configuration error in the result.

CSS custom properties

Use the root exports to check or repair color custom properties. auditCssVariables returns a CssAuditResult with the checked scopes and resolved values on each issue. fixCssVariables returns a CssFixResult that adds css and changes.

import { auditCssVariables, fixCssVariables } from 'colore-js';

const css = ':root { --background: #ffffff; --foreground: #999999; }';

auditCssVariables(css).issues[0]?.message;
// '--foreground on --background in :root: 2.85:1, needs 4.5:1'

fixCssVariables(css).css;
// ':root { --background: #ffffff; --foreground: #767676; }'

The CSS functions accept algorithm, level, pairs and autoPairs. They detect names such as --primary-foreground for --primary. When supplying named pairs, you can set an intent such as 'ui' on each pair object, and a scope such as '.dark' to check that pair in one selector only.