Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
color parsing API
/*
* Tab Atkins's color parsing API proposal
* Source: http://www.w3.org/blog/CSS/2014/07/10/minutes-telecon-219/
*/
var color;
// creating color objects from RGBA components
color = RGBAColor(50, 100, 150, .5);
color = RGBAColor({ r: 50, g: 100, b: 150, a: .5 });
// creating color objects from HSLA components
color = RGBAColor.fromHSLA(100, .3, .4, .5);
color = RGBAColor.fromHSLA({ h: 100, s: .3, l: .4, a: .5 });
// creating color objects from HEX values
// Q: Not sure how HEX values are different from RGBA values
// properties/methods of color objects
color.r
color.g
color.b
color.a
color.asHSL() // returns { h, s, l, a } object
color.asHex() // returns { r, g, b, a } object
color.asName()
color.toString()
// parsing any CSS color
color = CSS.parseColor('#FAA');
color = CSS.parseColor('rgb(100,150,200)');
color = CSS.parseColor('orangered');
// color objects can be assigned directly to CSS properties
document.body.style.backgroundColor = color;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.