Last active
April 8, 2019 09:38
-
-
Save anthify/467aefe304e8368f47c249124c677600 to your computer and use it in GitHub Desktop.
Converts RGB/RGBA string to object
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
// 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