Last active
August 29, 2015 14:02
-
-
Save pulges/13fdd12137922ff8ac61 to your computer and use it in GitHub Desktop.
Parse rgb(a) string to numbers
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
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