B4J Question Rgba to hex using xui.Color_ARGB has weird behaviour

FrostCodes

Active Member
Licensed User
Hello everyone,
So I am trying to convert an RGBA color to a Hex color.


B4X:
Private Sub ColorToHex(clr As Int) As String
    Dim bc As ByteConverter
    Return bc.HexFromBytes(bc.IntsToBytes(Array As Int(clr)))
End Sub


Private Sub RgbaToHex(RgbaColorStr As String) As String
    Dim parts() As String = Regex.Split(",", RgbaColorStr.SubString2(5, RgbaColorStr.Length - 1))
   
    Dim a As Float
    Dim r, g, b As Int
   
    r = parts(0)
    g = parts(1)
    b = parts(2)
    a = Round(255 * parts(3))
   
    Return "#" & ColorToHex(xui.Color_ARGB(r, g, b, a))
End Sub

The above code seems to work but if you look closely it only works when I pass the params of
xui.Color_ARGB as r, g, b, a but the original order of the param is a, r, g, b
What could be wrong or is this the expected behaviour?
 

William Lancee

Well-Known Member
Licensed User
Longtime User
Based on your prefix #, you want the Web hex representation, which has the alpha level lowest to the right.
but the xui.Color_ARGB(a,r,g,b) function puts the alpha to the left most position.

So no mystery, you just switched the integer values to do what you need. The ColorToHex function, blind to what the numbers represent, does the job.
 
Upvote 0
Top