Colord: Lightweight JavaScript Color Manipulation Library

Category: Color , Javascript , Recommended | November 12, 2026
Authoromgovich
Last UpdateNovember 12, 2026
LicenseMIT
Views0 views
Colord: Lightweight JavaScript Color Manipulation Library

Colord is a lightweight JavaScript color library for parsing, converting, and adjusting CSS colors.

It provides an immutable, dependency-free API to handle HEX, RGB, HSL, and HSV values, with optional plugins for OKLCH, palette generation, and contrast checks.

Features

  • Parses hexadecimal, RGB, and HSL strings, plus RGB, HSL, and HSV objects.
  • Converts colors to HEX, RGB, HSL, and HSV with built-in methods.
  • Adjusts alpha, hue, lightness, and saturation through chainable operations.
  • Adds OKLab, OKLCH, Lab, LCH, HWB, CMYK, and XYZ through optional plugins.
  • Generates color harmonies, mixes, tints, shades, and tones.
  • Checks text contrast and readability against WCAG 2 thresholds.

How To Use It

Installation

Install Colord with npm:

npm install colord

For browser use, load the versioned ES module in the document:

<script type="module">
  import { colord } from "https://cdn.jsdelivr.net/npm/[email protected]/index.mjs";
  console.log(colord("#ff6347").toHslString());
</script>

Basic Usage

import { colord } from "colord";
const color = colord("#ff6347");
console.log(color.toRgbString()); // "rgb(255, 99, 71)"
console.log(color.toHsl());
console.log(color.darken(0.2).toHex());
console.log(color.alpha(0.5).toRgbString());

Validate a Color Before Using It

An invalid input produces an instance whose fallback color is black. Check isValid() before saving a user-entered value or applying it to a theme:

import { colord } from "colord";
function normalizeColor(input) {
  const color = colord(input);
  return color.isValid() ? color.toHex() : null;
}
console.log(normalizeColor("rgb(255 99 71)")); // "#ff6347"
console.log(normalizeColor("not-a-color"));  // null

Set Theme Colors and Choose Contrasting Text

The a11y plugin calculates contrast ratios from RGB channels. Use the final composited colors if a text or background layer is translucent.

<button class="primary-action">Save changes</button>
import { colord, extend } from "colord";
import a11yPlugin from "colord/plugins/a11y";
extend([a11yPlugin]);
const brand = colord("#0f766e");
const foreground = colord("#fff").contrast(brand) >=
  colord("#000").contrast(brand) ? "#fff" : "#000";
document.documentElement.style.setProperty("--brand", brand.toHex());
document.documentElement.style.setProperty("--on-brand", foreground);
.primary-action {
  background: var(--brand);
  color: var(--on-brand);
}

Convert OKLCH Colors

Enable oklch to parse CSS oklch() strings and convert RGB colors to OKLCH. Object input needs ok: true to distinguish OKLCH from CIE LCH, which uses the same channel names.

import { colord, extend } from "colord";
import oklchPlugin from "colord/plugins/oklch";
extend([oklchPlugin]);
console.log(colord("oklch(0.5 0.2 240)").toHex()); // "#0069c7"
console.log(colord("#008080").toOklchString());
console.log(colord({ l: 0.5431, c: 0.09271, h: 194.77, ok: true }).toHex()); // "#008080"

Generate a Palette

The mix plugin interpolates colors in CIE Lab by default. Choose "rgb" as the color space when you need RGB-channel interpolation.

import { colord, extend } from "colord";
import mixPlugin from "colord/plugins/mix";
import harmoniesPlugin from "colord/plugins/harmonies";
extend([mixPlugin, harmoniesPlugin]);
const base = colord("#ff0000");
const tints = base.tints(5).map((color) => color.toHex());
const complementary = base.harmonies("complementary")
  .map((color) => color.toHex());
console.log(tints);
console.log(complementary); // ["#ff0000", "#00ffff"]

Color Inputs and Supported Models

Core parsing accepts hexadecimal strings (3, 4, 6, or 8 digits), RGB and HSL strings, and RGB, HSL, or HSV objects. Additional models require the corresponding plugins.

API / FormatDescription
HEXCore. Supports #rgb, #rgba, #rrggbb, and #rrggbbaa.
RGBCore. RGB/RGBA objects and CSS rgb()/rgba() strings, including space-separated syntax, percentages, and scientific notation.
HSLCore. HSL/HSLA objects and CSS hsl()/hsla() strings, including CSS angle units.
HSVCore. HSV/HSVA objects.
CSS keywordsnames plugin. Parses named colors and transparent.
HWBhwb plugin. Object and hwb() string input.
CMYKcmyk plugin. Objects and device-cmyk() strings.
Lablab plugin. CIE Lab objects.
LCHlch plugin. CIE LCH objects and lch() strings.
OKLaboklab plugin. oklab() strings or objects containing ok: true.
OKLCHoklch plugin. oklch() strings or objects containing ok: true.
XYZxyz plugin. CIE XYZ objects.

Public API Reference

Core Functions

FunctionDescription
colord(input)Parses a supported color input and returns a Colord instance. Call isValid() to detect failed parsing.
getFormat(input)Returns the parsed input format, such as "hex", "rgb", or "hsl"; returns undefined for unknown input.
extend(plugins)Registers one or more plugin extensions, adding their parsers and public methods.
random()Returns a Colord instance containing a random color.

Core Conversion Methods

MethodDescription
toHex()Returns a lowercase hexadecimal string. Includes an alpha byte when alpha is less than 1.
toRgb()Returns { r, g, b, a } with channel values and alpha.
toRgbString()Returns a CSS rgb() string, or rgba() when alpha is less than 1.
toHsl()Returns { h, s, l, a }.
toHslString()Returns a CSS hsl() or hsla() string.
toHsv()Returns { h, s, v, a }.

