Code archives/Graphics/ARGB type

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

Download source code

ARGB type by elyobo2008
pixel to colour bytes
Strict

' Type ARGB permits conversion from individual colour bytes to colour pixel and back
' All original code by codename El Yobo. 
' Free to use by any lifeform in the solar system or beyond.. NO GUARANTEES - it works for me.
' I guess multiplication by 256 also works..
' For BGRA just swap all the reds and blues - I think??!

Type ARGB
Field PColPixel:Int
Field PRed:Byte
Field PGreen:Byte
Field PBlue:Byte
Field PAlpha:Byte

Function MakeARGBc:ARGB(pixred:Byte, pixgreen:Byte, pixblue:Byte, pixalpha:Byte)
Local myARGB:ARGB = New ARGB
Local col:Int = Int pixred
col = col Shl 8
Local intnum:Int = Int  pixgreen
col = col~intnum
col = col Shl 8
intnum:Int = Int  pixblue
col = col~intnum
col = col Shl 8
intnum:Int = Int pixalpha
col = col~intnum
myARGB.PColPixel = col
myARGB.PRed = pixred
myARGB.PGreen = pixgreen
myARGB.PBlue = pixblue
myARGB.PAlpha = pixalpha

Return myARGB
End Function

Function MakeARGBp:ARGB(pixcolpixel:Int)
Local myARGB:ARGB = New ARGB
Local buf:Byte = 255
Local intnum:Int = 0
intnum = Int buf
myARGB.PAlpha = Byte (pixcolpixel&intnum)
myARGB.PBlue = Byte ((pixcolpixel Shr 8)&intnum)
myARGB.PGreen = Byte((pixcolpixel Shr 16)&intnum)
myARGB.PRed = Byte((pixcolpixel Shr 24)&intnum)
myARGB.PColPixel = pixcolpixel
Return myARGB
EndFunction 

EndType

'In application..

Graphics 640, 480

Local colourX:ARGB

colourX = ARGB.MakeARGBc(34, 32, 234, 255)

Local pxmp:TPixmap = CreatePixmap(20, 20, PF_RGBA8888)

For Local i = 0 To 19
For Local j = 0 To 19

WritePixel(pxmp, i, j, colourX.PColPixel)

Next
Next

DrawPixmap(pxmp, 200, 200)
Flip
WaitKey()

Local colourY:ARGB = ARGB.MakeARGBp(234*23*123*245)

Print colourY.PRed
Print colourY.PGreen
Print colourY.PBlue
Print colourY.PAlpha

'and of course..

colourX = ARGB.MakeARGBp(colourX.PColPixel)

Print colourX.PRed
Print colourX.PGreen
Print colourX.PBlue
Print colourX.PAlpha


End

Comments

UnderwoodNullium2008
Thanks a lot man! :)


Mordax_Praetorian2009
No joke here man, your tutorial just saved my job, thankyou


Jesse2009
Good luck keeping it Mordax. I just found out that it is not logically correct.
I set it to draw a full black color(0,0,0,255) and it draws blue. At least it does it in my PC.
I think it should be like this:
Type ARGB
	Field PColPixel:Int
	Field PRed:Byte
	Field PGreen:Byte
	Field PBlue:Byte
	Field PAlpha:Byte
	
	Function MakeARGBc:ARGB(pixred:Byte, pixgreen:Byte, pixblue:Byte, pixalpha:Byte)
		Local myARGB:ARGB = New ARGB
		myARGB.PColPixel = (pixalpha Shl 24)| (pixred Shl 16) | ( pixgreen Shl 8) | pixblue
		myARGB.PRed = pixred
		myARGB.PGreen = pixgreen
		myARGB.PBlue = pixblue
		myARGB.PAlpha = pixalpha
		Return myARGB
	End Function
	
	Function MakeARGBp:ARGB(pixcolpixel:Int)
		Local myARGB:ARGB = New ARGB
		myARGB.PAlpha = (pixcolpixel Shr 24) & $FF
		myARGB.PRed   = (pixcolpixel Shr 16) & $FF
		myARGB.PGreen = (pixcolpixel Shr 8) & $FF
		myARGB.PBlue  = (pixcolpixel & $FF)
		myARGB.PColPixel = pixcolpixel
		Return myARGB
	EndFunction 
	
EndType



Mordax_Praetorian2009
umm, thats because 0,0,0,255 is blue

Alpha,Red,Green,Blue

Blue is the last number


_PJ_2009
Actually, that is a good pioint, most of the blitz functions I see regarding colours have this ARGB format, however, it would be more logical in many cases when then retrieving the individual components, to have an Alpha parameter last. i.e. RGBa


Jesse2009
@Mordax_Praetorian

umm, thats because 0,0,0,255 is blue

Alpha,Red,Green,Blue

Blue is the last number



the problem is that the function says this:

Function MakeARGBc:ARGB(pixred:Byte, pixgreen:Byte, pixblue:Byte, pixalpha:Byte)

which makes the 255 the alpha.

@Malice
yes, it's odd how BlitMax refer to the format RGBA when it actually store the colors in ARGB.
I did my own RGBA type for pixmaps and whatever for the BMFC competition. it extracts and replaces independent color components from pixels:

Type TRGBApixel
	Field Red:Int
	Field Green:Int
	Field Blue:Int
	Field Alpha:Int
	Field ipixel:Int
	
	Method Set(r:Int, g:Int, b:Int,A:Int)
		Red = r
		green = g
		Blue = b
		Alpha = a
		ipixel = (A Shl 24) | ((r & $FF) Shl 16) | ((g & $FF) Shl 8) | (b & $FF)
	End Method
	
	Method iSet(i:Int)
		Alpha = i Shr 24
		Red = (i Shr 16) & $FF
		Green = (i Shr 8) & $FF
		Blue = i & $FF
		ipixel = i
	End Method
		
	Method iGet:Int()
		Return ipixel
	End Method
	
	Method GetAlpha:Int()
		Return Alpha
	End Method
	
	Method SetAlpha(a:Int)
		Alpha = a
		ipixel = (ipixel & $00FFFFFF) | ((a & $FF) Shl 24)
	End Method
	
	Method getRed:Int()
		Return Red
	End Method
	
	Method setRed(r:Int)
		Red = r
		ipixel = (ipixel & $FF00FFFF) | ((r & $FF) Shl 16)
	End Method
	
	Method getGreen:Int()
		Return Green
	End Method
	
	Method setGreen(g:Int)
		Green = g
		ipixel = (ipixel & $FFFF00FF) | ((g & $FF) Shl 8)
	End Method

	Method getBlue:Int()
		Return Blue
	End Method
	
	Method setBlue(b:Int)
		Blue = b
		ipixel = (ipixel & $FFFFFF00) | (b & $FF)
	End Method
	
End Type



Macguffin2009
This is excellent stuff, guys, and finally let me make sense of this stuff. Thanks!


beanage2009
Finally something like this appears in the code archives. Wonder why mark didnt include this functionality into max.. would have saved e some headaches^^


Code Archives Forum