Install chroma.js
mainYou can install chroma-js via npm, pnpm, or yarn for Node.js environments.
npm install chroma-js
# or
pnpm add chroma-js
# or
yarn add chroma-jsnpm install chroma-js
# pnpm add chroma-js
# yarn add chroma-jsrepository·main·Indexed 27 days ago
https://github.com/gka/chroma.jsA zero-dependency JavaScript library for color conversions and the creation of sophisticated color scales. It provides a chainable API to manipulate colors, support for multiple color spaces (including Lab, Lch, and OKLCH), and tools for computing color contrast (WCAG and APCA), distance, and data class breaks.
You can install chroma-js via npm, pnpm, or yarn for Node.js environments.
npm install chroma-js
# or
pnpm add chroma-js
# or
yarn add chroma-jsnpm install chroma-js
# pnpm add chroma-js
# yarn add chroma-jsImport the chroma package to begin manipulating colors and creating scales.
import chroma from "chroma-js";Depending on your environment, use the following import patterns:
Standard ESM Import:
import chroma from 'chroma-js';Tree-shaking (importing specific parts):
To reduce bundle size, you can import directly from chroma-js/src/*.
import deltaE from 'chroma-js/src/utils/deltaE.js';Observable Notebooks:
import { chroma } from "@gka/chroma-js";Chroma.js allows you to read, manipulate, and output colors in a chainable API. It also supports generating color scales for data visualization.
Simple color manipulation:
chroma('pink').darken().saturate(2).hex()Generating a color scale:
Use chroma.scale() to create a scale between colors, specify a color mode (e.g., 'lch'), and generate a specific number of colors.
chroma.scale(['#fafa6e', '#2A4858'])
.mode('lch')
.colors(6)chroma object or individual named exports from the library. The default export contains the core color functionality and a collection of utility methods, generators, and color scales.index-light.js entrypoint for a smaller build of chroma.js. This version includes core color conversion (CSS, Hex, HSL, Lab, Oklab, RGB), basic operators (alpha, darken, mix, set, shade), specific interpolators (LRGB, Oklab), and the mix generator..domain(). This method supports passing values and specifying a scale type, such as 'quantiles' or 'log' (logarithmic).You can initiate colors using various formats (like hex strings) and apply transformations such as .darken() before exporting the result using methods like .hex().
chroma('#D4F880').darken().hex(); // #a1c550Use chroma.scale() to create a color scale from an array of colors. You can then call the scale as a function with a value between 0 and 1 to retrieve an interpolated color.
scale = chroma.scale(['white', 'red']);
scale(0.5).hex(); // #FF7F7FTo improve visual results, you can change the interpolation mode of a scale. For example, using .mode('lab') provides better interpolation than standard RGB.
chroma.scale(['white', 'red']).mode('lab');Mix the current color with a target color using color.mix(targetcolor, ratio=0.5, mode='lrgb'). The ratio is a value between 0 and 1.
chroma('hotpink').mix('blue');
chroma('hotpink').mix('blue', 0.25);
chroma('hotpink').mix('blue', 0.75, 'lab');Convert a chroma object into various string or array formats:
color.hex(mode='auto|rgb|rgba|argb'): Returns a hex string. Default is 'auto' (includes alpha if < 1). Use 'rgb' to exclude alpha.color.css(colorSpace): Returns a CSS string (e.g., rgb(), hsl()). Supported spaces: rgb, hsl, lab, lch, oklab, oklch.color.rgb(round=true): Returns [r, g, b] as 0-255 numbers. Set round=false for floats.color.rgba(round=true): Returns [r, g, b, a] as 0-255 numbers.color.hsl(), color.hsv(), color.hsi(), color.lab(), color.lch(), color.oklab(), color.oklch(): Return arrays of components in the specified color space.color.gl(): Returns RGB components in the [0..1] range.chroma('orange').hex();
chroma('orange').alpha(0.5).hex('rgb');
chroma('teal').css('hsl');
chroma('orange').rgb(false);
chroma('orange').rgba();
chroma('skyblue').oklch();
chroma('33cc00').gl();