What is color-convert?
The color-convert npm package is a library for converting colors between different color models such as RGB, HSL, HSV, HEX, and more. It provides functions for one-to-one, one-to-many, and many-to-one conversions.
What are color-convert's main functionalities?
RGB to HEX conversion
Converts an RGB color value to its HEX representation.
const convert = require('color-convert');
const hexColor = convert.rgb.hex(255, 255, 255);
HEX to HSL conversion
Converts a HEX color value to its HSL representation.
const convert = require('color-convert');
const hslColor = convert.hex.hsl('FFFFFF');
HSL to HSV conversion
Converts an HSL color value to its HSV representation.
const convert = require('color-convert');
const hsvColor = convert.hsl.hsv(360, 100, 100);
Keyword to RGB conversion
Converts a CSS keyword color value to its RGB representation.
const convert = require('color-convert');
const rgbColor = convert.keyword.rgb('blue');
ANSI 16 to HEX conversion
Converts an ANSI 16 color code to its HEX representation.
const convert = require('color-convert');
const hexColor = convert.ansi16.hex(90);
Other packages similar to color-convert
chroma-js
chroma-js is a powerful library for all kinds of color conversions and manipulations. It supports a wide range of color models, similar to color-convert, but also includes functions for color scale generation, interpolation, and more complex color operations.
tinycolor2
tinycolor2 is a small, fast library for color manipulation and conversion in JavaScript. It provides many of the same conversion capabilities as color-convert but also includes additional features for checking color properties, such as brightness and readability.
color
color is a JavaScript library for immutable color conversion and manipulation with support for CSS color strings. It offers a chainable API and is similar to color-convert in terms of conversion features but also includes manipulation functions like lighten, darken, saturate, and desaturate.
color-convert
Color-convert is a color conversion library for JavaScript and node.
It converts all ways between rgb
, hsl
, hsv
, hwb
, cmyk
, ansi
, ansi16
and CSS keywords:
var converter = require("color-convert")();
converter.rgb(140, 200, 100).hsl()
converter.keyword("blue").rgb()
Install
$ npm install color-convert
API
Color-convert exports a converter object with getter/setter methods for each color space. It caches conversions:
var converter = require("color-convert")();
converter.rgb(140, 200, 100).hsl()
converter.rgb([140, 200, 100])
Plain functions
Get direct conversion functions with no fancy objects:
require("color-convert").rgb2hsl([140, 200, 100]);
Unrounded
To get the unrounded conversion, append Raw
to the function name:
convert.rgb2hslRaw([140, 200, 100]);
Hash
There's also a hash of the conversion functions keyed first by the "from" color space, then by the "to" color space:
convert["hsl"]["hsv"]([160, 0, 20]) == convert.hsl2hsv([160, 0, 20])
Other spaces
There are some conversions from rgb (sRGB) to XYZ and LAB too, available as rgb2xyz()
, rgb2lab()
, xyz2rgb()
, and xyz2lab()
.
Contribute
Please fork, add conversions, figure out color profile stuff for XYZ, LAB, etc. This is meant to be a basic library that can be used by other libraries to wrap color calculations in some cool way.