From d325de751098656ca98a44c929ff9721f1f6284c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 19 Dec 2021 09:44:53 -0600 Subject: [PATCH] Add additional guarding to color_rgb_to_rgbww (#62220) --- homeassistant/util/color.py | 4 +++- tests/util/test_color.py | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/homeassistant/util/color.py b/homeassistant/util/color.py index e8f802e3aa5..ccb4980492f 100644 --- a/homeassistant/util/color.py +++ b/homeassistant/util/color.py @@ -450,7 +450,9 @@ 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 if w_b else 0) + 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) diff --git a/tests/util/test_color.py b/tests/util/test_color.py index 9c4b781dc65..31778781676 100644 --- a/tests/util/test_color.py +++ b/tests/util/test_color.py @@ -401,6 +401,8 @@ def test_color_rgb_to_rgbww(): 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) + assert color_util.color_rgb_to_rgbww(0, 0, 0, 0, 100) == (0, 0, 0, 0, 0) + assert color_util.color_rgb_to_rgbww(255, 255, 255, 1, 5) == (103, 69, 0, 255, 255) def test_color_temperature_to_rgbww():