So I created this function to extract an RGB array from a style attribute, but everything inside me is screaming and telling me there is a better way of doing it.
My style attribute looks like this:
style="fill: rgb(0, 0, 0);"
so I wrote a function that looks like this:
var _extractRGBfromStyle = function (styles) {
// Create some variables
var length = styles.length - 1,
index = styles.indexOf(';');
// If we only have one style applied to the element
if (length === index) {
// Get the first part
var rgb = styles.split('(');
// If we have more than one result
if (rgb.length > 1) {
// Get our last part
rgb = rgb[1].split(')');
// If we have more than one result
if (rgb.length > 1) {
// Do one last split and return it
return rgb[0].split(',');
}
}
}
// Fallback
return [0, 0, 0];
};
I really don't like the nested splits. Is there a better way of doing this?