Android Code Snippet Convert VB6 / Windows color to b4a

This is a very simple code wich converts windows colors in b4a.
It's something trivial, but I've seen a lot of posts about color conversion, so I wrote this code.

The parameter Vb6Color can be a normal integer of exadecimal.
For example to convert the blue you can pass 16711680 or 0xFF0000

B4X:
Sub vb6Color2B4A(Vb6Color As Int ) As Int
    Dim rgb(3) As Int, r As Int
    rgb(0) = Bit.UnsignedShiftRight(Bit.AND(vb6Color, 0xff0000), 16)
    rgb(1) = Bit.UnsignedShiftRight(Bit.AND(vb6Color, 0xff00), 8)
    rgb(2) = Bit.AND(vb6Color, 0xff)           
    r= Colors.RGB(rgb(2),rgb(1),rgb(0))
    Return r
End Sub

You can improve this code adding a second parameter 'Alpha' for the alpha channel (range 0-255) and instead of Colors.RGB you can use Colors.ARGB which accept also the alpha value.

A bit of theory: why is there a difference in colors between windows and b4a?
Well, technically the difference is only apparent. Simply the same number is expressed in two different ways. Windows sees 16711680 as 0xFF0000 and other sistems (Android, Apple...) treats the number as 0x0000FF.
It's the long story about Big Endian and Little Endian: how a two (or more) bytes number is represented within a system, which byte comes first.

'Big Endian' and 'Little Endian' comes from a very famous book by Jonathan Swift (Gullver's Travels). In One of those travels Lemuel Gulliver meets a people divided in two faction. One faction breaks the eggs starting from the 'big end', the other from the little end (the 'edge' of the egg).
 

Beja

Expert
Licensed User
Longtime User
Hi Straker and thanks for sharing this.
Regarding the difference in color values between Windows and Android, I suddenly recalled my old brain cells and remembered
the way Intel and Motorola CPUs stored data in the CPU registers. Intel put the most significant byte first, while Motorola put it'at the end.
Zilog CPU (Z8xxx) followed Intel. I had hard start designing with MC6802 CPU, after using Z80 for a long time. I think Android
followed Motorola convention.
 
Top