JavaScript : Cross browser colour calculations

Just what the heck is “Cross browser colour calculations”?  Well, few weeks ago, I had to implement the function to convert any legal CSS colour value (rgb, named colours, etc) to the hex equivalent. And it has to work in all the browsers, of course.

I knew of implementations that use IE specific queryCommandValue(). For this and other MSIE internals, I suggest Geoff Chapels excellent site.
So, at least this is sorted, but what about other browsers? After a short googling session it was clear that getComputedStyle(), was made to do the job. Here is the final solution:

 

 

I use the “BUTTON” element since it has createTextRange() which I need for IE queryCommandValue(). All of his works quite nicely and quickly.

For browsers that need newborns to be attached to the document, to work, I attach to the “HEAD”, which, it seems, does not provoke a re-flow and thus does not slow down the dbj.color.toHex() function.
For dbj.browser.support.css_on_newborns, please see : https://dbj.org/does-your-browser-support-css-on-newborns/