Seperating $AARRGGBB into r,g,b
BlitzMax Forums/BlitzMax Beginners Area/Seperating $AARRGGBB into r,g,b
| ||
Agh! Only a short time away from coding and it seems I've forgotten everything I knew. I'm trying to seperate a 32 bit integer from a read pixel into seperate rgb. I thought it went something like this. r=(rgb shr 16) and $FF g=(rgb shr 8) and $FF b=(rgb) and $FF Which gets me the r,g,b from the integer represting $AARRGGBB But no, r,g,b just returns $FF everytime, regardless of the contents of rgb. I thought using AND meant it only return the last 8 bits, but apparently not. I'm at a bit of a loss it seems. Any help, or a refresher on bitwise operations would be greatly appreciated. |
| ||
and and or are only logical operators in BM You need to use & and | in BM as well as ~ for not as it is mentioned in the help |
| ||
Tryr=(rgb & $FF0000) shr 16 g=(rgb & $FF00) shr 8 b=(rgb & $FF)Notice the & instead of "And", this is important. |
| ||
. beaten to it |
| ||
A tad confusingly, ~ is the bitwise XOR operator too.A ~ B = A XOR B _ ~A = A |
| ||
Ah righto. Thanks for the help folks |
| ||
Here's a fun one.Local MyColor:Int = $FFAA00BB Local bytes:Byte Ptr = Varptr MyColor Local alpha:Int = bytes[0] Local red:Int = bytes[1] Local green:Int = bytes[2] Local blue:Int = bytes[3] |
| ||
More Fun and games :)RGB=$FF00AABB Type RGBQUAD Field Blue:Byte Field Green:Byte Field Red:Byte Field Alpha:Byte End Type RGBq:RGBQUAD=New RGBQUAD MemCopy Varptr(RGBq.Blue), Varptr(RGB), 4 Print RGBq.Alpha Print RGBq.Red Print RGBq.Green Print RGBq.Blue |
| ||
Type RGBQuad Field Blue:Byte Field Green:Byte Field Red:Byte Field Alpha:Byte ' And for more and more fun: Function FromInt:RGBQuad( i:Int ) Local r:RGBQuad = New RGBQuad MemCopy ( VarPtr r.Alpha, Varptr i, 4 ) Return r End Function Method ToInt:Int( i:Int ) Return ( Int Ptr( VarPtr Alpha ) )[0] End Method End Type Took Snarty's code a bit further for the hell of it. Encapsulation is fun. |