From 5223d2066826934eba2f9bedeae143832962dae5 Mon Sep 17 00:00:00 2001 From: mikebarris Date: Thu, 9 Jun 2016 00:25:32 -0500 Subject: [PATCH] Removed webcolors dependency in favor of dictionary lookup. (#2215) * Removed webcolors dependency in favor of dictionary lookup. * Fixed code style errors. * Moved color dictionary to module per suggestion. * Removed try/except per suggestion. --- homeassistant/util/color.py | 27 ++++++++++++++++++++++++++- requirements_all.txt | 1 - setup.py | 1 - tests/util/test_color.py | 16 ++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/homeassistant/util/color.py b/homeassistant/util/color.py index 23412344a85..1f702a50193 100644 --- a/homeassistant/util/color.py +++ b/homeassistant/util/color.py @@ -1,10 +1,35 @@ """Color util methods.""" +import logging import math # pylint: disable=unused-import -from webcolors import html5_parse_legacy_color as color_name_to_rgb # noqa + +_LOGGER = logging.getLogger(__name__) HASS_COLOR_MAX = 500 # mireds (inverted) HASS_COLOR_MIN = 154 +COLORS = { + 'white': (255, 255, 255), 'beige': (245, 245, 220), + 'tan': (210, 180, 140), 'gray': (128, 128, 128), + 'navy blue': (0, 0, 128), 'royal blue': (8, 76, 158), + 'blue': (0, 0, 255), 'azure': (0, 127, 255), 'aqua': (127, 255, 212), + 'teal': (0, 128, 128), 'green': (0, 255, 0), + 'forest green': (34, 139, 34), 'olive': (128, 128, 0), + 'chartreuse': (127, 255, 0), 'lime': (191, 255, 0), + 'golden': (255, 215, 0), 'red': (255, 0, 0), 'coral': (0, 63, 72), + 'hot pink': (252, 15, 192), 'fuchsia': (255, 119, 255), + 'lavender': (181, 126, 220), 'indigo': (75, 0, 130), + 'maroon': (128, 0, 0), 'crimson': (220, 20, 60)} + + +def color_name_to_rgb(color_name): + """Convert color name to RGB hex value.""" + hex_value = COLORS.get(color_name.lower()) + + if not hex_value: + _LOGGER.error('unknown color supplied %s default to white', color_name) + hex_value = COLORS['white'] + + return hex_value # Taken from: diff --git a/requirements_all.txt b/requirements_all.txt index 3f9c77f32b1..807ad2474e8 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -5,7 +5,6 @@ pytz>=2016.4 pip>=7.0.0 jinja2>=2.8 voluptuous==0.8.9 -webcolors==1.5 # homeassistant.components.isy994 PyISY==1.0.6 diff --git a/setup.py b/setup.py index c531281c75b..fbce912c3d6 100755 --- a/setup.py +++ b/setup.py @@ -17,7 +17,6 @@ REQUIRES = [ 'pip>=7.0.0', 'jinja2>=2.8', 'voluptuous==0.8.9', - 'webcolors==1.5', ] setup( diff --git a/tests/util/test_color.py b/tests/util/test_color.py index 82222a0b8a1..884b59ec10e 100644 --- a/tests/util/test_color.py +++ b/tests/util/test_color.py @@ -59,6 +59,22 @@ class TestColorUtil(unittest.TestCase): self.assertEqual([51, 153, 255, 0], color_util.rgb_hex_to_rgb_list('3399ff00')) + def test_color_name_to_rgb_valid_name(self): + """Test color_name_to_rgb.""" + self.assertEqual((255, 0, 0), + color_util.color_name_to_rgb('red')) + + self.assertEqual((0, 0, 255), + color_util.color_name_to_rgb('blue')) + + self.assertEqual((0, 255, 0), + color_util.color_name_to_rgb('green')) + + def test_color_name_to_rgb_unknown_name_default_white(self): + """Test color_name_to_rgb.""" + self.assertEqual((255, 255, 255), + color_util.color_name_to_rgb('not a color')) + class ColorTemperatureMiredToKelvinTests(unittest.TestCase): """Test color_temperature_mired_to_kelvin."""