B4J Question CSS problems... can't parse a string into a color

Cableguy

Expert
Licensed User
Longtime User
Hi guys....

I have this line of code...

B4X:
   CSSUtils.SetBorder(mBorder,BorderWidth,fx.Colors.From32Bit(Bit.ParseInt(BorderColor.Replace("#", ""), 16)),radius)

borderWidth, BorderColor and radius are retrieved also using CSSUtils, like this:

B4X:
    Dim radius As String = CSSUtils.GetStyleProperty(mBase,"border-radius")
    Dim BorderWidth As String = CSSUtils.GetStyleProperty(mBase,"border-width")
    Dim BorderColor As String = CSSUtils.GetStyleProperty(mBase,"border-color")

I then trim the string to my needs ...

B4X:
    radius = radius.SubString2(0,radius.LastIndexOf(","))
    BorderWidth = BorderWidth.SubString2(0,BorderWidth.LastIndexOf(","))

my issue is that, BorderColor gets a string like "#DCDCDC"...

I need to parse this STRING into a Color value...
Using the above (first) line of code, it does not give the border the passed color, But, if I do...

B4X:
CSSUtils.SetBorder(mBorder,BorderWidth,fx.Colors.Gray,radius)
thus hardcoding the color value, it works as expected...

What am I doing wrong in the first code line?????
 

stevel05

Expert
Licensed User
Longtime User
The problem is that fx.Colors.From32Bit returns a Color object and not a Paint object.

Try this instead:

B4X:
Dim BorderColor As String = CSSUtils.GetStyleProperty(Pane1,"border-color")
    Log(BorderColor)
    Dim Paint As JavaObject
    Paint.InitializeStatic("javafx.scene.paint.Paint")
    CSSUtils.SetBorder(Pane2,1,Paint.RunMethod("valueOf",Array(BorderColor)),0)

Just a test to copy the colour from one border to another, but you'll get the idea.
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I found a solution...
Funny enough, it was one of my own answers a few months ago to a similar problem.

B4X:
Private Sub GetColor(hex As String) As Paint
    Dim r,g,b As Int
    r = Bit.ParseInt(hex.SubString2(0,2), 16)
    g = Bit.ParseInt(hex.SubString2(2,4), 16)
    b = Bit.ParseInt(hex.SubString2(4,6), 16)

    Return fx.Colors.RGB(r, g, b)
End Sub
 
Upvote 0
Top