Created
October 7, 2015 11:58
-
-
Save u01jmg3/8465fdf1d908882f8c25 to your computer and use it in GitHub Desktop.
Convert RGB to RGBA over white
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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