jeudi 27 février 2020

Evaluate value for multiple ranges

what I am looking to achieve, is to take a Value for temperature, and check where it exists within certain ranges and to return a colour based on predefined thresholds.

I am using a customized Javascript color gradient to generate the colour gradients.

var temperatureThreasholds      = [15, 20, 24, 35];

var colourRangeTemperatureCold  =   generateColor("#00FFa0", "#0000FF", temperatureThreasholds[1] - temperatureThreasholds[0] + 1);
var colourRangeTemperatureWarm  =   generateColor("#FF0000", "#A0FF00", (temperatureThreasholds[3] - temperatureThreasholds[2]) + 1);

var _value = Math.round(Value); // Drop the decimal point for evaluation, round to nearest whole number

if      ( _value <= temperatureThreasholds[0] )       return [0, 0, 255, 200];
else if ( _value < temperatureThreasholds[1] )        return colourRangeTemperatureCold[_value - temperatureThreasholds[0]];
else if ( _value >= temperatureThreasholds[3] )       return [255, 0, 0, 200];
else if ( _value > temperatureThreasholds[2] )        return colourRangeTemperatureWarm[_value - (temperatureThreasholds[2] + 1)];
else if ( _value >= temperatureThreasholds[1] &&
          _value <= temperatureThreasholds[2] )       return [0, 255, 0, 128];
else                                                  return [237, 237, 237, 128];  // If the room highlights in gray then the value wasn't caught in logic

Any support to reduce the code and make it easier to read would be greatly appreaciate.

Aucun commentaire:

Enregistrer un commentaire