JavaScript Easy Color Parser for Node JS
Converts colors to formats hex, rgb, rgba, hsl, hsla and cmyk.
![downloads](https://img.shields.io/npm/dm/easy-color.svg)
Fork of Adrian's project
Supported color formats
- HEX
- SHORT HEX
- RGB
- RGBA
- HSL
- HSLA
- CMYK
Installation
npm install easy-color
How to use
Import the module and create an instance of the constructor by placing the color you want to convert
You can put in any supported format
Example:
var Color = require('easy-color');
var parser = new Color("#00AAFF");
parser.toHEX();
parser.toRGB();
parser.toRGBA();
parser.toHSL();
parser.toHSLA();
parser.toCMYK();
To convert the values of a color separately use:
var parser = new Color.fromRGB(0, 170, 255);
var parser = new Color.fromRGB([0, 170, 255]);
var parser = new Color.fromRGB({ r: 0, g: 170, b: 255 });
var parser = new Color.fromRGBA(0, 170, 255, 1);
var parser = new Color.fromRGBA([0, 170, 255, 1]);
var parser = new Color.fromRGBA({ r: 0, g: 170, b: 255, a: 1 });
var parser = new Color.fromHSL(200, 100, 50);
var parser = new Color.fromHSL([200, 100, 50]);
var parser = new Color.fromHSL({ h: 200, s: 100, l: 50 });
var parser = new Color.fromHSLA(200, 100, 50, 1);
var parser = new Color.fromHSLA([200, 100, 50, 1]);
var parser = new Color.fromHSLA({ h: 200, s: 100, l: 50, a: 1 });
var parser = new Color.fromCMYK(100, 33, 0, 0);
var parser = new Color.fromCMYK([100, 33, 0, 0]);
var parser = new Color.fromCMYK({ c: 100, m: 33, y: 0, k: 0 });
--
You can also get the name of the color format that you have set to make it easier in some cases
parser.colorType();
--
So you can also convert a color to a format from the format name
parser.to("RGBA")
--
CSS Color Table
You can pick up the CSS color table used to convert the color name to HEX, eg: white -> #FFFFFF
Read about CSS Color Table
To get the array of this table use:
var table = parser.CSSColorTable;
With this table you can convert colors just by name too
For example:
var parser = new EasyColorParser("aqua");
parser.toHex();
--
Values
To get the separate values for each format use:
var rgb = parser.rgb,
hsl = parser.hsl,
cmyk = parser.cmyk,
alpha = parser.alpha;
var red = rgb.r;
var green = rgb.g;
var blue = rgb.b;
var hue = hsl.h;
var saturation = hsl.s;
var lightness = hsl.l;
var cyan = cmyk.c;
var magenta = cmyk.m;
var yellow = cmyk.y;
var key = cmyk.k;
Links