Skip to content

Commit

Permalink
support tuple rgb
Browse files Browse the repository at this point in the history
  • Loading branch information
faissaloux committed Jul 8, 2024
1 parent a3a58a9 commit f66b9cf
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 18 deletions.
6 changes: 6 additions & 0 deletions termspark/helpers/list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Sequence, Union

from .rgb import RGB


class List:
def snake(self, elements: Sequence[Union[str, Sequence[str]]]) -> Sequence[str]:
Expand All @@ -9,6 +11,10 @@ def snake(self, elements: Sequence[Union[str, Sequence[str]]]) -> Sequence[str]:
if isinstance(elem, list):
snakeElements.insert(index, self.snake(elem)) # type: ignore
else:
if RGB.check(elem):
snakeElements.insert(index, elem) # type: ignore
continue

snakeElements.insert(index, elem.replace(" ", "_") if elem else elem) # type: ignore

return snakeElements
31 changes: 31 additions & 0 deletions termspark/helpers/rgb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import re
from typing import Sequence, Union


class RGB:
@staticmethod
def check(color: Union[str, Sequence[str]]) -> bool:
if color is None:
return False

if type(color) == tuple:
if len(color) != 3:
return False

color = RGB.to_str(color)

assert type(color) == str
regex = r"(\d+),\s*(\d+),\s*(\d+)"
match = re.match(regex, color)

if match is None:
return False

return all(0 <= int(group) <= 255 for group in match.groups())

@staticmethod
def to_str(color: Union[str, tuple]) -> str:
if type(color) == str:
return color

return ",".join([str(comp) for comp in color])
32 changes: 14 additions & 18 deletions termspark/painter/painter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re
from typing import Final, Type
from typing import Final, Type, Union

from ..helpers.rgb import RGB
from .constants.color import Color
from .constants.fore import Fore
from .constants.highlight import Highlight
Expand All @@ -10,27 +10,23 @@ class Painter:
SUFFIX: Final[str] = "m"
RESET: Final[str] = "\x1b[0m"

def __paint(self, color: str, type: Type[Color]) -> str:
if hasattr(type, color.upper()):
color = getattr(type, color.upper())
def __paint(self, color: Union[str, tuple], kind: Type[Color]) -> str:
if type(color) == tuple:
color = RGB.to_str(color)

assert type(color) == str
if hasattr(kind, color.upper()):
color = getattr(kind, color.upper())

assert type(color) == str
color = color.replace("_", "")
if color and self.__is_rgb(color):
return f"{type.PREFIX}{color.replace(',', ';')}{self.SUFFIX}"
if color and RGB.check(color):
return f"{kind.PREFIX}{color.replace(',', ';')}{self.SUFFIX}"

return ""

def paint_color(self, color: str) -> str:
def paint_color(self, color: Union[str, tuple]) -> str:
return self.__paint(color, Fore)

def paint_highlight(self, highlight: str) -> str:
def paint_highlight(self, highlight: Union[str, tuple]) -> str:
return self.__paint(highlight, Highlight)

def __is_rgb(self, color: str) -> bool:
regex = r"(\d+),\s*(\d+),\s*(\d+)"
match = re.match(regex, color)

if match is None:
return False

return all(0 <= int(group) <= 255 for group in match.groups())
12 changes: 12 additions & 0 deletions tests/painter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ def test_can_paint_rgb_color(self):

assert paint_color == f"{Fore.PREFIX}255;255;255{painter.SUFFIX}"

def test_can_paint_tuple_rgb_color(self):
painter = Painter()
paint_color = painter.paint_color((255, 255, 255))

assert paint_color == f"{Fore.PREFIX}255;255;255{painter.SUFFIX}"

def test_allow_spaces_on_rgb_color(self):
painter = Painter()
paint_color = painter.paint_color("_255,_255,_____255_")
Expand All @@ -72,6 +78,12 @@ def test_can_paint_rgb_highlight(self):

assert paint_highlight == f"{Highlight.PREFIX}36;114;200{painter.SUFFIX}"

def test_can_paint_tuple_rgb_highlight(self):
painter = Painter()
paint_highlight = painter.paint_highlight((36, 114, 200))

assert paint_highlight == f"{Highlight.PREFIX}36;114;200{painter.SUFFIX}"

def test_allow_spaces_rgb_highlight(self):
painter = Painter()
paint_highlight = painter.paint_highlight("___36___,___114__,_200__")
Expand Down
17 changes: 17 additions & 0 deletions tests/rgb_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from termspark.helpers.rgb import RGB


class TestRGB:
def test_check(self):
assert RGB.check("36,114,200")
assert RGB.check((36, 114, 200))
assert RGB.check(None) == False
assert RGB.check("") == False
assert RGB.check((36, 114)) == False
assert RGB.check((36, 114, 200, 114)) == False
assert RGB.check((36, 114, 256)) == False
assert RGB.check("36,114") == False

def test_to_str(self):
assert RGB.to_str("36,114,200") == "36,114,200"
assert RGB.to_str((36, 114, 200)) == "36,114,200"

0 comments on commit f66b9cf

Please sign in to comment.