Tuesday, January 5, 2010
Converting an RGB Color To Hex With JavaScript
function colorToHex(color) {Now, I can compare colors like so.
if (color.substr(0, 1) === '#') {
return color;
}
var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
var red = parseInt(digits[2]);
var green = parseInt(digits[3]);
var blue = parseInt(digits[4]);
var rgb = blue | (green << 8) | (red << 16);
return digits[1] + '#' + rgb.toString(16);
};
equals(colorToHex('rgb(120, 120, 240)'), '#7878f0');
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment