You can get rid of the color table :)

20 views
Skip to first unread message

Dean Edwards

unread,
Oct 22, 2009, 10:45:35 AM10/22/09
to google-excanvas
Hi Erik,

I noticed that there is now a large color table in the excanvas source:

http://code.google.com/p/explorercanvas/source/browse/trunk/excanvas.js#sl_svn69_260

You actually don't need this. :)

The function below will convert *any* color value to hex:

function toHex(color) {
var body = createPopup().document.body,
range = body.createTextRange();
body.style.color = color.toLowerCase();
var value = range.queryCommandValue("ForeColor").toString(16);
return "#" + "000000".slice(0, -value.length) + value;
};

-dean

Dean Edwards

unread,
Oct 22, 2009, 11:03:47 AM10/22/09
to google-excanvas

Actually, that should be:

function toHex(color) {
var body = createPopup().document.body,
range = body.createTextRange();

body.style.color = color;
var value = range.queryCommandValue("ForeColor");
value = ((value & 0x0000ff) << 16) | (value & 0x00ff00) | ((value &
0xff0000) >>> 16);
value = value.toString(16);
return "#000000".slice(0, 7 - value.length) + value;
};


I blogged it here in the end:

http://dean.edwards.name/weblog/2009/10/convert-any-colour-value-to-hex-in-msie/

-dean

Erik Arvidsson

unread,
Oct 22, 2009, 12:02:22 PM10/22/09
to google-...@googlegroups.com
Thanks, I forgot about that one.
--
erik

Bradley Neuberg

unread,
Oct 23, 2009, 2:23:17 AM10/23/09
to google-...@googlegroups.com
Nice function. Very clever.
Reply all
Reply to author
Forward
0 new messages