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
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.
Related
- Check color contrast in JavaScript to review individual pairs
- Dark mode colors to audit both modes
- ensureContrast to adjust one foreground