From 3eabd69666f8a8f08670f75abd95260441a3f5dc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 10 Dec 2021 20:19:54 -1000 Subject: [PATCH] Fix exception in color_rgb_to_rgbww (#61466) --- homeassistant/util/color.py | 2 +- tests/util/test_color.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/homeassistant/util/color.py b/homeassistant/util/color.py index 2daccf28915..3d4f7122ad0 100644 --- a/homeassistant/util/color.py +++ b/homeassistant/util/color.py @@ -450,7 +450,7 @@ def color_rgb_to_rgbww( 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, g / w_g, b / w_b) + white_level = min(r / w_r, g / w_g, 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) diff --git a/tests/util/test_color.py b/tests/util/test_color.py index db9ad988aee..d806a941965 100644 --- a/tests/util/test_color.py +++ b/tests/util/test_color.py @@ -366,3 +366,38 @@ def test_get_color_in_voluptuous(): schema("not a color") assert schema("red") == (255, 0, 0) + + +def test_color_rgb_to_rgbww(): + """Test color_rgb_to_rgbww conversions.""" + assert color_util.color_rgb_to_rgbww(255, 255, 255, 154, 370) == ( + 0, + 54, + 98, + 255, + 255, + ) + assert color_util.color_rgb_to_rgbww(255, 255, 255, 100, 1000) == ( + 255, + 255, + 255, + 0, + 0, + ) + assert color_util.color_rgb_to_rgbww(255, 255, 255, 1, 1000) == ( + 0, + 118, + 241, + 255, + 255, + ) + assert color_util.color_rgb_to_rgbww(128, 128, 128, 154, 370) == ( + 0, + 27, + 49, + 128, + 128, + ) + assert color_util.color_rgb_to_rgbww(64, 64, 64, 154, 370) == (0, 14, 25, 64, 64) + assert color_util.color_rgb_to_rgbww(32, 64, 16, 154, 370) == (9, 64, 0, 38, 38) + assert color_util.color_rgb_to_rgbww(0, 0, 0, 154, 370) == (0, 0, 0, 0, 0)