or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

color-models.mdconversion-functions.mdindex.mdrouting-system.md
tile.json

index.mddocs/

Color Convert

Color Convert is a comprehensive color conversion library for JavaScript and Node.js that enables conversion between multiple color models including RGB, HSL, HSV, HWB, CMYK, XYZ, LAB, LCH, OKLAB, OKLCH, ANSI, HEX strings, and CSS keywords. It offers both rounded and raw (unrounded) conversion results through a clean API, supports automatic routing for multi-step conversions between color spaces that don't have direct conversion paths, and includes TypeScript definitions for type safety.

Package Information

  • Package Name: color-convert
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install color-convert

Core Imports

import convert from 'color-convert';
import route from 'color-convert/route.js';

For CommonJS:

const convert = require('color-convert');
const route = require('color-convert/route.js');

Basic Usage

import convert from 'color-convert';

// Basic color conversions
convert.rgb.hsl(140, 200, 100);             // [96, 48, 59]
convert.keyword.rgb('blue');                // [0, 0, 255]
convert.hex.lab('DEADBF');                  // [76, 21, -2]
convert.rgb.oklab([153, 102, 255]);          // [64, 9, -20]
convert.rgb.oklch([153, 102, 255]);          // [64, 22, 295]

// Array input format also supported
convert.rgb.hsl([140, 200, 100]);           // [96, 48, 59]

// Raw (unrounded) conversions for precision
convert.hex.lab.raw('DEADBF');              // [75.56213190997677, ...]

// Access color model metadata
const rgbChannels = convert.rgb.channels;    // 3
const cmykChannels = convert.cmyk.channels;  // 4
const rgbLabels = convert.rgb.labels;        // 'rgb'

// Use route function for advanced routing
import route from 'color-convert/route.js';
const rgbRoutes = route('rgb');
const lchResult = rgbRoutes.lch([255, 0, 0]); // [53, 104, 40] (multi-step conversion)

Architecture

Color Convert is built around several key components:

  • Convert Object: Main export containing nested color model namespaces
  • Color Models: 17 supported color spaces (RGB, HSL, HSV, HWB, CMYK, XYZ, LAB, LCH, OKLAB, OKLCH, HEX, Keyword, ANSI16, ANSI256, HCG, Apple, Gray)
  • Conversion Functions: Direct conversion functions between color models with both rounded and raw variants
  • Automatic Routing: Breadth-first search algorithm for multi-step conversions between color spaces
  • Input Flexibility: Support for both spread arguments and array inputs
  • Type Safety: Complete TypeScript definitions for all color models and conversion functions

Capabilities

Color Model Conversion Functions

Core conversion functionality between 17 different color models. Each conversion returns rounded values by default, with raw variants available for precision.

interface Convert {
  rgb: ColorModelNamespace<RGB>;
  hsl: ColorModelNamespace<HSL>;
  hsv: ColorModelNamespace<HSV>;
  hwb: ColorModelNamespace<HWB>;
  cmyk: ColorModelNamespace<CMYK>;
  xyz: ColorModelNamespace<XYZ>;
  lab: ColorModelNamespace<LAB>;
  lch: ColorModelNamespace<LCH>;
  oklab: ColorModelNamespace<OKLAB>;
  oklch: ColorModelNamespace<OKLCH>;
  hex: ColorModelNamespace<HEX>;
  keyword: ColorModelNamespace<Keyword>;
  ansi16: ColorModelNamespace<ANSI16>;
  ansi256: ColorModelNamespace<ANSI256>;
  hcg: ColorModelNamespace<HCG>;
  apple: ColorModelNamespace<Apple>;
  gray: ColorModelNamespace<Gray>;
}

interface ColorModelNamespace<T> {
  readonly channels: number;
  readonly labels: string | string[];
  [toModel: string]: ConversionFunction<T>;
}

interface ConversionFunction<T> {
  (...args: any[]): any;
  raw: (...args: any[]) => any;
  conversion?: string[];
}

Conversion Functions

Color Model Types and Metadata

Type definitions and metadata for all supported color models, including channel counts and label information.

type RGB = [r: number, g: number, b: number];
type HSL = [h: number, s: number, l: number];
type HSV = [h: number, s: number, v: number];
type HWB = [h: number, w: number, b: number];
type CMYK = [c: number, m: number, y: number, k: number];
type XYZ = [x: number, y: number, z: number];
type LAB = [l: number, a: number, b: number];
type LCH = [l: number, c: number, h: number];
type OKLAB = [l: number, a: number, b: number];
type OKLCH = [l: number, c: number, h: number];
type HCG = [h: number, c: number, g: number];
type Apple = [r16: number, g16: number, b16: number];
type Gray = [gray: number];
type ANSI16 = number;
type ANSI256 = number;
type Keyword = string;
type HEX = string;

Color Models

Automatic Routing System

Intelligent routing system that automatically creates conversion paths between color models that don't have direct conversions, enabling conversion between any two supported color spaces.

function route(fromModel: string): {
  [toModel: string]: ConversionFunction & { conversion: string[] };
};

Routing System