TinyColor
JavaScript color parsing
Features
Usage
var t = tinycolor("red");
t.toHex()
t.toHexString()
t.toHex8()
t.toHex8String()
t.toRgb()
t.toRgbString()
t.toPercentageRgb()
t.toPercentageRgbString()
t.toHsv()
t.toHsvString()
t.toHsl()
t.toHslString()
t.toName()
tinycolor
may also be included as a node module like so:
npm install tinycolor2
Then it can be used:
var tinycolor = require("./tinycolor");
Accepted String Input
The string parsing is very permissive. It is meant to make typing a color as input as easy as possible. All commas, percentages, parenthesis are optional, and most input allow either 0-1, 0%-100%, or 0-n (where n is either 100, 255, or 360 depending on the value).
HSL and HSV both require either 0%-100% or 0-1. RGB requires either 0-255 or 0%-100%. If you call tinycolor.fromRatio, any input can also accept 0-1
Here are some examples of string input:
red
#fff
fff
#ffffff
ffffff
#ffffffff
ffffffff
rgb(255, 0, 0)
rgb 255 0 0
hsl(0, 100, 50)
hsl(0, 100%, 50%)
hsl 0 100 50
hsl 0 100% 50%
hsv(0, 100%, 100%)
hsv(0, 100, 100)
hsv 0 100% 100%
hsv 0 100 100
Accepted Object Input
If you are calling this from code, you may want to use object input. Here are examples of the different types of accepted object inputs:
{ r: 255, g: 0, b: 0 }
{ r: 255, g: 0, b: 0, a: .5 }
{ r: 1, g: 0, b: 0 }
{ h: 0, s: 100, l: 50 }
{ h: 0, s: 100, v: 100 }
See index.html in the project for a demo.