# HeatMap colours without if else

Hi,
I’m working on a heat map and I’m trying to improve my colour code.
I have a grid of squares with each having a value for the heat map, based on that value I have been assigning a colour from an array. I have been doing this using if else statements.

Currently this approach has 21 hard coded if else’s (yikes) and does not allow me to vary the number of steps I want in the graduation of colours.

I wrote the code below thinking it would work to decide the size of the steps for each colour and determine what colour it should be.

It does not do that, it does strange things such as having dark colours where light should be and other craziness that I haven’t been able to pin down.

My question is as follows:
Is there something wrong in my logic for what I am trying to achieve?
Is this best way to achieve filling the colours without resorting to hard coded if else statements?
Can anyone provide some help and guidance to achieve the affect I’m looking for?

val is the value of the square (the heat for the heat map)
highHeat is the greatest heat a single cell has in the heatmap. I use this value to determine the range that I want the array of colours spread over. I’m hoping that this allows me to have the darkest and brightest values even when there are relatively little input data as the current system makes everything very dark in that situation.
Clarification: I can set the number of colours and their hex values, that works just fine, it’s just when I set the colour of the grid square, if I have an array of 10 colours, it only works across the first 10 steps of the if else monster.

``````  void checkCol(int val, int highHeat) {
int stepSize = highHeat/colArray.length;
int low = 0;
int high = 0;
for (int l = 0; l < colArray.length; l++) {
low = stepSize*l;
high = (stepSize*l)+stepSize;
if (val >= low && val < high) {
setFill(colArray[l]);
break;
}
}
}
``````

I know this is a rambling jungle, but any help would be appreciated.

1 Like
``````int stepSize = highHeat/colArray.length+1 //< +1 fixes the issue;
``````

Turns out that this small bodge fixed the issue I was having (that I really didn’t articulate particularly well because I didn’t fully understand it).

That problem was that the `if` was never getting to the highHeat value, so that bodge makes that last range big enough that it catches the highHeat that I passed in.

Now i’ve got a different problem but that’s not related to this.

1 Like