export function mapValueToColor(minVal: number, maxVal: number, actualVal: number): string {
    const clamped = Math.min(Math.max(actualVal, minVal), maxVal);

    // Calculate interpolation ratio (0-1)
    const ratio = maxVal !== minVal
        ? (clamped - minVal) / (maxVal - minVal)
        : 0;
    return hsvDegToHex(120 * ratio);//120° is green, 0° is red
}

export function getColorFromPercent(percent: string): string {
    let perc = Number(percent) / 100
    if(perc > 1){
        perc = 2.5;
    }
    return hsvDegToHex(120 * perc);
}

export function hsvDegToHex(h: number): string {
    h = Math.max(0, Math.min(360, h));

    const c = 1;
    const x = (1 - Math.abs(((h / 60) % 2) - 1));
  
    let r, g, b;
    if (h < 60) {
      r = c; g = x; b = 0;
    } else if (h < 120) {
      r = x; g = c; b = 0;
    } else if (h < 180) {
      r = 0; g = c; b = x;
    } else if (h < 240) {
      r = 0; g = x; b = c;
    } else if (h < 300) {
      r = x; g = 0; b = c;
    } else {
      r = c; g = 0; b = x;
    }
  
    // scale to 0-255
    const rHex = Math.round(r * 255).toString(16).padStart(2, '0');
    const gHex = Math.round(g * 255).toString(16).padStart(2, '0');
    const bHex = Math.round(b * 255).toString(16).padStart(2, '0');
  
    return `#${rHex}${gHex}${bHex}`;
}
