简体   繁体   English

用javascript填充样式

[英]fill style in javascript

I have to fill an image with rgba component. 我必须用rgba组件填充图像。 It is working with function something.fillStyle = " rgba ("+r+","+g+","+b+","+a+")"; 它正在使用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 rgb . 但是我想使用something.fillStyle = value ,其中value是以alpha rgb形式十六进制表示的颜色值。 When value is #ffff00 (yellow) it is fine but when I use a value with alpha ( #ffffff00 ) it is always black. value#ffff00 (黄色)时可以,但是当我使用带alpha值( #ffffff00 )时,它始终是黑色。 Is there any way to do this? 有什么办法吗?

This is not possible, see http://www.w3.org/TR/css3-color/#numerical . 这是不可能的,请参阅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 . 十六进制颜色值不允许设置“ alpha”值,只有rgbahsla才可以hsla

It is possible, if you convert the hex to rgb and then add your alpha value. 如果将十六进制转换为rgb,然后添加您的alpha值,则可能的。 Also, you can set the globalAlpha which will apply even if the rest of the colors in the function are using hex colors. 另外,您可以设置globalAlpha,即使函数中的其他颜色使用十六进制颜色,该参数也将适用。

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

take a look at the hextorbg function here . 看看这里的hextorbg函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM