November 28, 2020, 02:26:09 AM

Author Topic: [bb] ARGB Converter by superStruct [ 1+ years ago ]  (Read 482 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] ARGB Converter by superStruct [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : ARGB Converter
Author : superStruct
Posted : 1+ years ago

Description : These functions take the ARGB value that you get from the ReadPixel command and separates them into the alpha, red, green and blue values.

Code :
Code: BlitzBasic
  1. Function Alpha(argb%)
  2.         temp$ = Bin(argb)
  3.         For i = 1 To 8
  4.                 alpha% = Val(Right(Left(temp,i),1))*2^(8 - i) + alpha
  5.         Next
  6.         Return alpha
  7. End Function
  8.  
  9. Function Red(argb%)
  10.         temp$ = Bin(argb)
  11.         For i = 1 To 8
  12.                 red% = Val(Right(Left(temp,i+8),1))*2^(8 - i) + red
  13.         Next
  14.         Return red
  15. End Function
  16.  
  17. Function Green(argb%)
  18.         temp$ = Bin(argb)
  19.         For i = 1 To 8
  20.                 green% = Val(Right(Left(temp,i+16),1))*2^(8 - i) + green
  21.         Next
  22.         Return green
  23. End Function
  24.  
  25. Function Blue(argb%)
  26.         temp$ = Bin(argb)
  27.         For i = 1 To 8
  28.                 blue% = Val(Right(Left(temp,i+24),1))*2^(8 - i) + blue
  29.         Next
  30.         Return blue
  31. End Function
  32.  
  33. Function Val#(StringNumeric$)
  34.  
  35.    Local Num# = 0
  36.    Local Hex1 = ((Left$(StringNumeric$,1)="#") Or (Left$(StringNumeric$,1)="$"))
  37.    Local Hex2 = (Left$(StringNumeric$,2)="0x")
  38.    Local Binary = (Left$(StringNumeric$,1)="%")
  39.    Local i,c
  40.    
  41.    If Hex1 Or Hex2
  42.       StringNumeric$ = Upper(StringNumeric$)
  43.       For i=(Hex1 + (Hex2 * 2) + 1) To Len(StringNumeric$)
  44.         c = Asc(Mid$(StringNumeric$,i,1))
  45.          Select True
  46.             Case (c>=48 And c<=57)  ;0 through 9
  47.                Num# = (Num# * 16) + c-48
  48.             Case (c>=65 And c<=70)  ;A through F
  49.                Num# = (Num# * 16) + c-55
  50.             Default
  51.                Return Num#                        
  52.          End Select
  53.       Next
  54.    Else
  55.       If Binary
  56.          For i=2 To Len(StringNumeric$)
  57.             Select Mid$(StringNumeric$,i,1)
  58.                Case "1"
  59.                   Num# = (Num# * 2) + 1
  60.                Case "0"
  61.                   Num# = (Num# * 2)
  62.                Default
  63.                   Return Num#                        
  64.             End Select
  65.          Next
  66.       Else
  67.          Num# = StringNumeric$
  68.       EndIf
  69.    EndIf
  70.    Return Num#
  71.    
  72. End Function


Comments :


Matty(Posted 1+ years ago)

 What a horribly inefficient way of doing it - using strings!Just do this:(also this already exists in the code archives multiple times!)Please note it is even faster, when processing lots of pixels, to remove the function calls and perform the processing directly...calling functions tens of thousands of times will definitely be slower than simply performing the calculations below directly.
Code: [Select]

function Alpha(argb)
return (argb shr 24) and 255
end function

function Red(argb)
return (argb shr 16) and 255
end function

function Green(argb)
return (argb shr 8) and 255
end function

function Blue(argb)
return argb and 255
end function

function ARGB(red,green,blue,alpha)
return ((alpha and 255) shl 24) or  ((red and 255) shl 16) or ((green and 255) shl 8) or (blue and 255)
end function



 

SimplePortal 2.3.6 © 2008-2014, SimplePortal