I have a value representing the +/-ity of an article which ranges from -1 to 1 with -1 being strongly negative and 1 being strongly positive.
I want to turn this into a color such that it ranges from red (strongly negative) pass through grey (neutral) and ends at green (strongly positive).
The solution that I have come up with does not fully satisfy what I am trying to achieve. I am not a color expert but for some reason any value even slightly negative e.g. -.07 is resulting in a bright red. I also expect any positive value to start looking green but what I see is actually orange.
What am I doing wrong? Is there a better way to represent what I am after?
function getAsHslHeatMap(value: number) {
value = Math.max(-1, Math.min(1, value));
let hue;
if (value <= 0) {
// Transition from red to grey | Red is 0 degrees
hue = (1 - value) * 0;
} else {
// Transition from grey to green | Green is 120 degrees
hue = value * 120;
}
// Convert hue to an integer for CSS
hue = Math.round(hue);
let saturation = 100; // Full saturation
let lightness = 50; // Mid lightness
return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
}
