Colore JS

Search documentation

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

categoricalColors

categoricalColors picks colors for chart series, legends and tags. It favors colors that stay far apart under normal vision and two red-green color vision simulations, and checks each one against your chart background (3:1 by default, or Lc 45 with APCA).

Example

import { categoricalColors } from 'colore-js';
import { categoricalColors as chartColors } from 'colore-js/fn';

categoricalColors(3).map((color) => color.css()); // ['#1781d4', '#4ba81b', '#ad1a6b']
chartColors(3);                                  // ['#1781d4', '#4ba81b', '#ad1a6b']

Syntax

// colore-js
categoricalColors(count: number, options?: CategoricalColorOptions): Color[]

// colore-js/fn
categoricalColors(count: number, options?: CategoricalColorOptions): string[]

Parameters

NameTypeDescription
countnumberA whole number from 1 to 12.
options.backgroundColorInputThe chart background. Defaults to white.
options.seedColorInputOptional first series color. Colore keeps it as supplied, so check its contrast separately.
options.algorithm'wcag2' | 'apca'Contrast measure. Defaults to WCAG 2.
options.level / options.target'aa' | 'aaa' / numberGenerated colors default to a 3:1 ratio or Lc 45. Pass target to set a different minimum.

Returns

The root function returns Color[]. The /fn version returns CSS strings. With the same options, increasing the count keeps the earlier colors in place.

Throws

A ColorError for invalid colors, unsupported options, an out-of-range count or too few colors at the requested contrast.

Pick colors for a dark dashboard

import { categoricalColors as chartColors } from 'colore-js/fn';

chartColors(5, { background: '#0a0a0a' });
// ['#70b2fb', '#bcdb29', '#ec5480', '#2feac3', '#f86927']
#70b2fb
#bcdb29
#ec5480
#2feac3
#f86927

Check the displayed colors against the real chart background. Labels or line patterns also help readers distinguish nearby series.