/ Published in: JavaScript
Transform a color given in hex to RGB. An optional opacity argument also enables RBGA.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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 + ')'; };