Chroma.js

3.2.0 · active · verified Sun Apr 19

Chroma.js is a lightweight, zero-dependency JavaScript library for comprehensive color conversions and generating sophisticated color scales. Currently at v3.2.0, the library sees a consistent, though not rapid, release cadence, addressing bugs and introducing new features periodically. Its core strength lies in supporting a wide array of color models including RGB, HSL, HSV, Lab, Lch, Oklab, and Oklch, alongside modern CSS color syntax. Key differentiators include its powerful color scale generation capabilities, allowing for custom domains, quantile-based scaling, logarithmic scales, and improved interpolation modes like Lab/Lch for visually smoother transitions, distinguishing it from simpler color utilities. It also provides access to Color Brewer palettes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic color creation, manipulation (darken, brighten), and the creation of linear and custom-domain color scales with different interpolation modes, including modern CSS color handling.

import chroma from 'chroma-js';

// --- Basic color manipulation ---
const initialColor = '#D4F880';
const colorInstance = chroma(initialColor);

// Darken and get hex string
const darkenedHex = colorInstance.darken().hex();
console.log(`Original: ${initialColor}, Darkened: ${darkenedHex}`); // Expected: #D4F880, #a1c550

// Lighten and get RGB array
const lightenedRgb = colorInstance.brighten(0.5).rgb();
console.log(`Lightened RGB: [${lightenedRgb.join(', ')}]`);

// --- Working with color scales ---
const scale1 = chroma.scale(['white', 'red']);
const intermediateColor1 = scale1(0.5).hex();
console.log(`Linear scale (white to red) at 0.5: ${intermediateColor1}`); // Expected: #FF7F7F

// Custom domain and mode
const myValues = [0, 10, 50, 100];
const customScale = chroma.scale(['lightyellow', 'navy'])
                          .domain(myValues, 5, 'quantiles')
                          .mode('lab'); // Lab interpolation for smoother transitions

// Get a color from the custom scale
const colorFromCustomScale = customScale(25).hex();
console.log(`Custom scale (quantiles, lab mode) at 25: ${colorFromCustomScale}`);

// Transparent color parsing (v3.1.0+)
const transparentColor = chroma('transparent').css();
console.log(`Transparent color parsed: ${transparentColor}`); // Expected: rgba(0,0,0,0) (or similar modern CSS format)`

view raw JSON →