Colore JS

Search documentation

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

contrastAPCA

contrastAPCA measures lightness contrast for text against a background using the sRGB APCA 0.0.98G-4g calculation. Colore returns the magnitude of Lc, so you can apply the same comparison in light and dark modes.

Example

import { colore } from 'colore-js';
import { contrastAPCA } from 'colore-js/fn';

colore('#111').contrastAPCA('#fff').toFixed(2); // '105.36'
contrastAPCA('#fff', '#111').toFixed(2);        // '107.38'

Syntax

colore(textColor).contrastAPCA(background: ColorInput | Color): number
contrastAPCA(textColor: ColorInput, background: ColorInput): number

Parameters

NameTypeDescription
textColorColorInputThe foreground color.
backgroundColorInputThe color behind the text.

Returns

Both APIs return the absolute Lc score. APCA assigns opposite signs to light-on-dark and dark-on-light text. Colore drops the sign, while swapping the inputs can still change the score. Use in-gamut sRGB colors for this calculation.

Throws

A ColorError when a color cannot be parsed.

Check translucent text against an Lc target

import { contrastAPCA } from 'colore-js/fn';

const lc = contrastAPCA('rgb(0 0 0 / 0.5)', '#fff');

lc.toFixed(2); // '67.13'
lc >= 60;      // true

Colore composites translucent colors in sRGB and assumes white beneath a translucent background. Resolve layers over the actual canvas if it differs. Choose an APCA target for your project; WCAG 2 uses contrast ratios.