Skip to content

Commit

Permalink
rename the hex_color attribute to color_value
Browse files Browse the repository at this point in the history
  • Loading branch information
MAfarrag committed Jul 20, 2024
1 parent bd1da7a commit 72d1472
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
65 changes: 54 additions & 11 deletions cleopatra/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,50 @@
class Colors:
"""Colors class for Cleopatra."""

def __init__(self, hex_color: Union[List[str], str]):
def __init__(
self,
color_value: Union[List[str], str, Tuple[float, int], List[Tuple[float, int]]],
):
"""
Parameters
----------
color_value: List[str]/Tuple[float, float]/str.
the color value could be a list of hex colors, a tuple of RGB values, or a single hex/RGB color.
"""
# convert the hex color to a list if it is a string
if isinstance(hex_color, str):
hex_color = [hex_color]
if isinstance(color_value, str) or isinstance(color_value, tuple):
color_value = [color_value]
elif not isinstance(color_value, list):
raise ValueError(

Check warning on line 23 in cleopatra/colors.py

View check run for this annotation

Codecov / codecov/patch

cleopatra/colors.py#L23

Added line #L23 was not covered by tests
"The color_value must be a list of hex colors, list of tuples (RGB color), a single hex "
"or single RGB tuple color."
)

self._color_value = color_value

@property
def color_value(self) -> List[str]:
"""Color values given by the user.
Returns
-------
List[str]
"""
return self._color_value

@property
def hex_color(self) -> List[str]:
"""hex_color.
self.hex_color = hex_color
Parameters
----------
Returns
-------
List[str]
"""
return self._hex_color

Check warning on line 51 in cleopatra/colors.py

View check run for this annotation

Codecov / codecov/patch

cleopatra/colors.py#L51

Added line #L51 was not covered by tests

def is_valid_hex(self) -> List[bool]:
"""is_valid_hex.
Expand All @@ -24,25 +62,30 @@ def is_valid_hex(self) -> List[bool]:
-------
"""
return [True if mcolors.is_color_like(col) else False for col in self.hex_color]
return [
True if mcolors.is_color_like(col) else False for col in self.color_value
]

def to_rgb(self, normalized: bool = True) -> List[Tuple[int, int]]:
def to_rgb(
self, normalized: bool = True
) -> List[Tuple[Union[int, float], Union[int, float]]]:
"""get_rgb.
Parameters
----------
normalized: [int]
True if you want the RGB values to be scaled between 0 and 1, .Default is True.
normalized: int, Default is True.
True if you want the RGB values to be scaled between 0 and 1. False if you want the RGB values to be scaled
between 0 and 255.
Returns
-------
List[Tuples
List[Tuples]
"""
if normalized == 1:
rgb = [mcolors.to_rgb(col) for col in self.hex_color]
rgb = [mcolors.to_rgb(col) for col in self.color_value]
else:
rgb = [
tuple([int(c * 255) for c in mcolors.to_rgb(col)])
for col in self.hex_color
for col in self.color_value
]
return rgb
19 changes: 13 additions & 6 deletions tests/test_colors.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from cleopatra.colors import Colors


def test_create_colors_object():
"""test_create_colors_object."""
hex_number = "ff0000"
color = Colors(hex_number)
assert color.hex_color == [hex_number]
class TestCreateColors:
def test_create_from_hex(self):
"""test_create_colors_object."""
hex_number = "ff0000"
color = Colors(hex_number)
assert color._color_value == [hex_number]

def test_create_from_rgb(self):
"""test_create_colors_object."""
hex_number = (128, 51, 204)
color = Colors(hex_number)
assert color._color_value == [hex_number]


def test_is_valid():
Expand All @@ -16,7 +23,7 @@ def test_is_valid():
assert valid == [False, True]


def test_get_rgb():
def test_to_rgb():
"""test_create_colors_object."""
hex_number = ["#ff0000", "#23a9dd"]
color = Colors(hex_number)
Expand Down

0 comments on commit 72d1472

Please sign in to comment.