I’m using Javascript and Canvas to make a painting app and was using strings in this format to designate chosen colors:
"rgb(255,0,0)"
Because the canvas context fillStyle property takes in strings of that format.
However, I now need to obtain individual components from this string and was wondering if there was a way to do it without messy string manipulation. Possibly some built in way to convert that string to a sort of color object and then access its r, g, and b components?
Thanks.
NOTE – We’re all on board with the regex ate my brains and kicked my dog attitude, but the regex version just seems the better method. My opinion. Check it out.
Non-regex method:
http://jsfiddle.net/userdude/Fg9Ba/
Outputs:
Or… A really simple regex:
EDIT: Ooops, had an
iin the regex for some reason.http://jsfiddle.net/userdude/Fg9Ba/2