简体   繁体   English

将 rgba 颜色转换为十六进制时接收到不正确的 alpha 通道

[英]Receive incorrect alpha channel when convert rgba color to hex

In order to convert rgba color with alpha channel to hex I use the code为了将带有 alpha 通道的 rgba 颜色转换为十六进制,我使用代码

 let rChannel = 255; let gChannel = 255; let bChannel = 255; let aChannel = 0.7; console.log('#'+(256 + parseFloat(rChannel)).toString(16).substr(1) + ((1 << 24) + (parseFloat(gChannel) << 16) | (parseFloat(bChannel) << 8) | aChannel).toString(16).substr(1)); // #ffffff00, but expect #ffffffb3

It's given from the answer by the link https://stackoverflow.com/a/9766195/3208225它由链接https://stackoverflow.com/a/9766195/3208225的答案给出

But it returns incorrect alpha channel in hex (00 instead of b3).但它以十六进制返回不正确的 alpha 通道(00 而不是 b3)。

How to fix?怎么修?

Using a bitwise |使用按位| (or) operator on a floating-point value does not make sense. (or) 浮点值运算符没有意义。 You'll have to use ~~(achannel * 255) to get an integer value.您必须使用~~(achannel * 255)来获得 integer 值。

Depending on the range of your 0... 1 achannel values, you might want to use 256 instead of 255 if the value can never be 1.根据您的 0...1 achannel 值的范围,如果值永远不能为 1,您可能希望使用 256 而不是 255。

edit — now that I think about it, you don't need the ~~ explicit integer conversion, because the |编辑——现在我想起来了,你不需要~~明确的 integer 转换,因为| will do that anyway, so simply (achannel * 256) will work so long as the fractional value will always be less than 1;无论如何都会这样做,所以只要分数值始终小于 1,简单的(achannel * 256)就可以工作; otherwise 255.否则255。

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

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