Code archives/Graphics/ARGB Converter
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
| These functions take the ARGB value that you get from the ReadPixel command and separates them into the alpha, red, green and blue values. | |||||
Function Alpha(argb%)
temp$ = Bin(argb)
For i = 1 To 8
alpha% = Val(Right(Left(temp,i),1))*2^(8 - i) + alpha
Next
Return alpha
End Function
Function Red(argb%)
temp$ = Bin(argb)
For i = 1 To 8
red% = Val(Right(Left(temp,i+8),1))*2^(8 - i) + red
Next
Return red
End Function
Function Green(argb%)
temp$ = Bin(argb)
For i = 1 To 8
green% = Val(Right(Left(temp,i+16),1))*2^(8 - i) + green
Next
Return green
End Function
Function Blue(argb%)
temp$ = Bin(argb)
For i = 1 To 8
blue% = Val(Right(Left(temp,i+24),1))*2^(8 - i) + blue
Next
Return blue
End Function
Function Val#(StringNumeric$)
Local Num# = 0
Local Hex1 = ((Left$(StringNumeric$,1)="#") Or (Left$(StringNumeric$,1)="$"))
Local Hex2 = (Left$(StringNumeric$,2)="0x")
Local Binary = (Left$(StringNumeric$,1)="%")
Local i,c
If Hex1 Or Hex2
StringNumeric$ = Upper(StringNumeric$)
For i=(Hex1 + (Hex2 * 2) + 1) To Len(StringNumeric$)
c = Asc(Mid$(StringNumeric$,i,1))
Select True
Case (c>=48 And c<=57) ;0 through 9
Num# = (Num# * 16) + c-48
Case (c>=65 And c<=70) ;A through F
Num# = (Num# * 16) + c-55
Default
Return Num#
End Select
Next
Else
If Binary
For i=2 To Len(StringNumeric$)
Select Mid$(StringNumeric$,i,1)
Case "1"
Num# = (Num# * 2) + 1
Case "0"
Num# = (Num# * 2)
Default
Return Num#
End Select
Next
Else
Num# = StringNumeric$
EndIf
EndIf
Return Num#
End Function |
Comments
| ||
| 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. 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 |
Code Archives Forum