"""Shared usage-percent color scale (green at 0 % → red at 100 %)."""
from __future__ import annotations

_PERCENT_COLOR_GREEN = (102, 255, 102)
_PERCENT_COLOR_RED = (255, 68, 68)


def _interpolate_rgb(pct: float) -> tuple[int, int, int]:
    t = max(0.0, min(100.0, pct)) / 100.0
    return (
        int(_PERCENT_COLOR_GREEN[0] + (_PERCENT_COLOR_RED[0] - _PERCENT_COLOR_GREEN[0]) * t),
        int(_PERCENT_COLOR_GREEN[1] + (_PERCENT_COLOR_RED[1] - _PERCENT_COLOR_GREEN[1]) * t),
        int(_PERCENT_COLOR_GREEN[2] + (_PERCENT_COLOR_RED[2] - _PERCENT_COLOR_GREEN[2]) * t),
    )


def percent_color(pct: float) -> str:
    r, g, b = _interpolate_rgb(pct)
    return f"#{r:02x}{g:02x}{b:02x}"


def percent_color_rgba(pct: float, alpha: int = 255) -> tuple[int, int, int, int]:
    r, g, b = _interpolate_rgb(pct)
    return r, g, b, alpha
