from __future__ import annotations import numpy as np from ha_deconz_bridge.color import Color def test_rgb_roundtrip_is_stable() -> None: color = Color.from_rgb((32, 128, 255), brightness=0.5) roundtrip = Color.from_xy(color.xy, brightness=color.brightness) assert np.linalg.norm(color.uv - roundtrip.uv) < 0.05 assert roundtrip.brightness == color.brightness def test_weighted_sum_of_ct_colors_uses_xyz_mixing() -> None: warm = Color.from_color_temp(450, brightness=0.8) cool = Color.from_color_temp(200, brightness=0.2) mixed = Color.weighted_sum((warm, cool), (1.0, 1.0)) assert mixed.mode == "rgb" assert 200 < mixed.color_temp < 450 assert mixed.brightness == 1.0 def test_weighted_sum_promotes_to_rgb_when_any_rgb_present() -> None: white = Color.from_color_temp(300, brightness=0.5) red = Color.from_rgb((1.0, 0.0, 0.0), brightness=0.25) mixed = Color.weighted_sum((white, red), (1.0, 1.0)) assert mixed.mode == "rgb" assert mixed.brightness > 0.0 def test_ct_mix_inverts_back_to_midpoint() -> None: color = Color.from_color_temp(326, brightness=1.0, temp_range=(153, 500)) cool, warm = color.ct_mix((153, 500)) assert 0.0 < cool <= 1.0 assert 0.0 < warm <= 1.0 def test_color_temp_preserves_requested_mireds_for_serialization() -> None: color = Color.from_color_temp(455, brightness=1.0, temp_range=(153, 500)) dimmed = color.with_brightness(0.2) assert color.color_temp == 455 assert dimmed.color_temp == 455