【javascript】Convert any colour value to hex in MSIE

The following function will convert any colour value (rgb, named colours, etc) to the hex equivalent in MSIE:

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;

};

For other browsers you can use getComputedStyle() so that is already a solved problem.