Core Manipulation Methods

Method / OptionDescription
alpha()Returns the current alpha value from 0 to 1.
alpha(value)Returns a new instance with the specified alpha.
hue()Returns the current hue in degrees.
hue(value)Returns a new instance with the hue set to the supplied degrees.
rotate(amount = 15)Rotates the HSL hue by the supplied degrees.
invert()Inverts the RGB color.
saturate(amount = 0.1, options?)Increases HSL saturation. Set { relative: true } to scale the current value.
desaturate(amount = 0.1, options?)Decreases HSL saturation. Supports { relative: true }.
grayscale()Returns a grayscale color with the source lightness.
lighten(amount = 0.1, options?)Increases HSL lightness. Supports { relative: true }.
darken(amount = 0.1, options?)Decreases HSL lightness. Supports { relative: true }.
options.relativefalse (default) adds or removes a fraction of the full 0 to 100% range. true scales the existing saturation or lightness value by the given fraction.

Core Color Analysis

MethodDescription
isValid()Returns whether parsing succeeded. Invalid inputs otherwise use black as their fallback color.
isEqual(color2)Returns true when the supplied value represents the same color.
brightness()Returns a brightness score from 0 to 1.
isLight()Returns true when brightness is at least 0.5.
isDark()Returns true when brightness is below 0.5.

Optional Plugins and Their Methods

Import a plugin from colord/plugins/<name> and register it with extend([plugin]).

PluginDescription
a11yluminance() returns WCAG relative luminance. contrast(color2 = "#FFF") returns a contrast ratio. isReadable(color2 = "#FFF", options?) returns a WCAG 2 text-contrast result.
cmyktoCmyk() returns CMYK channel values; toCmykString() produces device-cmyk().
harmoniesharmonies(type = "complementary") returns an array of Colord instances for a selected hue harmony.
hwbtoHwb() returns HWB channels; toHwbString() produces a CSS hwb() string.
labtoLab() returns CIE Lab channels. delta(color2 = "#FFF") returns normalized CIEDE2000 color difference.
lchtoLch() returns CIE LCH channels; toLchString() returns a CSS lch() string.
minifyminify(options?) finds the shortest permitted color string representation.
mixmix(color2, ratio = 0.5, space = "lab") returns a mixed color. tints(count = 5, space = "lab"), shades(count = 5, space = "lab"), and tones(count = 5, space = "lab") return arrays of Colord instances, including the source color. They mix toward white, black, and gray, respectively.
namesParses CSS keyword colors. toName(options?) returns a corresponding keyword or undefined.
oklabtoOklab() and toOklabString() convert to OKLab object and CSS string formats.
oklchtoOklch() and toOklchString() convert to OKLCH object and CSS string formats.
xyztoXyz() returns CIE XYZ channel values.

Plugin Options and Accepted Values

ParameterDescription
isReadable(): level"AA" (default) or "AAA".
isReadable(): size"normal" (default) or "large". WCAG 2 thresholds: AA normal 4.5, AA large 3, AAA normal 7, AAA large 4.5.
toName(): closestfalse (default) returns exact keyword matches; true selects a closest available named color.
mix() / tints() / shades() / tones(): space"lab" (default) or "rgb". ratio is the contribution of the second color from 0 to 1.
harmonies(): type"analogous", "complementary" (default), "double-split-complementary", "rectangle", "split-complementary", "tetradic", or "triadic".

Color Minification Options

Register the minify plugin to shorten CSS color strings. Keyword output also requires the names plugin.

import { colord, extend } from "colord";
import minifyPlugin from "colord/plugins/minify";
extend([minifyPlugin]);
console.log(colord("#112233").minify()); // "#123"
console.log(colord("#aabbcc66").minify({ alphaHex: true }));
OptionDescription
hextrue by default. Considers 3- and 6-digit hexadecimal strings.
alphaHexfalse by default. Enables 4- and 8-digit hexadecimal strings with alpha.
rgbtrue by default. Enables rgb() and rgba().
hsltrue by default. Enables hsl() and hsla().
namefalse by default. Enables CSS color keywords when the names plugin is registered.
transparentfalse by default. Enables the transparent keyword for fully transparent black.

TypeScript Types

Colord includes TypeScript declarations for its core API, plugins, and color objects.

Color modelDescription
RGBRgbColor, RgbaColor.
HSL / HSV / HWBHslColor, HslaColor, HsvColor, HsvaColor, HwbColor, HwbaColor.
Lab / LCHLabColor, LabaColor, LchColor, LchaColor.
OKLab / OKLCHOklabColor, OklabaColor, OklchColor, OklchaColor.
XYZXyzColor, XyzaColor.
import { colord, extend, type RgbColor, type OklchaColor } from "colord";
import oklchPlugin from "colord/plugins/oklch";
extend([oklchPlugin]);
const rgb: RgbColor = { r: 255, g: 99, b: 71 };
const perceptual: OklchaColor = colord(rgb).toOklch();

Alternatives and Related Resources

FAQs

Why does colord(“tomato”).isValid() return false?

Register the names plugin before parsing CSS color keywords: extend([namesPlugin]).

Does Colord parse CSS color(display-p3 ...) values?

No. Colord supports the input formats listed above, including OKLCH and Lab through plugins. Convert Display P3 colors to a supported format before passing them to Colord.

Should I use isLight() or contrast() for text color?

isLight() uses a brightness threshold. The a11y plugin’s contrast() calculates the contrast ratio for the actual text and background colors.

You Might Be Interested In:


Leave a Reply