Code archives/Graphics/Int to RGBA and back

This code has been declared by its author to be Public Domain code.

Download source code

Int to RGBA and back by *2012
This converts an int to its red, green, blue and alpha components and back again.

IntToRGBA requires you pass the red, green, blue and alpha variables as parameters of the function. RGBAtoInt just converts it back.

There are loads of examples on here of it but some of them dont work as expected or take ARGB values. I always work with RGBA :)
Function IntToRGBA( Value:Int, r:Byte Var, G:Byte Var, B:Byte Var, A:Byte Var )
	a = ( Value Shr 24 ) & $FF
	r = ( Value Shr 16) & $FF
	g = ( Value Shr 8) & $FF
	b = Value & $FF
End Function

Function RGBAtoInt:Int( red:Byte, Green:Byte, Blue:Byte, Alpha:Byte )
	Return ( alpha Shl 24 | red Shl 16 | green Shl 8 | blue )
End Function

Comments

_PJ_2012
I can't say much regarding BMax since I dont use it, but assuming it still works within the Windows framework, the reason for so many variations on whether similar versions use aRGB or even seem to be reversed, is because there are different image formats and Windows own internal pixel determination.

Sometimes, these are Little-Endian'ed or sometimes the Alpha byte is considered a greater significance - it's annoying, and can be confusing - but it's best to find out what works for your project and stick with it!

Still, it never hurts to have the alternatives on the code arcs :)


Code Archives Forum