Skip to content

Instantly share code, notes, and snippets.

@pulges
Last active August 29, 2015 14:02
Show Gist options
  • Save pulges/13fdd12137922ff8ac61 to your computer and use it in GitHub Desktop.
Save pulges/13fdd12137922ff8ac61 to your computer and use it in GitHub Desktop.
Parse rgb(a) string to numbers
var color = "rgba(12, 23 , 34 , 0.2)";
var getRGBA = function(colorStr) {
if (!colorStr || typeof colorStr !== 'string') {
return;
}
var arr = colorStr.match(/(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*,?\s*([\d\.]+)?\s*)/);
if (arr) {
return {
r: +arr[2],
g: +arr[3],
b: +arr[4],
a: (arr[5]) ? +arr[5] : 1
};
}
};
var parsedColor = getRGBA(color);
// Object {r: 12, g: 23, b: 34, a: 0.2}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment