How can I modify this function to include blue and properly work within the -1 to 1 range? I’m struggling to figure out the right math for this. Any help would be great!
I’ve been working with color mapping in JavaScript for a while, and I think I can offer a slightly different approach that might be useful for your specific requirements. Instead of using a single linear equation, we can break it down into segments for more precise control:
function valueToHue(value) {
let hue;
if (value >= 0.5) {
hue = 60 - (value - 0.5) * 120; // Yellow to Red
} else if (value >= 0) {
hue = 170 - value * 220; // Green to Yellow
} else {
hue = 200 - value * 30; // Blue to Green
}
return `hsl(${Math.round(hue)}, 100%, 50%)`;
}
This function divides the range into three parts, allowing for more accurate color transitions at each stage. It should give you a smoother gradient across the entire spectrum. Remember to adjust the multipliers if you need to fine-tune the color transitions further. Also, rounding the hue value can help prevent any potential floating-point issues.
This function handles both positive and negative values separately, ensuring a smooth transition between colors. It should give you red at 1, yellow at 0.5, green at 0, and blue at -1, as requested. You may need to fine-tune the calculations slightly to get the exact hues you want, but this should serve as a solid starting point.