Ooops
March 05, 2021, 07:21:07 AM

Author Topic: [bmx] ARGB type by elyobo [ 1+ years ago ]  (Read 429 times)

Offline BlitzBot

[bmx] ARGB type by elyobo [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : ARGB type
Author : elyobo
Posted : 1+ years ago

Description : pixel to colour bytes

Code :
Code: BlitzMax
  1. Strict
  2.  
  3. ' Type ARGB permits conversion from individual colour bytes to colour pixel and back
  4. ' All original code by codename El Yobo.
  5. ' Free to use by any lifeform in the solar system or beyond.. NO GUARANTEES - it works for me.
  6. ' I guess multiplication by 256 also works..
  7. ' For BGRA just swap all the reds and blues - I think??!
  8.  
  9. Type ARGB
  10. Field PColPixel:Int
  11. Field PRed:Byte
  12. Field PGreen:Byte
  13. Field PBlue:Byte
  14. Field PAlpha:Byte
  15.  
  16. Function MakeARGBc:ARGB(pixred:Byte, pixgreen:Byte, pixblue:Byte, pixalpha:Byte)
  17. Local myARGB:ARGB = New ARGB
  18. Local col:Int = Int pixred
  19. col = col Shl 8
  20. Local intnum:Int = Int  pixgreen
  21. col = col~intnum
  22. col = col Shl 8
  23. intnum:Int = Int  pixblue
  24. col = col~intnum
  25. col = col Shl 8
  26. intnum:Int = Int pixalpha
  27. col = col~intnum
  28. myARGB.PColPixel = col
  29. myARGB.PRed = pixred
  30. myARGB.PGreen = pixgreen
  31. myARGB.PBlue = pixblue
  32. myARGB.PAlpha = pixalpha
  33.  
  34. Return myARGB
  35. End Function
  36.  
  37. Function MakeARGBp:ARGB(pixcolpixel:Int)
  38. Local myARGB:ARGB = New ARGB
  39. Local buf:Byte = 255
  40. Local intnum:Int = 0
  41. intnum = Int buf
  42. myARGB.PAlpha = Byte (pixcolpixel&intnum)
  43. myARGB.PBlue = Byte ((pixcolpixel Shr 8)&intnum)
  44. myARGB.PGreen = Byte((pixcolpixel Shr 16)&intnum)
  45. myARGB.PRed = Byte((pixcolpixel Shr 24)&intnum)
  46. myARGB.PColPixel = pixcolpixel
  47. Return myARGB
  48. EndFunction
  49.  
  50. EndType
  51.  
  52. 'In application..
  53.  
  54. Graphics 640, 480
  55.  
  56. Local colourX:ARGB
  57.  
  58. colourX = ARGB.MakeARGBc(34, 32, 234, 255)
  59.  
  60. Local pxmp:TPixmap = CreatePixmap(20, 20, PF_RGBA8888)
  61.  
  62. For Local i = 0 To 19
  63. For Local j = 0 To 19
  64.  
  65. WritePixel(pxmp, i, j, colourX.PColPixel)
  66.  
  67. Next
  68. Next
  69.  
  70. DrawPixmap(pxmp, 200, 200)
  71. Flip
  72. WaitKey()
  73.  
  74. Local colourY:ARGB = ARGB.MakeARGBp(234*23*123*245)
  75.  
  76. Print colourY.PRed
  77. Print colourY.PGreen
  78. Print colourY.PBlue
  79. Print colourY.PAlpha
  80.  
  81. 'and of course..
  82.  
  83. colourX = ARGB.MakeARGBp(colourX.PColPixel)
  84.  
  85. Print colourX.PRed
  86. Print colourX.PGreen
  87. Print colourX.PBlue
  88. Print colourX.PAlpha
  89.  
  90.  
  91. End


Comments :


UnderwoodNullium(Posted 1+ years ago)

 Thanks a lot man!  :)


Mordax_Praetorian(Posted 1+ years ago)

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


Jesse(Posted 1+ years ago)

 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:
Code: [Select]
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_Praetorian(Posted 1+ years ago)

 umm, thats because 0,0,0,255 is blueAlpha,Red,Green,BlueBlue is the last number


_PJ_(Posted 1+ years ago)

 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


Jesse(Posted 1+ years ago)

 @Mordax_Praetorian<div class="quote"> umm, thats because 0,0,0,255 is blueAlpha,Red,Green,BlueBlue is the last number </div>the problem is that the function says this:
Code: [Select]
Function MakeARGBc:ARGB(pixred:Byte, pixgreen:Byte, pixblue:Byte, pixalpha:Byte)
which makes the 255 the alpha.@Maliceyes, 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:
Code: [Select]

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



Macguffin(Posted 1+ years ago)

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


beanage(Posted 1+ years ago)

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

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal