argb help

BlitzMax Forums/BlitzMax Programming/argb help

rdodson41(Posted 2005) [#1]
I have 3 byte variables containing my R G and B values for a pixel, how do I put them together to form a 4 byte argb value?


sswift(Posted 2005) [#2]
Magic!

RGB = (R Shl 16) Or (G Shl 8) Or B


rdodson41(Posted 2005) [#3]
Ha thanks! Some of this stuff does work like magic! But is there anyway to do the reverse as well? Take an argb value and find the rgb values from it?


sswift(Posted 2005) [#4]
Nope!


No wait, I mean...


Yes!
R = (RGB Shr 16) And 255 
G = (RGB Shr 8 ) And 255
B = (RGB       ) And 255



The way the first one works is it moves the green byte over to the left by 8 bits, and the red byte over to the left by 16 bits, and adds all three together. This produces an integer which has four bytes in it, ARGB.

The way the second one works is it rotates the red byte over to the right by 16 bits, which puts it in the first byte position, and then 255 is used to mask it to get rid of any bits that might be in the first byte of the integer which contains the alpha byte. 255, when stored in a longint is 00000000 00000000 00000000 11111111 in binary.

If R is in the second byte... and the alpha byte is 255..,
11111111 00001111 00000000 00000000

And we rotate the int 16 bits to the right...
00000000 00000000 11111111 00001111

Then if we AND the integer with 255, leaving only those bits on which are in 255 AND the integer...

00000000 00000000 11111111 00001111
AND
00000000 00000000 00000000 11111111
=
00000000 00000000 00000000 00001111

Then as you can see, the final number is the byte that represents the R value.


rdodson41(Posted 2005) [#5]
Okay thanks sswift


Clyde(Posted 2005) [#6]
Also use Shl / Shr 24 for the Aplha value.


rdodson41(Posted 2005) [#7]
Yeah but I havent been using it.
[200th post!]


Clyde(Posted 2005) [#8]
On some occasions when using WritePixel you'll notice you wont see anything, so you may want to include the alpha if this happens; I usually set the alpha to 255 (seems to work). Converting to a different TPixmap format may cut this out from being needed.