Skip to content

Commit

Permalink
update color builder methods to accept coordinates as direct arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
apparebit committed May 25, 2024
1 parent 3e52ce9 commit 143d25d
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions prettypretty/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from typing import cast, overload, Self, TypeAlias

from .ansi import Ansi, DEFAULT_COLOR, is_default, Layer
from .color.spec import ColorSpec, CoordinateSpec
from .fidelity import Fidelity
from .color.spec import ColorSpec
from .fidelity import Fidelity, FidelityTag


class TextAttribute(enum.Enum):
Expand Down Expand Up @@ -290,29 +290,33 @@ def hidden(self) -> Self:
def _use_color(
self,
color: int | ColorSpec | str,
coordinates: None | CoordinateSpec,
c1: None | float,
c2: None | float,
c3: None | float,
) -> ColorSpec:
if isinstance(color, int):
return ColorSpec('ansi' if color <= 15 else 'eight_bit', (color,))
elif isinstance(color, str):
assert coordinates is not None
return ColorSpec(color, coordinates)
else:
elif isinstance(color, ColorSpec):
return color

assert c1 is not None and c2 is not None and c3 is not None
return ColorSpec(color, (c1, c2, c3))

@overload
def fg(self, color: int, /) -> Self:
...
@overload
def fg(self, color: ColorSpec, /) -> Self:
...
@overload
def fg(self, tag: str, coordinates: CoordinateSpec, /) -> Self:
def fg(self, tag: str, c1: float, c2: float, c3: float, /) -> Self:
...
def fg(
self,
color: int | ColorSpec | str,
coordinates: None | CoordinateSpec = None
c1: None | float = None,
c2: None | float = None,
c3: None | float = None,
) -> Self:
"""
Set the foreground color.
Expand All @@ -324,7 +328,7 @@ def fg(
coordinates of color specifications. Otherwise, there are no
restrictions on valid colors.
"""
return dataclasses.replace(self, foreground=self._use_color(color, coordinates))
return dataclasses.replace(self, foreground=self._use_color(color, c1, c2, c3))

@overload
def bg(self, color: int, /) -> Self:
Expand All @@ -333,12 +337,14 @@ def bg(self, color: int, /) -> Self:
def bg(self, color: ColorSpec, /) -> Self:
...
@overload
def bg(self, tag: str, coordinates: CoordinateSpec, /) -> Self:
def bg(self, tag: str, c1: float, c2: float, c3: float, /) -> Self:
...
def bg(
self,
color: int | ColorSpec | str,
coordinates: None | CoordinateSpec = None
c1: None | float = None,
c2: None | float = None,
c3: None | float = None,
) -> Self:
"""
Set the background color.
Expand All @@ -350,12 +356,13 @@ def bg(
coordinates of color specifications. Otherwise, there are no
restrictions on valid colors.
"""
return dataclasses.replace(self, background=self._use_color(color, coordinates))
return dataclasses.replace(self, background=self._use_color(color, c1, c2, c3))

def prepare(self, fidelity: Fidelity) -> Self:
def prepare(self, fidelity: Fidelity | FidelityTag) -> Self:
"""
Adjust this style specification for rendering with the given fidelity.
"""
fidelity = Fidelity.from_tag(fidelity)
if self.fidelity is not None and self.fidelity <= fidelity:
return self

Expand Down

0 comments on commit 143d25d

Please sign in to comment.