Skip to content

Instantly share code, notes, and snippets.

@nealfennimore
Created March 15, 2019 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nealfennimore/7b295baf7c79eb42106421c1c772f72f to your computer and use it in GitHub Desktop.
Save nealfennimore/7b295baf7c79eb42106421c1c772f72f to your computer and use it in GitHub Desktop.
RGBtoRGBA
// https://stackoverflow.com/questions/6672374/convert-rgb-to-rgba-over-white
function RGBtoRGBA(r, g, b){
if((g == null) && (typeof r === 'string')){
var hex = r.replace(/^\s*#|\s*$/g, '');
if(hex.length === 3){
hex = hex.replace(/(.)/g, '$1$1');
}
r = parseInt(hex.substr(0, 2), 16);
g = parseInt(hex.substr(2, 2), 16);
b = parseInt(hex.substr(4, 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