# How to convert Int to R,G,B,Trans?

Discussion in 'Android Questions' started by Widget, Oct 15, 2011.

If I have a Color which is an Int, how to I extract the three R,G,B colors (0..255) as well as the transparency (0..255)?

TIA
Widget

Here a general reuseble means:

Code:
`Sub Process_Globals   Type clr(a As Int, r As Int, g As Int, b As Int)   Dim cols As clrEnd SubSplitColors(thisColor)a = cols.ar = cols.rg = cols.gb = cols.bSub SplitColors(x As Long)   cols.Initialize   Dim a, r, g, b As Int   Dim a0, r0, g0 As Long   cols.a = Floor(x / Power(2,24))   r0 = x Mod Power(2,24)   cols.r = Floor(r0 / Power(2,16))   g1 = r0 Mod Power(2,16)   cols.g = Floor(g0 / Power(2,8))   cols.b = cols.g Mod Power(2,8)End Sub`

Got to love a command named "power"... :wav:

Alfcen,
Thanks for the attempt, but I wasn't able to get SplitColors to work reliably. I tried something like it last night using Mod and got similar results. It is not as easy as it looks because Color can be negative.

I finally had to use IntsToBytes() from the ByteConverter library. Here is what I finally ended up with.

Code:
`Sub Globals  Type TARGBColor(Alpha, Red, Green, Blue As Int)End SubSub CnvtByteToInt(  aByte As Byte) As Int  Dim Num As Int  Num = aByte  If aByte < 0 Then Num = Num + 256  Return NumEnd SubSub GetColors(    aColor As Int)  Dim Conv     As ByteConverter   'From ByteConverter library  Dim bytes(8) As Byte     Conv.LittleEndian = False        'Force it to False so we are sure  bytes = Conv.IntsToBytes(Array As Int(aColor))     Dim ARGBColor As TARGBColor  ARGBColor.Initialize     ARGBColor.Alpha = CnvtByteToInt(Bytes(0))  ARGBColor.Red   = CnvtByteToInt(Bytes(1))  ARGBColor.Green = CnvtByteToInt(Bytes(2))  ARGBColor.Blue  = CnvtByteToInt(Bytes(3))  Log("[GetColors] "&ARGBColor)  Return ARGBColorEnd Sub`
I have no idea why B4A doesn't have a built-in function to do this because we are working with colors, RGB and alpha all the time.

Widget

Here is another solution:
Code:
`Sub Activity_Create(FirstTime As Boolean)    Dim argb() As Int    argb = GetARGB(Colors.Transparent)    Log("A = " & argb(0))    Log("R = " & argb(1))    Log("G = " & argb(2))    Log("B = " & argb(3))End SubSub GetARGB(Color As Int) As Int()    Dim res(4) As Int    res(0) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff000000), 24)    res(1) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff0000), 16)    res(2) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff00), 8)    res(3) = Bit.And(Color, 0xff)    Return resEnd Sub`

hani bassam and Peter Simpson like this.

Erel,

Widget

Yeah, I found it after Erel posted his last message. Had I known that 3 days ago it would have made things sooo much easier. Oh well. Live and learn

Widget

Thanks for this post, Erel,
I'm getting differing results when I create an INT value of a Windows 7 color as follows:
VB.NET
Code:
`Dim Colr as integer = System.Drawing.ColorTranslator.ToWin32(Color.Red)`
B4A
Code:
`'returns a color from an integerSub GetARGB(Color As Int) As Int()    Dim res(4) As Int    res(0) = Bit.UnsignedShiftRight(Bit.AND(Color, 0xff000000), 24)    'A (transparency)    res(1) = Bit.UnsignedShiftRight(Bit.AND(Color, 0xff0000), 16)    'R    res(2) = Bit.UnsignedShiftRight(Bit.AND(Color, 0xff00), 8)        'G    res(3) = Bit.AND(Color, 0xff)                                    'B    Return resEnd Sub`
When I use the GetARGB array as follows on the Android tablet:
B4A
Code:
`L1Colr = Fn.getargb(Colr)rt.color2(Colors.RGB(L1Colr(1), L1Colr(2), L1Colr(3)), "{1}")`
I do not get the same color or even close... For example red on the PC is light blue or lavender...
Thanks
Rusty

For those who would like an answer:
In the PC, the value is: (0)- alpha (1)-Red (2)-Green (3)-Blue
In Android, the result of the above featured getARGB needs to load the array inverted (see below and note the res(3)...2, 1, 0 array load):

Code:
`'returns a color from an integerSub GetARGB(Color As Int) As Int()    Dim res(4) As Int    res(3) = Bit.UnsignedShiftRight(Bit.AND(Color, 0xff000000), 24)    'B    res(2) = Bit.UnsignedShiftRight(Bit.AND(Color, 0xff0000), 16)    'G    res(1) = Bit.UnsignedShiftRight(Bit.AND(Color, 0xff00), 8)        'R    res(0) = Bit.AND(Color, 0xff)                                    'A (transparency)    Return resEnd Sub`
in order to be able to match the integer storage from the PC (Windows 7, Visual Studio)
Regards,
Rusty

Last edited: Nov 22, 2013