Return to Snippet

Revision: 35231
at November 4, 2010 18:35 by madr


Initial Code
function hex2rgb(hex, opacity) {
  var rgb = hex.replace('#', '').match(/(.{2})/g);

  var i = 3;
  while (i--) {
    rgb[i] = parseInt(rgb[i], 16);
  }

  if (typeof opacity == 'undefined') {
    return 'rgb(' + rgb.join(', ') + ')';
  }

  return 'rgba(' + rgb.join(', ') + ', ' + opacity + ')';
};

Initial URL
http://madr.se/blog/151

Initial Description
Transform a color given in hex to RGB. An optional opacity argument also enables RBGA.

Initial Title
Hex to RGB or RGBA transluder

Initial Tags
javascript

Initial Language
JavaScript