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
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.
Related
- Black or white text for how APCA, WCAG 2 and brightness picks compare
- Check color contrast in JavaScript to verify the chosen color
- isReadable to apply a minimum
- ensureContrast to adjust a color when the candidates fall short