color
JavaScript library for immutable color conversion and manipulation with support for CSS color strings.
var color = Color('#7743CE').alpha(0.5).lighten(0.5);
console.log(color.hsl().string()); // 'hsla(262, 59%, 81%, 0.5)'
console.log(color.cmyk().round().array()); // [ 16, 25, 0, 8, 0.5 ]
console.log(color.ansi256().object()); // { ansi256: 183, alpha: 0.5 }Install
$ npm install colorUsage
var Color = require('color');Constructors
var color = Color('rgb(255, 255, 255)')
var color = Color({r: 255, g: 255, b: 255})
var color = Color.rgb(255, 255, 255)
var color = Color.rgb([255, 255, 255])Set the values for individual channels with alpha, red, green, blue, hue, saturationl (hsl), saturationv (hsv), lightness, whiteness, blackness, cyan, magenta, yellow, black
String constructors are handled by color-string
Getters
Convert a color to a different space (hsl(), cmyk(), etc.).
Get a hash of the color value. Reflects the color's current model (see above).
Get an array of the values with array(). Reflects the color's current model (see above).
Get the rgb number value.
Get the hex value.
Get the value for an individual channel.
CSS Strings
Calling .string() with a number rounds the numbers to that decimal place. It defaults to 1.
Luminosity
The WCAG luminosity of the color. 0 is black, 1 is white.
The WCAG contrast ratio to another color, from 1 (same color) to 21 (contrast b/w white and black).
Get whether the color is "light" or "dark", useful for deciding text color.
Manipulation
Propers
The API was inspired by color-js. Manipulation functions by CSS tools like Sass, LESS, and Stylus.
Last updated
Was this helpful?