B4J Question Paint a label based on its value

jroriz

Active Member
Licensed User
Longtime User
Hi.

Is there a better way to acomplish this?
I need to paint a label, based on the value, from light red to solid green, passing through the yellow.

My solution:
B4X:
' Use: myLabel.Style = Paint(mylabel.text)
Sub Paint(value As Double) As String
    Dim rgb As String
    If value > 90 Then rgb = "0,128,0"
    If value > 80 And value < 91 Then rgb = "0,255,0"
    If value > 60 And value < 81 Then rgb = "128,255,0"
    If value > 40 And value < 61 Then rgb = "255,255,0"
    If value > 20 And value < 41 Then rgb = "255,255,128"
    If value > 10 And value < 21 Then rgb = "255,128,128"
    If value < 11 Then rgb = "255,0,0"
  
    Return $"-fx-Font-size: 18px; -fx-Font-weight: bold; -fx-background-color: rgb(${rgb});"$
End Sub

Could be a range like this:

range.PNG
 
Last edited:

jroriz

Active Member
Licensed User
Longtime User
1. Don't set the Style property directly. Use CSSUtils.SetBackgroundColor or B4XView.Color.

2. Your code doesn't look 100% correct. What is the expected value for 20.5?

I opted for the style because I need to set bold as well.
The expected value for 20.5 can be either 20.5 or 20. You mentioned this because there is an implicit conversion from string to double?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I opted for the style because I need to set bold as well.
Set properties with CSSUtils. Otherwise you will overwrite existing properties.

You mentioned this because there is an implicit conversion from string to double?
It will be True in both tests:
B4X:
If value > 20 And value < 41 Then rgb = "255,255,128"
If value > 10 And value < 21 Then rgb = "255,128,128"
It will of course work however it looks like a programming mistake.
 
Upvote 0

jroriz

Active Member
Licensed User
Longtime User
Set properties with CSSUtils. Otherwise you will overwrite existing properties.


It will be True in both tests:
B4X:
If value > 20 And value < 41 Then rgb = "255,255,128"
If value > 10 And value < 21 Then rgb = "255,128,128"
It will of course work however it looks like a programming mistake.

Now I see...
Correct code.

B4X:
    If value > 90 Then rgb = "0,128,0"
    If value > 80 And value < 91 Then rgb = "0,255,0"
    If value > 60 And value < 81 Then rgb = "128,255,0"
    If value > 40 And value < 61 Then rgb = "255,255,0"
    If value > 20 And value < 41 Then rgb = "255,255,128"
    If value > 9 And value < 21 Then rgb = "255,128,128"
    If value < 10 Then rgb = "255,0,0"
 
Last edited:
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
You could use a select statement, it makes the code shorter to write and I think clearer.( but that's just personal choice)

B4X:
 Dim rgb As String ="255,0,0"
 Select True
  Case (value > 90): rgb="0,128,0"
  Case (value > 80): rgb="0,255,0"
  Case (value > 60): rgb="128,255,0"
  Case (value > 40): rgb="255,255,0"
  Case (value > 20): rgb="255,255,128"
  Case (value >=10): rgb="255,128,128"
 End Select
 
Last edited:
Upvote 0
Top