From ef132e4583c3d6df9557769db9b9d921ffcfcfef Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 29 Jan 2016 18:44:21 -0800 Subject: [PATCH] Add tests for color util --- homeassistant/util/color.py | 13 +++++++------ tests/util/test_color.py | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/homeassistant/util/color.py b/homeassistant/util/color.py index a88fa16896bc..e5b11f2afeb1 100644 --- a/homeassistant/util/color.py +++ b/homeassistant/util/color.py @@ -47,6 +47,7 @@ def color_RGB_to_xy(R, G, B): # taken from # https://github.com/benknight/hue-python-rgb-converter/blob/master/rgb_cie.py +# Copyright (c) 2014 Benjamin Knight / MIT License. # pylint: disable=bad-builtin def color_xy_brightness_to_RGB(vX, vY, brightness): ''' @@ -57,12 +58,12 @@ def color_xy_brightness_to_RGB(vX, vY, brightness): return (0, 0, 0) Y = brightness - if vY != 0: - X = (Y / vY) * vX - Z = (Y / vY) * (1 - vX - vY) - else: - X = 0 - Z = 0 + + if vY == 0: + vY += 0.00000000001 + + X = (Y / vY) * vX + Z = (Y / vY) * (1 - vX - vY) # Convert to RGB using Wide RGB D65 conversion. r = X * 1.612 - Y * 0.203 - Z * 0.302 diff --git a/tests/util/test_color.py b/tests/util/test_color.py index 6b0d169f5165..4b4a70ebe9c6 100644 --- a/tests/util/test_color.py +++ b/tests/util/test_color.py @@ -20,3 +20,20 @@ class TestColorUtil(unittest.TestCase): self.assertEqual((0.6400744994567747, 0.3299705106316933), color_util.color_RGB_to_xy(255, 0, 0)) + + def test_color_xy_brightness_to_RGB(self): + """ Test color_RGB_to_xy. """ + self.assertEqual((0, 0, 0), + color_util.color_xy_brightness_to_RGB(1, 1, 0)) + + self.assertEqual((255, 235, 214), + color_util.color_xy_brightness_to_RGB(.35, .35, 255)) + + self.assertEqual((255, 0, 45), + color_util.color_xy_brightness_to_RGB(1, 0, 255)) + + self.assertEqual((0, 255, 0), + color_util.color_xy_brightness_to_RGB(0, 1, 255)) + + self.assertEqual((0, 83, 255), + color_util.color_xy_brightness_to_RGB(0, 0, 255))