[O] Refactor HSL and RGB
This commit is contained in:
parent
330de73df3
commit
101d798c0a
1 changed files with 28 additions and 8 deletions
|
|
@ -1,8 +1,7 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import colorsys
|
import colorsys
|
||||||
from typing import NamedTuple, Callable, Optional
|
from dataclasses import dataclass, astuple
|
||||||
|
|
||||||
from typing_extensions import Literal
|
from typing_extensions import Literal
|
||||||
|
|
||||||
from .constants import GLOBAL_CFG
|
from .constants import GLOBAL_CFG
|
||||||
|
|
@ -84,11 +83,28 @@ def redistribute_rgb(r: int, g: int, b: int) -> tuple[int, int, int]:
|
||||||
return int(gray + x * r), int(gray + x * g), int(gray + x * b)
|
return int(gray + x * r), int(gray + x * g), int(gray + x * b)
|
||||||
|
|
||||||
|
|
||||||
class RGB(NamedTuple):
|
@dataclass
|
||||||
|
class HSL:
|
||||||
|
h: float
|
||||||
|
s: float
|
||||||
|
l: float
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(astuple(self))
|
||||||
|
|
||||||
|
def rgb(self) -> RGB:
|
||||||
|
return RGB(*[round(v * 255.0) for v in colorsys.hls_to_rgb(self.h, self.l, self.s)])
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class RGB:
|
||||||
r: int
|
r: int
|
||||||
g: int
|
g: int
|
||||||
b: int
|
b: int
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(astuple(self))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_hex(cls, hex: str) -> "RGB":
|
def from_hex(cls, hex: str) -> "RGB":
|
||||||
"""
|
"""
|
||||||
|
|
@ -175,6 +191,10 @@ class RGB(NamedTuple):
|
||||||
"""
|
"""
|
||||||
return RGB(*redistribute_rgb(*[v * multiplier for v in self]))
|
return RGB(*redistribute_rgb(*[v * multiplier for v in self]))
|
||||||
|
|
||||||
|
def hsl(self) -> HSL:
|
||||||
|
h, l, s = colorsys.rgb_to_hls(*[v / 255.0 for v in self])
|
||||||
|
return HSL(h, s, l)
|
||||||
|
|
||||||
def set_light(self, light: float, at_least: bool | None = None, at_most: bool | None = None) -> 'RGB':
|
def set_light(self, light: float, at_least: bool | None = None, at_most: bool | None = None) -> 'RGB':
|
||||||
"""
|
"""
|
||||||
Set HSL lightness value
|
Set HSL lightness value
|
||||||
|
|
@ -185,16 +205,16 @@ class RGB(NamedTuple):
|
||||||
:return: New color (original isn't modified)
|
:return: New color (original isn't modified)
|
||||||
"""
|
"""
|
||||||
# Convert to HSL
|
# Convert to HSL
|
||||||
h, l, s = colorsys.rgb_to_hls(*[v / 255.0 for v in self])
|
hsl = self.hsl()
|
||||||
|
|
||||||
# Modify light value
|
# Modify light value
|
||||||
if at_least is None and at_most is None:
|
if at_least is None and at_most is None:
|
||||||
l = light
|
hsl.l = light
|
||||||
else:
|
else:
|
||||||
if at_most:
|
if at_most:
|
||||||
l = min(l, light)
|
hsl.l = min(hsl.l, light)
|
||||||
if at_least:
|
if at_least:
|
||||||
l = max(l, light)
|
hsl.l = max(hsl.l, light)
|
||||||
|
|
||||||
# Convert back to RGB
|
# Convert back to RGB
|
||||||
return RGB(*[round(v * 255.0) for v in colorsys.hls_to_rgb(h, l, s)])
|
return hsl.rgb()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue