Colore JS

Search documentation

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

Accessible chart colors for dashboards

Four lines in a signup chart need to stand apart from each other and from the plot. On a pale card, a light teal stroke can be hard to spot. On a dark card, the same palette does a different job. Generate colors for each background, ranked by how distinct they stay for readers with red-green color blindness, then give each series a line style and label that stay the same across themes.

Direct
Search
Referral
Email
Direct
Search
Referral
Email
npm install colore-js
yarn add colore-js

Generate colors for the plot background

Tell categoricalColors how many series you have and what sits behind the marks. Use the card's color if the chart is drawn on a card, even when the rest of the page has a different background.

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

console.log(categoricalColors(4, { background: '#f8fafc' }).join(' '));
// #1781d4 #bf2e0f #a7207b #f35a86

These colors are for categories like Direct, Search, Referral and Email. If color encodes an ordered value such as revenue or temperature, use a scale with an ordered progression instead.

Check a stroke against the card

Use contrastRatio to check the pair the reader sees. In this example, the pink line has 3.03:1 contrast against the card. That clears the 3:1 threshold for a graphical object that readers need to understand the data. Lowering the stroke opacity can change the result. Check against a neighboring fill, too, if the lines are drawn over one. WCAG non-text contrast

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

console.log(contrastRatio('#f35a86', '#f8fafc').toFixed(2));
// 3.03

Build the series styles for both modes

This script emits the stroke color, dash pattern and contrast for each series on both plot backgrounds. Keep the id stable in your chart data and use it to look up the style. Give the chart's visible legend the same label and line pattern as the plotted series.

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

const series = [
  { id: 'direct', label: 'Direct', dash: 'none' },
  { id: 'search', label: 'Search', dash: '8 3' },
  { id: 'referral', label: 'Referral', dash: '2 3' },
  { id: 'email', label: 'Email', dash: '8 3 2 3' },
];

for (const [mode, background] of Object.entries({ light: '#f8fafc', dark: '#0f172a' })) {
  const colors = categoricalColors(series.length, { background });
  series.forEach(({ id, label, dash }, index) => {
    const stroke = colors[index];
    const ratio = contrastRatio(stroke, background).toFixed(2);
    console.log(`${mode} ${id} (${label}): ${stroke}, dash ${dash}, ${ratio}:1`);
  });
}
light direct (Direct): #1781d4, dash none, 3.91:1
light search (Search): #bf2e0f, dash 8 3, 5.56:1
light referral (Referral): #a7207b, dash 2 3, 6.40:1
light email (Email): #f35a86, dash 8 3 2 3, 3.03:1
dark direct (Direct): #70b2fb, dash none, 8.03:1
dark search (Search): #bcdb29, dash 8 3, 11.31:1
dark referral (Referral): #ec5480, dash 2 3, 5.23:1
dark email (Email): #2feac3, dash 8 3 2 3, 11.63:1

For SVG, use the color as stroke and the dash pattern as stroke-dasharray. If the legend is far from the plot, add labels at the ends of the lines where there is room. Keep a readable table or equivalent text when readers need the exact values. WCAG use of color

Check the chart at its smallest rendered size as well. Short dashes need enough room to be recognizable, and labels need a clear background.

What Colore checks for you

Colore checks the candidate colors against the supplied background. It also ranks them by how far apart they look in normal vision and in protanopia and deuteranopia simulations. Your chart still needs visible labels and a way to identify each series when lines cross. The patterns stay useful even if a display shows the chart in grayscale.