Skip to content

Instantly share code, notes, and snippets.

@vieron
Created September 21, 2013 15:43
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 vieron/6651704 to your computer and use it in GitHub Desktop.
Save vieron/6651704 to your computer and use it in GitHub Desktop.
rgba to rgb + a
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"
};
}
@loopmode
Copy link

Two issues

  • color = color.replace(' ', ''); only replaces the first space. It should be color.replace(/ /g, ''); or color.replace(/\s/g, ''); instead.
  • Because Array.prototype.splice is a mutating method , there is a bug in line 16: rgba[5] is undefined and should be rgba[2] instead

Otherwise thanks for the function :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment