Skip to content

Instantly share code, notes, and snippets.

@cmarkle27
Created February 25, 2014 13:54
Show Gist options
  • Save cmarkle27/9209181 to your computer and use it in GitHub Desktop.
Save cmarkle27/9209181 to your computer and use it in GitHub Desktop.
RBGtoRGBA
function RGBtoRGBA(red, green, blue) {
if ((green === void 0) && (typeof red === 'string')) {
red = red.replace(/^\s*#|\s*$/g, '');
if (red.length == 3) {
red = red.replace(/(.)/g, '$1$1');
}
green = parseInt(red.substr(2, 2), 16);
blue = parseInt(red.substr(4, 2), 16);
red = parseInt(red.substr(0, 2), 16);
}
var min, alpha = (255 - (min = Math.min(red, green, blue))) / 255;
return {
red: red = 0|( red - min ) / alpha,
green: green = 0|( green - min ) / alpha,
blue: blue = 0|( blue - min ) / alpha,
alpha: alpha = (0|1000*alpha)/1000,
rgba: 'rgba(' + red + ', ' + green + ', ' + blue + ', ' + alpha + ')'
};
}
RGBtoRGBA(204, 153, 102)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment