Skip to content

Instantly share code, notes, and snippets.

@u01jmg3
Created October 7, 2015 11:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save u01jmg3/8465fdf1d908882f8c25 to your computer and use it in GitHub Desktop.
Save u01jmg3/8465fdf1d908882f8c25 to your computer and use it in GitHub Desktop.
Convert RGB to RGBA over white
function RGBtoRGBA(r, g, b){
if((g==void 0) && (typeof r == 'string')){
r = r.replace(/^\s*#|\s*$/g, '');
if(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