"""Farbverlaeufe der Heatmap."""

import bpy
import os
import glob
import time
import math
import traceback

import gpu
import blf
from gpu_extras.batch import batch_for_shader
from bpy_extras.object_utils import world_to_camera_view
from bpy_extras import view3d_utils
from mathutils import Vector
from bpy.types import Panel, Operator, PropertyGroup, AddonPreferences
from bpy.props import (IntProperty, StringProperty, PointerProperty,
                       FloatProperty, BoolProperty, EnumProperty)

from . import state


def get_heatmap_color(value, min_val, max_val):
    """Berechnet Heatmap-Farbe basierend auf Wert (Blau=kalt -> Grün -> Gelb -> Rot=heiß)"""
    if max_val == min_val:
        normalized = 0.5
    else:
        normalized = (value - min_val) / (max_val - min_val)
    
    normalized = max(0.0, min(1.0, normalized))
    
    # Heatmap: Blau -> Cyan -> Grün -> Gelb -> Rot
    if normalized < 0.25:
        t = normalized / 0.25
        r, g, b = 0.0, t, 1.0
    elif normalized < 0.5:
        t = (normalized - 0.25) / 0.25
        r, g, b = 0.0, 1.0, 1.0 - t
    elif normalized < 0.75:
        t = (normalized - 0.5) / 0.25
        r, g, b = t, 1.0, 0.0
    else:
        t = (normalized - 0.75) / 0.25
        r, g, b = 1.0, 1.0 - t, 0.0
    
    return (r, g, b)
