From f8f060b72ba415e88a94f0db590dd91bf568d7aa Mon Sep 17 00:00:00 2001 From: Matthias Alphart <farmio@alphart.net> Date: Thu, 11 Nov 2021 07:29:16 +0100 Subject: [PATCH] Make util.color._match_max_scale public (#59207) --- homeassistant/util/color.py | 12 ++++++------ tests/util/test_color.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/homeassistant/util/color.py b/homeassistant/util/color.py index 1cfd6447e8a..2daccf28915 100644 --- a/homeassistant/util/color.py +++ b/homeassistant/util/color.py @@ -404,8 +404,8 @@ def color_hs_to_xy( return color_RGB_to_xy(*color_hs_to_RGB(iH, iS), Gamut) -def _match_max_scale( - input_colors: tuple[int, ...], output_colors: tuple[int, ...] +def match_max_scale( + input_colors: tuple[int, ...], output_colors: tuple[float, ...] ) -> tuple[int, ...]: """Match the maximum value of the output to the input.""" max_in = max(input_colors) @@ -426,7 +426,7 @@ def color_rgb_to_rgbw(r: int, g: int, b: int) -> tuple[int, int, int, int]: # Match the output maximum value to the input. This ensures the full # channel range is used. - return _match_max_scale((r, g, b), rgbw) # type: ignore + return match_max_scale((r, g, b), rgbw) # type: ignore[return-value] def color_rgbw_to_rgb(r: int, g: int, b: int, w: int) -> tuple[int, int, int]: @@ -436,7 +436,7 @@ def color_rgbw_to_rgb(r: int, g: int, b: int, w: int) -> tuple[int, int, int]: # Match the output maximum value to the input. This ensures the # output doesn't overflow. - return _match_max_scale((r, g, b, w), rgb) # type: ignore + return match_max_scale((r, g, b, w), rgb) # type: ignore[return-value] def color_rgb_to_rgbww( @@ -458,7 +458,7 @@ def color_rgb_to_rgbww( # 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 match_max_scale((r, g, b), rgbww) # type: ignore[return-value] def color_rgbww_to_rgb( @@ -481,7 +481,7 @@ def color_rgbww_to_rgb( # Match the output maximum value to the input. This ensures the # output doesn't overflow. - return _match_max_scale((r, g, b, cw, ww), rgb) # type: ignore + return match_max_scale((r, g, b, cw, ww), rgb) # type: ignore[return-value] def color_rgb_to_hex(r: int, g: int, b: int) -> str: diff --git a/tests/util/test_color.py b/tests/util/test_color.py index 8f520f4a7ec..db9ad988aee 100644 --- a/tests/util/test_color.py +++ b/tests/util/test_color.py @@ -279,6 +279,20 @@ def test_color_rgb_to_hex(): assert color_util.color_rgb_to_hex(255, 67.9204190, 0) == "ff4400" +def test_match_max_scale(): + """Test match_max_scale.""" + match_max_scale = color_util.match_max_scale + assert match_max_scale((255, 255, 255), (255, 255, 255)) == (255, 255, 255) + assert match_max_scale((0, 0, 0), (0, 0, 0)) == (0, 0, 0) + assert match_max_scale((255, 255, 255), (128, 128, 128)) == (255, 255, 255) + assert match_max_scale((0, 255, 0), (64, 128, 128)) == (128, 255, 255) + assert match_max_scale((0, 100, 0), (128, 64, 64)) == (100, 50, 50) + assert match_max_scale((10, 20, 33), (100, 200, 333)) == (10, 20, 33) + assert match_max_scale((255,), (100, 200, 333)) == (77, 153, 255) + assert match_max_scale((128,), (10.5, 20.9, 30.4)) == (44, 88, 128) + assert match_max_scale((10, 20, 30, 128), (100, 200, 333)) == (38, 77, 128) + + def test_gamut(): """Test gamut functions.""" assert color_util.check_valid_gamut(GAMUT) -- GitLab