I\'m trying to get the individual values of a rgb string. I\'ve been getting close, but I\'m just hitting a wall. I\'m looking to do something like this:
v
Typescript solution:
interface RGB {
r: number,
g: number,
b: number
}
const str2rgb = (color: string): RGB => {
const rgbSubset = color
.split('(')
.pop()
?.split(')')
.shift()
?.split(',')
.slice(0, 3)
.map(Number);
if (rgbSubset && rgbSubset.length === 3) {
const [r, g, b] = rgbSubset;
return {r, g, b};
}
throw new Error(`Invalid color = ${color}`);
};