Skip to content

Instantly share code, notes, and snippets.

@stickupkid
Created July 13, 2011 09:29
Show Gist options
  • Save stickupkid/1079992 to your computer and use it in GitHub Desktop.
Save stickupkid/1079992 to your computer and use it in GitHub Desktop.
RGBtoRGBA
function RGBtoRGBA(r, g, b){
(g==void 0) && (typeof r == 'string') &&
(r = r.replace(/^\s*#|\s*$/g, ''),
(r.length == 3) && (r = r.replace(/(.)/g, '$1$1')),
g = parseInt(r.substr(2, 2), 16),
b = parseInt(r.substr(4, 2), 16),
r = parseInt(r.substr(0, 2), 16));
var min, a = ( 255 - (min = Math.min(r, g, b)) ) / 255;
return {
r : r = 0|( r - min ) / a,
g : g = 0|( g - min ) / a,
b : b = 0|( b - min ) / a,
a : a = (0|1000*a)/1000,
rgba : 'rgba(' + r + ', ' + g + ', ' + b + ', ' + a + ')'
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment