def color_rgb_to_rgbww(
    r: int,
    g: int,
    b: int,
    min_kelvin: int,
    max_kelvin: int,
) -> tuple[int, int, int, int, int]:
    """Convert an rgb color to an rgbww representation."""
    # Find the color temperature when both white channels have equal brightness
    max_mireds = color_temperature_kelvin_to_mired(min_kelvin)
    min_mireds = color_temperature_kelvin_to_mired(max_kelvin)
    mired_range = max_mireds - min_mireds
    mired_midpoint = min_mireds + mired_range / 2
    color_temp_kelvin = color_temperature_mired_to_kelvin(mired_midpoint)
    w_r, w_g, w_b = color_temperature_to_rgb(color_temp_kelvin)

    # Find the ratio of the midpoint white in the input rgb channels
    white_level = min(
        r / w_r if w_r else 0,
        g / w_g if w_g else 0,
        b / w_b if w_b else 0,
    )

    # Subtract the white portion from the rgb channels.
    rgb = (r - w_r * white_level, g - w_g * white_level, b - w_b * white_level)
    rgbww = (*rgb, round(white_level * 255), round(white_level * 255))

    # Match the output maximum value to the input. This ensures the full
    # channel range is used.
    return match_max_scale((r, g, b), rgbww)  # type: ignore[return-value]
Convert an rgb color to an rgbww representation.
webapp/homeassistant_util_color.py