Skip to content

Commit

Permalink
Small improvement for RGB and HSB types
Browse files Browse the repository at this point in the history
  • Loading branch information
spacemanspiff2007 committed Dec 19, 2023
1 parent 2774657 commit 0d299ea
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 31 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ MyOpenhabRule()
- Added HABApp.util.RateLimiter
- Added CompressedMidnightRotatingFileHandler
- Updated dependencies
- Small improvement for RGB and HSB types

#### 23.11.0 (2023-11-23)
- Fix for very small float values (#425)
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
# -----------------------------------------------------------------------------
# Packages for source formatting
# -----------------------------------------------------------------------------
pre-commit >= 3.5, < 3.6
ruff >= 0.1.6, < 0.2
pre-commit >= 3.6, < 4
ruff >= 0.1.8, < 0.2

# -----------------------------------------------------------------------------
# Packages for other developement tasks
Expand Down
6 changes: 3 additions & 3 deletions requirements_setup.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
aiohttp == 3.9.1
pydantic == 2.5.2
msgspec == 0.18.4
msgspec == 0.18.5
pendulum == 2.1.2
bidict == 0.22.1
watchdog == 3.0.0
ujson == 5.8.0
ujson == 5.9.0
aiomqtt == 1.2.1

immutables == 0.20
Expand All @@ -15,7 +15,7 @@ colorama == 0.4.6

voluptuous == 0.14.1

typing-extensions == 4.8.0
typing-extensions == 4.9.0

aiohttp-sse-client == 0.2.1

Expand Down
2 changes: 1 addition & 1 deletion requirements_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
# -----------------------------------------------------------------------------
packaging == 23.2
pytest == 7.4.3
pytest-asyncio == 0.21.1
pytest-asyncio == 0.23.2
70 changes: 46 additions & 24 deletions src/HABApp/core/types/color.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from colorsys import hsv_to_rgb as _hsv_to_rgb
from colorsys import rgb_to_hsv as _rgb_to_hsv
from typing import Tuple, Union, Optional
from typing import Optional, Tuple, Union

from typing_extensions import Self

Expand Down Expand Up @@ -90,19 +90,30 @@ def __str__(self):
def __eq__(self, other):
if isinstance(other, self.__class__):
return self._r == other._r and self._g == other._g and self._b == other._b
elif isinstance(other, HSB):
if isinstance(other, HSB):
return self == self.__class__.from_hsb(other)
else:
return NotImplemented

def __getitem__(self, item: int) -> int:
if item == 0:
return self._r
if item == 1:
return self._g
if item == 2:
return self._b
raise IndexError()
return NotImplemented

def __getitem__(self, item: Union[int, str]) -> int:
if isinstance(item, int):
if item == 0:
return self._r
if item == 1:
return self._g
if item == 2:
return self._b
raise IndexError()

if isinstance(item, str):
if item in ('r', 'red'):
return self._r
if item in ('g', 'green'):
return self._g
if item in ('b', 'blue'):
return self._b
raise KeyError()

raise TypeError()

# ------------------------------------------------------------------------------------------------------------------
# Conversions
Expand Down Expand Up @@ -227,17 +238,28 @@ def __eq__(self, other):
return self._hue == other._hue and \
self._saturation == other._saturation and \
self._brightness == other._brightness
else:
return NotImplemented

def __getitem__(self, item: int) -> float:
if item == 0:
return self._hue
if item == 1:
return self._saturation
if item == 2:
return self._brightness
raise IndexError()
return NotImplemented

def __getitem__(self, item: Union[int, str]) -> float:
if isinstance(item, int):
if item == 0:
return self._hue
if item == 1:
return self._saturation
if item == 2:
return self._brightness
raise IndexError()

if isinstance(item, str):
if item in ('h', 'hue'):
return self._hue
if item in ('s', 'saturation'):
return self._saturation
if item in ('b', 'brightness'):
return self._brightness
raise KeyError()

raise TypeError()

# ------------------------------------------------------------------------------------------------------------------
# Conversions
Expand Down
18 changes: 17 additions & 1 deletion tests/test_core/test_types/test_color.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from HABApp.core.types.color import RGB, HSB
from HABApp.core.types.color import HSB, RGB


def test_rgb():
Expand All @@ -10,8 +10,16 @@ def test_rgb():
assert rgb.b == rgb.blue == 3

assert rgb[0] == 1
assert rgb['r'] == 1
assert rgb['red'] == 1

assert rgb[1] == 2
assert rgb['g'] == 2
assert rgb['green'] == 2

assert rgb[2] == 3
assert rgb['b'] == 3
assert rgb['blue'] == 3

r, g, b = rgb
assert r == rgb.r
Expand Down Expand Up @@ -69,8 +77,16 @@ def test_hsb():
assert hsb.b == hsb.brightness == 3

assert hsb[0] == 1
assert hsb['h'] == 1
assert hsb['hue'] == 1

assert hsb[1] == 2
assert hsb['s'] == 2
assert hsb['saturation'] == 2

assert hsb[2] == 3
assert hsb['b'] == 3
assert hsb['brightness'] == 3

h, s, b = hsb
assert h == hsb.h
Expand Down

0 comments on commit 0d299ea

Please sign in to comment.