Skip to content

Instantly share code, notes, and snippets.

@simevidas
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simevidas/cd4c5af291dec41a816f to your computer and use it in GitHub Desktop.
Save simevidas/cd4c5af291dec41a816f to your computer and use it in GitHub Desktop.
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