RGBA Hex from RGB Hex
I want to make from color开发者_如何学JAVA like 0xFF0000
(RGB), an RGBA color with FF alpha, like 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. it means it moves exactly one byte left so for example 0x12345678
will change to 0x34567800
. The top two digits won't fit in 32 bits after shifting, so they are removed. Every thing else shifts left and then new zeroes are added in the low position.
The next operator is a bitwise or
. it's an or applied on every bit so 0x34567800 | 0xFF
will result in 0x345678FF
(thanks to @Gajet for explanation)
精彩评论