View Single Post
velox's Avatar
Posts: 394 | Thanked: 1,341 times | Joined on Dec 2009
#9
Try something like:
Code:
          
Rectangle {
    id: colorTemperature
    width: 100; height: 140
    radius: 12
    property int temperatureInt: {
        return parseInt(temperatureHighLabel.text)
    }
    anchors.centerIn: temperatureHighLabel
    color: {
        if (temperatureInt >= 38 ) {
            return "#b20012" // dark red
        }
        else if (temperatureInt >= 32 ) {
            return "#bd0014" // red
        }
        else if (temperatureInt >= 27 ) {
            return "#ec0019" // scarlet
        }
        else if (temperatureInt >= 21 ) {
            return "#ec9649" // orange
        }
        else if (temperatureInt >= 16 ) {
            return "#f7c200" // yellow
        }
        else if (temperatureInt >= 10 ) {
            return "#9ad346" // rich green
        }
        else if (temperatureInt >= 4  ) {
            return "#47b04b" // green
        }
        else if (temperatureInt >= -1 ) {
            return "#4293ff" // sky blue
        }
        else if (temperatureInt >= -7 ) {
            return "#3c00ff" // blue
        }
        else if (temperatureInt >= -12) {
            return "#6a28a3" // purple
        }
        else if (temperatureInt >= -18) {
            return "#8d009a" // pinky purple
        }
        else return "#bd009a" // magenta
    }
}
I have not tested it in any way, just quickly written it down, so I may have missed something or it may not be correct syntax (I may or may not have even added errors on purpose to help you learn – just kidding). More or less interesting points are:
  • there is a new property with an integer type, this should fix the negative comparisons
  • You had a gap (no colours between -1 and 4) between where you switched to "<=" – it's easier to just stick with ">=" and make an else for everything that does not match.

Keep in mind:
Long lists of if/else almost always are a sign that something could be better thought out.

Edit: The number conversion, of course, is exactly what pichlo pointed out earlier. I just wrote it down.
__________________
slumber: sensors enabled sleep timer for SFOS (translations/input/… appreciated if you've got some spare time)
talefish: directory based audiobook player for SFOS
nofono: ofono restart for SFOS
___
list of i486/noarch packages on openrepos (jolla tablet)

Last edited by velox; 2018-09-05 at 06:54. Reason: credit where credit is due
 

The Following 4 Users Say Thank You to velox For This Useful Post: