| Number of watchers on Github | 30 |
| Number of open issues | 0 |
| Main language | JavaScript |
| Open pull requests | 0+ |
| Closed pull requests | 0+ |
| Last commit | almost 4 years ago |
| Repo Created | over 7 years ago |
| Repo Last Updated | about 2 years ago |
| Size | 53 KB |
| Organization / Author | automattic |
| Contributors | 1 |
| Page Updated | 2018-03-10 |
| Do you use Color.js? Leave a review! | |
| View Color.js activity | |
| View on github | |
| Fresh, new opensource launches 🚀🚀🚀 | |
|
Trendy new open source projects in your inbox!
View examples
|
|
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][h] 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 );
[h]: As hue is measured in circular degrees, anything out of range will simply be plotted as a mode of 360.
hsv: an object containing h, 's', and v integer properties. h should be in the range [0,360][h] 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' );
getGrayscaleContrastingColor methodincrementLightness method._error method for error cases.h(), .s(), and .l() methods for DRYness.a() get/set method for alpha channel.clone() method.toString()
new keyword