A utility class for working with colors in JavaScript. It supports easy object creation, manipulation, and conversion between color formats.
Documentation incomplete, more to come
Basic Example:
// Make a Color object
var myColor = Color( '#bada55' );
// Output HSL CSS
myColor.toCSS( 'hsl' );
// "hsl( 74, 64%, 59% )"
All construction methods can be called explicitly, but Color
will usually intelligently know the value you're passing it.
hex
: an RRGGBB hexadecimal color, with or without a leading hash (#).
var hex = '#bada55';
var myColor = Color().fromHex( hex );
// or, just:
var myColorShorthand = Color( hex );
rgb
: an object containing r
, g
, and b
integer properties. r
, g
, and b
must be integers within the range [0, 256]
.
var rgb = {
r: 0,
g: 127,
b: 256
};
var myRgbColor = Color().fromRgb( rgb );
var myRgbShorthand = Color( rgb );
hsl
: an object containing h
, 's', and l
integer properties. h
should be in the range [0,360]
1 and s
and l
must be in the range [0,100]
.
var hsl = {
h: 210,
s: 80,
l: 23
};
var myHslColor = Color().fromHsl( hsl );
var myHslShorthand = Color( hsl );
hsv
: an object containing h
, 's', and v
integer properties. h
should be in the range [0,360]
1 and s
and v
must be in the range [0,100]
.
var hsv = {
h: 21,
s: 33,
v: 80
};
var myHsvColor = Color().fromHsv( hsv );
var myHsvShorthand = Color( hsv );
css
: (string) Any valid CSS color
var css = 'rgb( 0, 127, 256 )';
var color1 = Color().fromCSS( css );
var color1Shorthand = Color( css );
var color2 = Color( 'hsla( 210, 80%, 23%, 0.4 )' );
var color3 = Color( '#bada55' );
Each will be parsed and passed to the appropriate from*
methods.
var c = Color( 'rgb(12, 34, 145)' );
c.toString();
// "#0c2291"
Note that there is no toHex
method, as this is essentially it.
var c = Color( 'rgb(12, 34, 145)' );
c.toRgb();
// { r: 12, g: 34, b: 145 }
var c = Color( 'rgb(12, 34, 145)' );
c.toHsl();
// { h: 230, s: 85, l: 31 }
Note that the toHsl
method only outputs integers for its members, leading it to be not 100% faithful when performing conversions. This is a pragmatic decision made for the sake of elegance in the API - perfectionists can surely find other libraries. :)
var c = Color( 'rgb(12, 34, 145)' );
c.toHsv();
// { h: 230, s: 92, v: 57 }
var c = Color( 'rgb(12, 34, 145)' );
c.toCSS();
// '#0c2291'
c.toCSS( 'rgb' );
// 'rgb( 12, 34, 145 )'
c.toCSS( 'rgba' );
- top-level node.js exports compatibility.
- now using Grunt 0.4.x
- remove redundant
getGrayscaleContrastingColor
method - remove deprecated
incrementLightness
method
- massively improved CSS string parsing
- use internal
._error
method for error cases
- preserve hue/saturation in HSV flows
- allow construction from an HSV object
- refactor
.h()
,.s()
, and.l()
methods for DRYness - add
.a()
get/set method for alpha channel - fix missing % signs in hsl CSS output
- add a
.clone()
method - add HSV functionality
- return blank string on error in
.toString()
- improved error handling
- construct without
new
keyword
- initial public release