Skip to content

Commit 72d1472

Browse files
committed
rename the hex_color attribute to color_value
1 parent bd1da7a commit 72d1472

File tree

2 files changed

+67
-17
lines changed

2 files changed

+67
-17
lines changed

cleopatra/colors.py

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,50 @@
55
class Colors:
66
"""Colors class for Cleopatra."""
77

8-
def __init__(self, hex_color: Union[List[str], str]):
8+
def __init__(
9+
self,
10+
color_value: Union[List[str], str, Tuple[float, int], List[Tuple[float, int]]],
11+
):
12+
"""
13+
14+
Parameters
15+
----------
16+
color_value: List[str]/Tuple[float, float]/str.
17+
the color value could be a list of hex colors, a tuple of RGB values, or a single hex/RGB color.
18+
"""
919
# convert the hex color to a list if it is a string
10-
if isinstance(hex_color, str):
11-
hex_color = [hex_color]
20+
if isinstance(color_value, str) or isinstance(color_value, tuple):
21+
color_value = [color_value]
22+
elif not isinstance(color_value, list):
23+
raise ValueError(
24+
"The color_value must be a list of hex colors, list of tuples (RGB color), a single hex "
25+
"or single RGB tuple color."
26+
)
27+
28+
self._color_value = color_value
29+
30+
@property
31+
def color_value(self) -> List[str]:
32+
"""Color values given by the user.
33+
34+
Returns
35+
-------
36+
List[str]
37+
"""
38+
return self._color_value
39+
40+
@property
41+
def hex_color(self) -> List[str]:
42+
"""hex_color.
1243
13-
self.hex_color = hex_color
44+
Parameters
45+
----------
46+
47+
Returns
48+
-------
49+
List[str]
50+
"""
51+
return self._hex_color
1452

1553
def is_valid_hex(self) -> List[bool]:
1654
"""is_valid_hex.
@@ -24,25 +62,30 @@ def is_valid_hex(self) -> List[bool]:
2462
-------
2563
2664
"""
27-
return [True if mcolors.is_color_like(col) else False for col in self.hex_color]
65+
return [
66+
True if mcolors.is_color_like(col) else False for col in self.color_value
67+
]
2868

29-
def to_rgb(self, normalized: bool = True) -> List[Tuple[int, int]]:
69+
def to_rgb(
70+
self, normalized: bool = True
71+
) -> List[Tuple[Union[int, float], Union[int, float]]]:
3072
"""get_rgb.
3173
3274
Parameters
3375
----------
34-
normalized: [int]
35-
True if you want the RGB values to be scaled between 0 and 1, .Default is True.
76+
normalized: int, Default is True.
77+
True if you want the RGB values to be scaled between 0 and 1. False if you want the RGB values to be scaled
78+
between 0 and 255.
3679
3780
Returns
3881
-------
39-
List[Tuples
82+
List[Tuples]
4083
"""
4184
if normalized == 1:
42-
rgb = [mcolors.to_rgb(col) for col in self.hex_color]
85+
rgb = [mcolors.to_rgb(col) for col in self.color_value]
4386
else:
4487
rgb = [
4588
tuple([int(c * 255) for c in mcolors.to_rgb(col)])
46-
for col in self.hex_color
89+
for col in self.color_value
4790
]
4891
return rgb

tests/test_colors.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
from cleopatra.colors import Colors
22

33

4-
def test_create_colors_object():
5-
"""test_create_colors_object."""
6-
hex_number = "ff0000"
7-
color = Colors(hex_number)
8-
assert color.hex_color == [hex_number]
4+
class TestCreateColors:
5+
def test_create_from_hex(self):
6+
"""test_create_colors_object."""
7+
hex_number = "ff0000"
8+
color = Colors(hex_number)
9+
assert color._color_value == [hex_number]
10+
11+
def test_create_from_rgb(self):
12+
"""test_create_colors_object."""
13+
hex_number = (128, 51, 204)
14+
color = Colors(hex_number)
15+
assert color._color_value == [hex_number]
916

1017

1118
def test_is_valid():
@@ -16,7 +23,7 @@ def test_is_valid():
1623
assert valid == [False, True]
1724

1825

19-
def test_get_rgb():
26+
def test_to_rgb():
2027
"""test_create_colors_object."""
2128
hex_number = ["#ff0000", "#23a9dd"]
2229
color = Colors(hex_number)

0 commit comments

Comments
 (0)