Skip to content

Commit

Permalink
make RGB consitant as int. fixes home-assistant#10766
Browse files Browse the repository at this point in the history
  • Loading branch information
perosb committed Nov 24, 2017
1 parent f6547ec commit 3d4e27c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
16 changes: 8 additions & 8 deletions homeassistant/util/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand All @@ -402,18 +402,18 @@ 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
tmp_red = 329.698727446 * math.pow(temperature - 60, -0.1332047592)
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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/util/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 3d4e27c

Please sign in to comment.