Skip to content

Commit

Permalink
Move new and _handle_color_input to a classmethod
Browse files Browse the repository at this point in the history
New has no need to have any knowledge of the current color and simply
instantiates a new color, it should be a classmethod

Internal `_handle_color_input` has no need to be an instance method
either.
  • Loading branch information
facelessuser committed Sep 5, 2024
1 parent 2427f7b commit a3b0f03
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions coloraide/color.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,13 +561,14 @@ def is_nan(self, name: str) -> bool: # pragma: no cover

return math.isnan(self.get(name))

def _handle_color_input(self, color: ColorInput) -> Color:
@classmethod
def _handle_color_input(cls, color: ColorInput) -> Color:
"""Handle color input."""

if isinstance(color, (str, Mapping)):
return self.new(color)
elif self._is_color(color):
return color if self._is_this_color(color) else self.new(color)
return cls.new(color)
elif cls._is_color(color):
return color if cls._is_this_color(color) else cls.new(color)
else:
raise TypeError(f"Unexpected type '{type(color)}'")

Expand All @@ -576,16 +577,17 @@ def space(self) -> str:

return self._space.NAME

@classmethod
def new(
self,
cls,
color: ColorInput,
data: VectorLike | None = None,
alpha: float = util.DEF_ALPHA,
**kwargs: Any
) -> Color:
"""Create new color object."""

return type(self)(color, data, alpha, **kwargs)
return cls(color, data, alpha, **kwargs)

def clone(self) -> Color:
"""Clone."""
Expand Down

0 comments on commit a3b0f03

Please sign in to comment.