read/write pixel fast work out colours?
Blitz3D Forums/Blitz3D Beginners Area/read/write pixel fast work out colours?
| ||
Hi guys, I'm a bit confused with how to work with the colours obtained through readpixelfast and writepixelfast. I know when it reads a pixel, it turns the colour into a single integer value which contains the alpha, r, g, and b, but how do you look at those values individually? If I wanted to know what the red was, and had the integer that is returned by readpixelfast, how do I separate the red from the integer value, for example? |
| ||
Hey :) Thatīs easy sorted out like this: argb = readpixelfast(x,y) alpha=(argb Shr 24) and 255 ; This shifts so that all bits that we're interested in (which is ; alpha here) becomes the lowest 8 bits. After this we chop away all other bits, using AND 255. red=(argb Shr 16) and 255 ; Same here but red needs another amount of shifting. green=(argb Shr 8) and 255 ; Same here. blue=argb and 255 ; blue are the lowest 8 bits already, so it just needs some chopping. And if you want to write it back somewhere sometime you just do this: argb = (alpha Shl 24) or (red Shl 16) or (green Shl 8) or blue ; We prepare argb to become 4 integers ; and shift each integer the right amount first, then we merge them into one integer using OR. |
| ||
Thanks heaps Kimor! It was driving me a bit crazy. |
| ||
You can find more info about the color and alpha arrangement in the integer used by these commands in the post I made at: http://www.blitzbasic.com/b3ddocs/command.php?name=ReadPixel&ref=2d_cat You can find some code which shows the byte ordering for each color component at: http://www.blitzbasic.com/codearcs/codearcs.php?code=2999 |