Piotrek
Piotrek

Reputation: 11211

Can I get r, g and b from fillStyle of my canvas?

It is possible to get rgb color from context.fillStyle (and put 'red', 'green' and 'blue' to variables)? How?

Upvotes: 2

Views: 451

Answers (1)

andrewmu
andrewmu

Reputation: 14534

When set with a simple HTML hexadecimal color value, the fillStyle property exposes a string of the form: #RRGGBB. You can extract the colors like so:

var r = parseInt(context.fillStyle.substring(1,3), 16);
var g = parseInt(context.fillStyle.substring(3,5), 16);
var b = parseInt(context.fillStyle.substring(5), 16);

The colors values have the range 0 to 255.

If you've set a colour value with alpha (such as "rgba(127, 63, 255, 0.5)") they are returned like that and you'll have to do a bit more work.

Upvotes: 3

Related Questions