Skip to content

Commit

Permalink
split the is_valid_rgb to two different functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MAfarrag committed Jul 20, 2024
1 parent 7609f25 commit 03b1eb8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
26 changes: 16 additions & 10 deletions cleopatra/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def get_type(self) -> List[str]:
>>> rgb_color = (0.5, 0.2, 0.8)
>>> color = Colors(rgb_color)
>>> print(color.get_type())
['rgb']
['rgb-normalized']
- Create a color object from an RGB color (values are between 0 and 255):
Expand All @@ -84,7 +84,9 @@ def get_type(self) -> List[str]:
"""
color_type = []
for color_i in self.color_value:
if self.is_valid_rgb_i(color_i):
if self.is_valid_rgb_norm(color_i):
color_type.append("rgb-normalized")
elif self.is_valid_rgb_255(color_i):
color_type.append("rgb")
elif self.is_valid_hex_i(color_i):
color_type.append("hex")
Expand Down Expand Up @@ -146,25 +148,29 @@ def is_valid_hex_i(hex_color: str) -> bool:
def is_valid_rgb(self) -> List[bool]:
"""is_valid_rgb.
is_valid_hex
Parameters
----------
Returns
-------
List[bool]
List of boolean values for each color
"""
return [self.is_valid_rgb_i(col) for col in self.color_value]
return [
self.is_valid_rgb_norm(col) or self.is_valid_rgb_255(col)
for col in self.color_value
]

@staticmethod
def is_valid_rgb_i(rgb_tuple: Any) -> bool:
def is_valid_rgb_255(rgb_tuple: Any) -> bool:
"""validate a single color whither it is rgb or not."""
if isinstance(rgb_tuple, tuple) and len(rgb_tuple) == 3:
if all(isinstance(value, int) for value in rgb_tuple):
return all(0 <= value <= 255 for value in rgb_tuple)
elif all(isinstance(value, float) for value in rgb_tuple):
return False

@staticmethod
def is_valid_rgb_norm(rgb_tuple: Any) -> bool:
"""validate a single color whither it is rgb or not."""
if isinstance(rgb_tuple, tuple) and len(rgb_tuple) == 3:
if all(isinstance(value, float) for value in rgb_tuple):
return all(0.0 <= value <= 1.0 for value in rgb_tuple)
return False

Expand Down
9 changes: 6 additions & 3 deletions tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ def test_get_type():
mixed_color = [(128, 51, 204), "#23a9dd", (0.5, 0.2, 0.8)]
color = Colors(mixed_color)
color_types = color.get_type()
assert color_types == ["rgb", "hex", "rgb"]
assert color_types == ["rgb", "hex", "rgb-normalized"]


def test__is_valid_rgb():
def test_is_valid_rgb_norm_255():
"""test_create_colors_object."""
rgb_color = (128, 51, 204)
color = Colors(rgb_color)
assert color.is_valid_rgb_i(rgb_color) is True
assert color.is_valid_rgb_255(rgb_color) is True
rgb_color = (0.5, 0.2, 0.8)
color = Colors(rgb_color)
assert color.is_valid_rgb_norm(rgb_color) is True


def test_is_valid_rgb():
Expand Down

0 comments on commit 03b1eb8

Please sign in to comment.