Created
September 21, 2013 15:43
-
-
Save vieron/6651704 to your computer and use it in GitHub Desktop.
rgba to rgb + a
This file contains hidden or 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 rgbaToSVGAttrs(color) { | |
var regex, rgba; | |
color = color.replace(' ', ''); | |
if (color.indexOf('rgba(') !== 0) { | |
return color; | |
} | |
regex = /(.*?)rgba\((\d+),(\d+),(\d+),([0-9]+\.[0-9]+|\d)\)/; | |
rgba = regex.exec(color); | |
if (!rgba) { return color; } | |
return { | |
color: 'rgb(' + rgba.splice(2, 3).join(',') + ')', | |
opacity: rgba[5] || "1" | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Two issues
color = color.replace(' ', '');
only replaces the first space. It should becolor.replace(/ /g, '');
orcolor.replace(/\s/g, '');
instead.Array.prototype.splice
is a mutating method , there is a bug in line 16:rgba[5]
is undefined and should bergba[2]
insteadOtherwise thanks for the function :)