From d1121478ab6e50546422a3921e882fe33a886362 Mon Sep 17 00:00:00 2001 From: Anders Melchiorsen Date: Thu, 27 Apr 2017 09:59:49 +0200 Subject: [PATCH] Reduce color_xy_brightness_to_hsv to color_xy_to_hs (#7320) This makes more sense because the input and output brightness is the same. We currently convert through RGB which does contain a brightness so we supply an arbitrary value as input and discard the output. --- homeassistant/components/light/hue.py | 10 ++++---- .../components/light/lifx/__init__.py | 5 +--- homeassistant/util/color.py | 8 +++---- tests/util/test_color.py | 24 +++++++++---------- 4 files changed, 21 insertions(+), 26 deletions(-) diff --git a/homeassistant/components/light/hue.py b/homeassistant/components/light/hue.py index 22e2541b271..c15acdd2b44 100644 --- a/homeassistant/components/light/hue.py +++ b/homeassistant/components/light/hue.py @@ -380,12 +380,10 @@ class HueLight(Light): if ATTR_XY_COLOR in kwargs: if self.info.get('manufacturername') == "OSRAM": - hsv = color_util.color_xy_brightness_to_hsv( - *kwargs[ATTR_XY_COLOR], - ibrightness=self.info['bri']) - command['hue'] = hsv[0] - command['sat'] = hsv[1] - command['bri'] = hsv[2] + hue, sat = color_util.color_xy_to_hs(*kwargs[ATTR_XY_COLOR]) + command['hue'] = hue + command['sat'] = sat + command['bri'] = self.info['bri'] else: command['xy'] = kwargs[ATTR_XY_COLOR] elif ATTR_RGB_COLOR in kwargs: diff --git a/homeassistant/components/light/lifx/__init__.py b/homeassistant/components/light/lifx/__init__.py index c43b901e910..33ed80bed06 100644 --- a/homeassistant/components/light/lifx/__init__.py +++ b/homeassistant/components/light/lifx/__init__.py @@ -354,10 +354,7 @@ class LIFXLight(Light): brightness = self._bri if ATTR_XY_COLOR in kwargs: - hue, saturation, _ = \ - color_util.color_xy_brightness_to_hsv( - *kwargs[ATTR_XY_COLOR], - ibrightness=255) + hue, saturation = color_util.color_xy_to_hs(*kwargs[ATTR_XY_COLOR]) saturation = saturation * (BYTE_MAX + 1) changed_color = True diff --git a/homeassistant/util/color.py b/homeassistant/util/color.py index 57d88c5328d..a925ea337fb 100644 --- a/homeassistant/util/color.py +++ b/homeassistant/util/color.py @@ -268,10 +268,10 @@ def color_RGB_to_hsv(iR: int, iG: int, iB: int) -> Tuple[int, int, int]: # pylint: disable=invalid-sequence-index -def color_xy_brightness_to_hsv(vX: float, vY: float, - ibrightness: int) -> Tuple[int, int, int]: - """Convert an xy brightness color to its hsv representation.""" - return color_RGB_to_hsv(*color_xy_brightness_to_RGB(vX, vY, ibrightness)) +def color_xy_to_hs(vX: float, vY: float) -> Tuple[int, int]: + """Convert an xy color to its hs representation.""" + h, s, _ = color_RGB_to_hsv(*color_xy_brightness_to_RGB(vX, vY, 255)) + return (h, s) # pylint: disable=invalid-sequence-index diff --git a/tests/util/test_color.py b/tests/util/test_color.py index bf2f4e5832f..43b5904a895 100644 --- a/tests/util/test_color.py +++ b/tests/util/test_color.py @@ -56,22 +56,22 @@ class TestColorUtil(unittest.TestCase): self.assertEqual((0, 255, 255), color_util.color_RGB_to_hsv(255, 0, 0)) - def test_color_xy_brightness_to_hsv(self): - """Test color_RGB_to_xy.""" - self.assertEqual(color_util.color_RGB_to_hsv(0, 0, 0), - color_util.color_xy_brightness_to_hsv(1, 1, 0)) + def test_color_xy_to_hs(self): + """Test color_xy_to_hs.""" + self.assertEqual((8609, 255), + color_util.color_xy_to_hs(1, 1)) - self.assertEqual(color_util.color_RGB_to_hsv(255, 243, 222), - color_util.color_xy_brightness_to_hsv(.35, .35, 255)) + self.assertEqual((6950, 32), + color_util.color_xy_to_hs(.35, .35)) - self.assertEqual(color_util.color_RGB_to_hsv(255, 0, 60), - color_util.color_xy_brightness_to_hsv(1, 0, 255)) + self.assertEqual((62965, 255), + color_util.color_xy_to_hs(1, 0)) - self.assertEqual(color_util.color_RGB_to_hsv(0, 255, 0), - color_util.color_xy_brightness_to_hsv(0, 1, 255)) + self.assertEqual((21845, 255), + color_util.color_xy_to_hs(0, 1)) - self.assertEqual(color_util.color_RGB_to_hsv(0, 63, 255), - color_util.color_xy_brightness_to_hsv(0, 0, 255)) + self.assertEqual((40992, 255), + color_util.color_xy_to_hs(0, 0)) def test_rgb_hex_to_rgb_list(self): """Test rgb_hex_to_rgb_list."""