Question

11111 111111 11111  > 16 bit
  R      G     B

how can I get the R/G/B values?

R = x/2048 (2048 are the 6+5 G+B  11 bit)
G = x-R*2048/32 (32 are the B )

is there a simple explanation of this? why should the decimal value be divided by others bit to get the single group value?

Était-ce utile?

La solution

If you want to get R you have to move 11 bits on the right so that G and B disappear. So

R=x>>11

but every shift of 1 bit is equivalent to a division by 2 so the expression above is equivalent to

R=X/(2^11)=x/2048

And so you get the R.

To get the G, according to the formula, you can first remove the R, which is the R you obtained previously but shift back of 11 bits (or multiplied by 2048). Now done this you have a number like:

111111 11111

You shift left of 5 bits (or divide for 32) and you get the result.

Note that in practice you can do this easier doing this:

B=(x and 0x1F)
G=((x>>5) and 0x3F)
R=((x>>11) and 0x1F)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top