Install color-convert via npm
masterTo use color-convert in your JavaScript or Node.js project, install it using npm:
npm install color-convertrepository·master·Indexed 21 days ago
https://github.com/qix-/color-convertA utility library for converting colors between various color spaces including RGB, HSL, HSV, HWB, XYZ, LAB, CMYK, and others. It features a property-access API (convert.[from].[to]), automatic routing through intermediate models, and support for both rounded and raw floating-point results.
To use color-convert in your JavaScript or Node.js project, install it using npm:
npm install color-convertThe API follows a property-access pattern: convert.[from].[to](args).
Supported color models include rgb, hsl, hsv, hwb, cmyk, ansi, ansi16, hex, and CSS keywords.
.raw to the function call..channels property indicating the number of expected input channels (excluding alpha).hex or keyword).XYZ -> RGB -> CMYK). Note that multi-step conversions may result in precision loss.import convert from 'color-convert';
// Basic conversion
convert.rgb.hsl(140, 200, 100); // [96, 48, 59]
convert.keyword.rgb('blue'); // [0, 0, 255]
// Accessing channel counts
const rgbChannels = convert.rgb.channels; // 3
// Using .raw for unrounded results
convert.hex.lab.raw('DEADBF'); // [ 75.56213190997677, 20.653827952644754, -2.290532499330533 ]
// Passing arguments as an array
convert.rgb.hex([123, 45, 67]); // '7B2D43'Conversions in color-convert rely on agreed-upon 'full-scale' values for each channel. When working with these color spaces, keep these scales in mind:
| Color Space | Channel | Full-scale value |
|---|---|---|
| rgb | r, g, b | 255 |
| hsl | h, s, l | 360, 100, 100 |
| hsv | h, s, v | 360, 100, 100 |
| hwb | h, w, b | 360, 100, 100 |
| xyz | x, y, z | 94, 99, 108 |
| lab | l, a, b | 100, (-86 to 98), (-108 to 94) |
| lch | l, c, h | 100, 133, 360 |
| oklab | l, a, b | 100, (-23 to 28), (-31 to 20) |
| oklch | l, c, h | 100, 32, 360 |
| cmyk | c, m, y, k | 100, 100, 100, 100 |
| hex | hex | 0xffffff |
| keyword | name | any key from color-name |
| apple | 0, 1, 2 | 65535, 65535, 65535 |
| gray | gray | 100 |
The color-convert library provides a hierarchical API for converting colors between different color spaces. The API is structured as convert.[fromModel].[toModel](args).
fromModel: The source color space (e.g., rgb, hsl, hsv, hwb, xyz, lab).toModel: The target color space.args: An array of numbers representing the color components (e.g., [r, g, b] or [h, s, l]).By default, conversion methods return rounded integer values. If you require high-precision floating-point results, use the .raw property on the conversion function.
import convert from 'color-convert';
// Standard usage (returns rounded integers)
const rgb = convert.hsl.rgb([360, 100, 50]);
// High-precision usage (returns raw floating-point values)
const rgbRaw = convert.hsl.rgb.raw([360, 100, 50]);Standard conversion methods in color-convert automatically round the resulting color components to the nearest integer. To bypass rounding and obtain the exact floating-point values, call the .raw property attached to the conversion function.
Example: convert.[fromModel].[toModel].raw(args)
import convert from 'color-convert';
// Rounded result
const rounded = convert.rgb.hsl([255, 0, 0]);
// Precise floating-point result
const precise = convert.rgb.hsl.raw([255, 0, 0]);Each source color model object (e.g., convert.rgb, convert.hsl) contains metadata describing its structure. You can access these properties to understand the expected input/output format:
channels: An array representing the number of components in the color model.labels: An array of strings representing the names of the color components (e.g., ['r', 'g', 'b']).import convert from 'color-convert';
console.log(convert.rgb.channels); // e.g., [3]
console.log(convert.rgb.labels); // e.g., ['r', 'g', 'b']