B4J Question Set label fill color from color stored as int.

Nokia

Active Member
Licensed User
Longtime User
if I have a color stored as int (-16777216). How do I convert int color to use in a style.

lbl.Style = $"-fx-text-fill: <color here>;"$
 

zed

Active Member
Licensed User
Try this
B4X:
Dim myString As String
myString = intToExString(-16777216)
Log(myString)

private Sub intToExString(i As Int) As String
    Dim hexstr As String
    hexstr = Bit.ToHexString(i)
    Return hexstr
End Sub
 
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
Try this
B4X:
Dim myString As String
myString = intToExString(-16777216)
Log(myString)

private Sub intToExString(i As Int) As String
    Dim hexstr As String
    hexstr = Bit.ToHexString(i)
    Return hexstr
End Sub
I tried and tried a similar code as well and with both, the color does not come out right. I have added "#" or "0x" to the bigging and the color does not come out right.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Another option:

B4X:
Public Sub IntColorToCssRGBA(color As Int) As String
    Dim BC As ByteConverter
    Dim ARGB() As Byte = BC.IntsToBytes(Array As Int(color))
    Dim Alpha As Double = (Bit.And(ARGB(0),0XFF) / 255)
    Return $"rgba(${Bit.And(ARGB(1),0XFF)},${Bit.And(ARGB(2),0XFF)},${Bit.And(ARGB(3),0xFF)},${NumberFormat2(Alpha,1,2,2,False)})"$
End Sub

Out of curiosity, why can't you change it directly on the Label?
 
Last edited:
Upvote 0

Nokia

Active Member
Licensed User
Longtime User
Another option:

Public Sub IntColorToCssRGBA(color As Int) As String
Dim BC As ByteConverter
Dim ARGB() As Byte = BC.IntsToBytes(Array As Int(color))
Dim Alpha As Double = (Bit.And(ARGB(0),0XFF) / 255)
Return $"rgba(${Bit.And(ARGB(1),0XFF)},${Bit.And(ARGB(2),0XFF)},${Bit.And(ARGB(3),0xFF)},${NumberFormat2(Alpha,1,2,2,False)})"$
End Sub

Out of curiosity, why can't you change it directly on the Label?

this worked. thx..

I'm saving the colors in a database file as part of configuration settings.
 
Upvote 0
Top