Skip to content

Instantly share code, notes, and snippets.

@anthify
Last active April 8, 2019 09:38
Show Gist options
  • Save anthify/467aefe304e8368f47c249124c677600 to your computer and use it in GitHub Desktop.
Save anthify/467aefe304e8368f47c249124c677600 to your computer and use it in GitHub Desktop.
Converts RGB/RGBA string to object
// pass a rgb/rgba string value into this function i.e. rgb(0,0,0)
// and it will return an object of { r: 0, g: 0, b: 0, a: 1 }
const parseRGBString = str => {
const vals = str.substring(str.indexOf('(') + 1, str.length - 1).split(', ');
const colors = {
r: parseInt(vals[0], 10),
g: parseInt(vals[1], 10),
b: parseInt(vals[2], 10),
a: parseFloat(vals[3])
};
return colors;
};
export default parseRGBString;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment