Chroma.js
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
-
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an ES module context or a Node.js project configured for ESM after Chroma.js moved to ES modules in v2.5.0.fixChange `const chroma = require('chroma-js');` to `import chroma from 'chroma-js';`. Ensure your `package.json` has `"type": "module"` if in a pure ESM Node.js environment. -
Uncaught TypeError: chroma.scale is not a function
cause Incorrect import of the `chroma` object, possibly due to a default export issue or CJS/ESM mismatch, leading to `chroma` being `undefined` or not the expected object.fixVerify `import chroma from 'chroma-js';` is correct. If using CommonJS, ensure your setup correctly transpiles or handles ESM. Check that `chroma-js` is installed correctly. -
color.css() output is 'oklab(...)' but I expected 'rgb(...)'
cause After v3.0.0, `color.css()` defaults to modern CSS color space outputs (e.g., `oklab()`, `lch()`) instead of legacy `rgb()` or `rgba()`.fixAdapt your code to parse or expect modern CSS color strings. If legacy `rgb()/rgba()` is strictly needed, you might need to format the color components manually (e.g., `chroma(color).rgb().join(',')` for RGB values).
Warnings
- breaking Beginning with v2.5.0, Chroma.js was refactored to primarily use ES modules. This change impacts how the library is imported, especially in Node.js environments that expect CommonJS modules.
- breaking As of v3.0.0, the `color.css()` method no longer returns legacy CSS color formats (e.g., `rgb(255, 255, 0)`). Instead, it outputs modern CSS color spaces like `lab()`, `lch()`, `oklab()`, `oklch()` by default.
- gotcha In v3.2.0, the `scale.domain` function was updated to return all scaled positions rather than only `[min, max]`. This fix might subtly change the interpolation or how you interact with the domain if your code relied on the previous, potentially limited, return value.
- gotcha Users sometimes perceive the project as inactive due to periods with fewer commits. However, the author clarifies that the library is stable and maintained, with bugs fixed and new features introduced when appropriate, rather than constant cosmetic changes.
Install
-
npm install chroma-js -
yarn add chroma-js -
pnpm add chroma-js
Imports
- chroma
const chroma = require('chroma-js');import chroma from 'chroma-js';
- ChromaStatic
import type { ChromaStatic } from 'chroma-js'; - Color
import type { Color } from 'chroma-js';
Quickstart
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)`