Refactored Theme class to correspond SOLID patterns

This commit is contained in:
Vladyslav Hroshev
2025-04-10 18:29:19 +03:00
parent 31e1a3deb9
commit 40a88cb7f4
26 changed files with 534 additions and 252 deletions

View File

@@ -0,0 +1,42 @@
import colorsys
from abc import ABC, abstractmethod
class ColorConverter(ABC):
@staticmethod
@abstractmethod
def hex_to_rgba(hex_color):
pass
@staticmethod
@abstractmethod
def hsl_to_rgb(hue, saturation, lightness):
pass
class ColorConverterImpl(ColorConverter):
@staticmethod
def hex_to_rgba(hex_color):
try:
if len(hex_color) in range(6, 10):
hex_color = hex_color.lstrip('#') + "ff"
# if is convertable
int(hex_color[:], 16)
else:
raise ValueError
except ValueError:
raise ValueError(f'Error: Invalid HEX color code: {hex_color}')
else:
return int(hex_color[0:2], 16), \
int(hex_color[2:4], 16), \
int(hex_color[4:6], 16), \
int(hex_color[6:8], 16) / 255
@staticmethod
def hsl_to_rgb(hue, saturation, lightness):
# colorsys works in range(0, 1)
h = hue / 360
red, green, blue = [int(item * 255) for item in colorsys.hls_to_rgb(h, lightness, saturation)]
return red, green, blue