简体   繁体   English

来自 RGB Hex 的 RGBA Hex

[英]RGBA Hex from RGB Hex

I want to make from color like 0xFF0000 (RGB), an RGBA color with FF alpha, like 0xFF0000FF .我想用0xFF0000 (RGB) 之类的颜色制作,一种具有 FF alpha 的 RGBA 颜色,例如0xFF0000FF It will be something with bits, I think.我认为这将是一些带有位的东西。

Thanks in advance, Kacper提前致谢, 卡珀

uint32_t rgb=0xFF0000;
uint32_t rgba = (rgb << 8) | 0xFF;

Explanation: (rgb << 8) shifts the rgb value by 8 bits to the left.说明: (rgb << 8)将 rgb 值向左移动 8 位。 it means it moves exactly one byte left so for example 0x12345678 will change to 0x34567800 .这意味着它恰好向左移动一个字节,因此例如0x12345678将更改为0x34567800 The top two digits won't fit in 32 bits after shifting, so they are removed.前两位数字在移位后不适合 32 位,因此它们被删除。 Every thing else shifts left and then new zeroes are added in the low position.其他所有东西都向左移动,然后在低 position 中添加新的零。

The next operator is a bitwise or .下一个运算符是按位or it's an or applied on every bit so 0x34567800 | 0xFF它是一个或应用于每一位,所以0x34567800 | 0xFF 0x34567800 | 0xFF will result in 0x345678FF 0x34567800 | 0xFF将导致0x345678FF

(thanks to @Gajet for explanation) (感谢@Gajet 的解释)

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

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