Colore JS

Search documentation

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

Pick black or white text for a background color

A badge, avatar or tag takes its background from user data, and the label on it has to be black or white. With Colore that's one line:

import { colore } from 'colore-js';

const label = colore(background).isDark() ? '#ffffff' : '#000000';

isDark() switches at the point where white text starts to get more WCAG 2 contrast than black, so the label gets the color with the higher ratio. On any background, that color reaches at least 4.58:1, which passes WCAG AA for normal text.

npm install colore-js
yarn add colore-js

If your design follows APCA, the newer contrast method, bestTextColor picks by APCA instead:

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

bestTextColor('#3b82f6'); // '#ffffff'
bestTextColor('#facc15'); // '#000000'

On some mid-tone colors the two disagree. On Tailwind's blue-500, isDark() picks black and bestTextColor picks white.

Three ways to choose

Perceived brightness

Many answers online weight the RGB channels into a brightness value from 0 to 255 and switch at 128. The weights come from the YIQ color model used for analog TV.

function textColorByBrightness(hex) {
  const [r, g, b] = [1, 3, 5].map((i) => parseInt(hex.slice(i, i + 2), 16));
  const brightness = (r * 299 + g * 587 + b * 114) / 1000;
  return brightness >= 128 ? '#000000' : '#ffffff';
}

It's short, but it predates the WCAG 2 contrast formula, and it reads only six-digit hex.

WCAG 2 contrast ratio

Pick whichever of black and white has the higher WCAG 2 ratio. This is the method behind the 4.5:1 rule that accessibility audits check, and it's what isDark() does in one call. Written out with contrastRatio, it looks like this:

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

function textColorByWcag(background) {
  return contrastRatio('#ffffff', background) >= contrastRatio('#000000', background) ? '#ffffff' : '#000000';
}

Without a library, compute the relative luminance as in Check color contrast and use white text when it's 0.179 or less.

APCA

APCA is a newer contrast method, developed as a possible replacement for the WCAG 2 formula. It isn't part of WCAG 2, which is the version audits and accessibility laws refer to. bestTextColor uses it.

Where they disagree

These are Tailwind CSS 3's 500 shades. Slate stands in for the five grays, which all pick white. The last two columns are the WCAG 2 ratios of white and black text on each one.

ColorBackgroundBrightnessWCAG 2APCAWhiteBlack
red#ef4444whiteblackwhite3.765.58
orange#f97316blackblackwhite2.807.49
amber#f59e0bblackblackblack2.159.78
yellow#eab308blackblackblack1.9210.95
lime#84cc16blackblackblack1.9810.63
green#22c55eblackblackblack2.289.22
emerald#10b981blackblackblack2.548.28
teal#14b8a6blackblackblack2.498.44
cyan#06b6d4blackblackblack2.438.65
sky#0ea5e9whiteblackwhite2.777.58
blue#3b82f6whiteblackwhite3.685.71
indigo#6366f1whiteblackwhite4.474.70
violet#8b5cf6whiteblackwhite4.234.96
purple#a855f7blackblackwhite3.965.31
fuchsia#d946efblackblackwhite3.466.07
pink#ec4899blackblackwhite3.535.95
rose#f43f5ewhiteblackwhite3.675.72
slate#64748bwhitewhitewhite4.764.41

WCAG 2 picks black on every color except slate. APCA picks white on eleven of them, and on ten of those eleven, white is below the 4.5:1 that WCAG 2 AA needs for normal text. The brightness formula matches APCA on 14 of the 18 and WCAG 2 on 12, so it fails WCAG 2 on six of them.

WCAG 2 pick, 5.71:1 contrast, #000000 on #3b82f6
APCA pick, 3.68:1 contrast, #ffffff on #3b82f6

If your site is audited against WCAG 2, use isDark(). The APCA pick on this blue is 3.68:1, below AA.

Keep white text on a brand color

Sometimes the design calls for white text on a brand color that's a little too light for it. isReadable tells you whether the pair passes WCAG 2 AA, and ensureContrast darkens the background just enough to pass:

import { contrastRatio, ensureContrast, isReadable } from 'colore-js/fn';

isReadable('#ffffff', '#3b82f6'); // false

const fixed = ensureContrast('#3b82f6', '#ffffff'); // adjusts the first color, the background here
// { color: '#2c72e5', satisfied: true, iterations: 1 }
contrastRatio('#ffffff', fixed.color); // 4.52055373235116

For a full theme, generateSystem checks every text and background pair. By default it follows WCAG 2, so on #3b82f6 it keeps the blue and uses black text. Pass { algorithm: 'apca' } to choose by APCA. You can try it on your own brand color in the theme generator.