Skip to content

Commit 03b1eb8

Browse files
committed
split the is_valid_rgb to two different functions
1 parent 7609f25 commit 03b1eb8

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

cleopatra/colors.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def get_type(self) -> List[str]:
7373
>>> rgb_color = (0.5, 0.2, 0.8)
7474
>>> color = Colors(rgb_color)
7575
>>> print(color.get_type())
76-
['rgb']
76+
['rgb-normalized']
7777
7878
- Create a color object from an RGB color (values are between 0 and 255):
7979
@@ -84,7 +84,9 @@ def get_type(self) -> List[str]:
8484
"""
8585
color_type = []
8686
for color_i in self.color_value:
87-
if self.is_valid_rgb_i(color_i):
87+
if self.is_valid_rgb_norm(color_i):
88+
color_type.append("rgb-normalized")
89+
elif self.is_valid_rgb_255(color_i):
8890
color_type.append("rgb")
8991
elif self.is_valid_hex_i(color_i):
9092
color_type.append("hex")
@@ -146,25 +148,29 @@ def is_valid_hex_i(hex_color: str) -> bool:
146148
def is_valid_rgb(self) -> List[bool]:
147149
"""is_valid_rgb.
148150
149-
is_valid_hex
150-
151-
Parameters
152-
----------
153-
154151
Returns
155152
-------
156153
List[bool]
157154
List of boolean values for each color
158155
"""
159-
return [self.is_valid_rgb_i(col) for col in self.color_value]
156+
return [
157+
self.is_valid_rgb_norm(col) or self.is_valid_rgb_255(col)
158+
for col in self.color_value
159+
]
160160

161161
@staticmethod
162-
def is_valid_rgb_i(rgb_tuple: Any) -> bool:
162+
def is_valid_rgb_255(rgb_tuple: Any) -> bool:
163163
"""validate a single color whither it is rgb or not."""
164164
if isinstance(rgb_tuple, tuple) and len(rgb_tuple) == 3:
165165
if all(isinstance(value, int) for value in rgb_tuple):
166166
return all(0 <= value <= 255 for value in rgb_tuple)
167-
elif all(isinstance(value, float) for value in rgb_tuple):
167+
return False
168+
169+
@staticmethod
170+
def is_valid_rgb_norm(rgb_tuple: Any) -> bool:
171+
"""validate a single color whither it is rgb or not."""
172+
if isinstance(rgb_tuple, tuple) and len(rgb_tuple) == 3:
173+
if all(isinstance(value, float) for value in rgb_tuple):
168174
return all(0.0 <= value <= 1.0 for value in rgb_tuple)
169175
return False
170176

tests/test_colors.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ def test_get_type():
2020
mixed_color = [(128, 51, 204), "#23a9dd", (0.5, 0.2, 0.8)]
2121
color = Colors(mixed_color)
2222
color_types = color.get_type()
23-
assert color_types == ["rgb", "hex", "rgb"]
23+
assert color_types == ["rgb", "hex", "rgb-normalized"]
2424

2525

26-
def test__is_valid_rgb():
26+
def test_is_valid_rgb_norm_255():
2727
"""test_create_colors_object."""
2828
rgb_color = (128, 51, 204)
2929
color = Colors(rgb_color)
30-
assert color.is_valid_rgb_i(rgb_color) is True
30+
assert color.is_valid_rgb_255(rgb_color) is True
31+
rgb_color = (0.5, 0.2, 0.8)
32+
color = Colors(rgb_color)
33+
assert color.is_valid_rgb_norm(rgb_color) is True
3134

3235

3336
def test_is_valid_rgb():

0 commit comments

Comments
 (0)