From 3d4e27c0c4b710f749c71634ae3559271ae17050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Per=20Osb=C3=A4ck?= Date: Fri, 24 Nov 2017 19:03:46 +0100 Subject: [PATCH] make RGB consitant as int. fixes #10766 --- homeassistant/util/color.py | 16 ++++++++-------- tests/util/test_color.py | 1 + 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/homeassistant/util/color.py b/homeassistant/util/color.py index 794f654611398..ee0ddd5fcac4a 100644 --- a/homeassistant/util/color.py +++ b/homeassistant/util/color.py @@ -355,9 +355,9 @@ def color_rgbw_to_rgb(r, g, b, w): return _match_max_scale((r, g, b, w), rgb) -def color_rgb_to_hex(r, g, b): +def color_rgb_to_hex(r: int, g: int, b: int): """Return a RGB color from a hex color string.""" - return '{0:02x}{1:02x}{2:02x}'.format(r, g, b) + return '{0:02x}{1:02x}{2:02x}'.format(int(r), int(g), int(b)) def rgb_hex_to_rgb_list(hex_string): @@ -392,8 +392,8 @@ def color_temperature_to_rgb(color_temperature_kelvin): return (red, green, blue) -def _bound(color_component: float, minimum: float=0, - maximum: float=255) -> float: +def _bound(color_component: int, minimum: int=0, + maximum: int=255) -> int: """ Bound the given color component value between the given min and max values. @@ -402,10 +402,10 @@ def _bound(color_component: float, minimum: float=0, will be 10. """ color_component_out = max(color_component, minimum) - return min(color_component_out, maximum) + return int(min(color_component_out, maximum)) -def _get_red(temperature: float) -> float: +def _get_red(temperature: float) -> int: """Get the red component of the temperature in RGB space.""" if temperature <= 66: return 255 @@ -413,7 +413,7 @@ def _get_red(temperature: float) -> float: return _bound(tmp_red) -def _get_green(temperature: float) -> float: +def _get_green(temperature: float) -> int: """Get the green component of the given color temp in RGB space.""" if temperature <= 66: green = 99.4708025861 * math.log(temperature) - 161.1195681661 @@ -422,7 +422,7 @@ def _get_green(temperature: float) -> float: return _bound(green) -def _get_blue(temperature: float) -> float: +def _get_blue(temperature: float) -> int: """Get the blue component of the given color temperature in RGB space.""" if temperature >= 66: return 255 diff --git a/tests/util/test_color.py b/tests/util/test_color.py index 4c14258f2f2a0..ed3aa08cd8b93 100644 --- a/tests/util/test_color.py +++ b/tests/util/test_color.py @@ -212,6 +212,7 @@ def test_color_rgb_to_hex(self): assert color_util.color_rgb_to_hex(255, 255, 255) == 'ffffff' assert color_util.color_rgb_to_hex(0, 0, 0) == '000000' assert color_util.color_rgb_to_hex(51, 153, 255) == '3399ff' + assert color_util.color_rgb_to_hex(255, 67.9204190, 0) == 'ff4300' class ColorTemperatureMiredToKelvinTests(unittest.TestCase):