Pigment
A color information, conversion and manipulation library with support for various color models!
Installation
$ npm install pigment
Usage
To use pigment in your projects, you need to require the node module first.
var Color = ;
If you don't need all the functionality, you can use the basic package.
var Color = ;
The basic package supports the name
, rgb
, hex
and hsl
color models.
It also includes the following methods for color manipulation,
luminance()
- luminance value of the colordarkness()
- darkness value of the colornegate()
- negative color of the colorgreyscale()
/grayscale()
- greyscale color from the colormix(add, weight)
- mix with another color, whereadd
is a valid color of any supported models,weight
(0-1, default: 0.5) is the amount to addfadein(ratio)
- increase alpha value byratio
(0-1)fadeout(ratio)
- decrease alpha value byratio
(0-1)rotate(degrees)
- rotate the hue bydegrees
(0-360)saturate(ratio)
- increase the saturation byratio
(0-1)desaturate(ratio)
- decrease the saturation byratio
(0-1)lighten(ratio)
- increase the luma byratio
(0-1)darken(ratio)
- decrease the luma byratio
(0-1)
The default package supports all the models supported by the basic package as well as the hsv
, hwb
, xyz
, lab
and cmyk
color models;
You can use the full package if you want color schemes and color blindness support.
var Color = ;
To be able to perform any operations on a color, create a new color object. To convert to a specific color model, just prefix the model name by to
.
var c = "#f06860"; /* Get the color in hex, rgb, hsl etc. */c; // #f06860c; // rgb(240, 104, 96)c; // hsla(3, 83%, 66%, 1) /* Get luminance or darkness of the color */c; // 0.29270422282503833c; // 0.4362666666666666 /* Perform operations on the color */c; // rgba(236, 186, 96, 0.99)
Apart from color conversion and manipulation, pigment is also able to parse a chunk of text to find out any colors.
var colors = Color for var i = 0 l = colorslength; i < l; i++ console; // #ff0000, #0000ff
Generating a random color is also easy,
Color; // #a68a4b
Core API
All the functionality in pigment is achieved with models. The addModel
method can be used to add a new color model to pigment.
Color;
The model object must consist 3 properties,
match
- A function or a regex which is used to detect if the color is represented in the specific model representation.format
- A function which formats the color string into model's representation.convert
- A function which converts the color object into model's representation.frommodel
- A function which converts the model's representation into an object withred
,green
andblue
properties, and an optionalalpha
property.tomodel
- A function which converts the color to the model representation.
There is also an optional init
method which is called when instantiating a color object.
Any dependencies on other color models can be specified with a depends
property with the model names in an array.
Have a look at the models
directory in the repository for examples.