From c2a3e5bd72826dc5240bde7b2fbfd01f0172c55a Mon Sep 17 00:00:00 2001 From: Manitej Boorgu Date: Fri, 7 Aug 2020 03:55:03 +0000 Subject: [PATCH] added in hsv and hls support as classmethods using colorsys --- src/replit/termutils.py | 45 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/replit/termutils.py b/src/replit/termutils.py index 1102a88..446b712 100644 --- a/src/replit/termutils.py +++ b/src/replit/termutils.py @@ -1,5 +1,5 @@ """Pure Python ANSI Color Escape Code generator.""" - +import colorsys def clear() -> None: """Clear the terminal.""" @@ -56,6 +56,49 @@ class color: return cls(r, g, b) + @classmethod + def hsv(cls, h, s, v) -> None: + """ + Convert Hex Value to RGB + then generate an ANSI escape code + + Args: + hexvalue (str): The color's hex value + + Raises: + ValueError + + Returns: + color : RGB colors from Hex Value + """ + try: + r, g, b = colorsys.hsv_to_rgb(h, s, v) + except: + raise ValueError('Converting HSV to RGB ran into an error') + + return cls(r, g, b) + + @classmethod + def hls(cls, h, l, s) + """ + Convert Hex Value to RGB + then generate an ANSI escape code + + Args: + hexvalue (str): The color's hex value + + Raises: + ValueError + + Returns: + color : RGB colors from Hex Value + """ + try: + r, g, b = colorsys.hls_to_rgb(h, s, v) + except: + raise ValueError('Converting HLS to RGB ran into an error') + + return cls(r, g, b) class bit: def __init__(self, value: int) -> None: