color
color
is a JavaScript library for color conversion and manipulation with support for CSS color strings.
var color = Color("#7743CE");
color.alpha(0.5).lighten(0.5);
console.log(color.hslString());
Install
browser
Download the latest color.js. The Color
object is exported.
node
For node with npm:
npm install color
And use with var Color = require("color")
API
Setters
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])
Pass any valid CSS color string into Color()
or a hash of values. Also load in color values with rgb()
, hsl()
, hsv()
, hwb()
, and cmyk()
.
color.red(120)
Set the values for individual channels with alpha
, red
, green
, blue
, hue
, saturation
(hsl), saturationv
(hsv), lightness
, whiteness
, blackness
, cyan
, magenta
, yellow
, black
Getters
color.rgb()
Get a hash of the rgb values with rgb()
, similarly for hsl()
, hsv()
, and cmyk()
color.rgbArray()
Get an array of the values with rgbArray()
, hslArray()
, hsvArray()
, and cmykArray()
.
color.red()
Get the value for an individual channel.
CSS Strings
color.hslString()
Different CSS String formats for the color are on hexString
, rgbString
, percentString
, hslString
, hwbString
, and keyword
(undefined if it's not a keyword color). "rgba"
and "hsla"
are used if the current alpha value of the color isn't 1
.
Luminosity
color.luminosity();
The WCAG luminosity of the color. 0 is black, 1 is white.
color.contrast(Color("blue"))
The WCAG contrast ratio to another color, from 1 (same color) to 21 (contrast b/w white and black).
color.light();
color.dark();
Get whether the color is "light" or "dark", useful for deciding text color.
Manipulation
color.negate()
color.lighten(0.5)
color.darken(0.5)
color.saturate(0.5)
color.desaturate(0.5)
color.greyscale()
color.whiten(0.5)
color.blacken(0.5)
color.clearer(0.5)
color.opaquer(0.5)
color.rotate(180)
color.rotate(-90)
color.mix(Color("yellow"))
color.mix(Color("yellow"), 0.3)
color.green(100).greyscale().lighten(0.6)
Clone
You can can create a copy of an existing color object using clone()
:
color.clone()
And more to come...
Propers
The API was inspired by color-js. Manipulation functions by CSS tools like Sass, LESS, and Stylus.