Colore JS

Search documentation

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

bestTextColor

bestTextColor picks black or white text for a background. Pass an array of approved colors to choose from that set instead. Colore compares the candidates using absolute APCA contrast.

Example

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

colore('#7c3aed').bestTextColor().css(); // '#ffffff'
bestTextColor('#7c3aed');                // '#ffffff'

Syntax

colore(background).bestTextColor(candidates?: ColorInput[]): Color
bestTextColor(background: ColorInput, candidates?: ColorInput[]): string

Parameters

NameTypeDescription
backgroundColorInputThe color behind the text.
candidatesColorInput[]Allowed foreground colors. Defaults to black and white.

Returns

The method returns the winning candidate as a Color. The function returns it as CSS. It uses hex if the winner fits sRGB, Display P3 if it fits that wider gamut, and OKLCH otherwise. Check isReadable when you also require a specific minimum.

Throws

A ColorError for invalid colors or an empty candidate list.

Choose from a brand palette

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

bestTextColor('#000', ['#aaa', 'color(display-p3 0 1 0)']);
// 'color(display-p3 0.00000 1.00000 0.00000)'

For contrast checks with a wide-gamut candidate, map the candidate to sRGB before measuring an sRGB threshold.