user894554
user894554

Reputation: 268

fill style in javascript

I have to fill an image with r g b a component. It is working with function something.fillStyle = " rgba ("+r+","+g+","+b+","+a+")"; but i want to use something.fillStyle = value where value is the color value in hex of the form alpha r g b. When value is #ffff00 (yellow) it is fine but when I use a value with alpha (#ffffff00) it is always black. Is there any way to do this?

Upvotes: 0

Views: 896

Answers (2)

lbutlr
lbutlr

Reputation: 424

It is possible, if you convert the hex to rgb and then add your alpha value. Also, you can set the globalAlpha which will apply even if the rest of the colors in the function are using hex colors.

if (myAlpha) {
    ctx.globalAlpha = myAlpha;
}

take a look at the hextorbg function here.

Upvotes: 0

Wladimir Palant
Wladimir Palant

Reputation: 57651

This is not possible, see http://www.w3.org/TR/css3-color/#numerical. Hex color values don't allow setting the "alpha" value, that's only possible with rgba and hsla.

Upvotes: 1

Related Questions