Skip to content

Commit 6499b86

Browse files
committed
added hsv support
1 parent d9c1d6f commit 6499b86

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

manim/utils/color/core.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
# logger = _config.logger
6+
import colorsys
67
import random
78
from typing import Any, Sequence, Union
89

@@ -177,6 +178,9 @@ def named_lines_group(length, colors, names, text_colors, align_to_block):
177178
RGBA_Array_Int: TypeAlias = "np.ndarray[Literal[4], np.dtype[ManimInt]]"
178179
RGBA_Tuple_Int: TypeAlias = "tuple[int, int, int, int]"
179180

181+
HSV_Array_Float: TypeAlias = RGB_Array_Float
182+
HSV_Tuple_Float: TypeAlias = RGB_Tuple_Float
183+
180184
ManimColorInternal: TypeAlias = "np.ndarray[Literal[4], np.dtype[ManimColorDType]]"
181185

182186
import re
@@ -355,6 +359,9 @@ def to_hex(self, with_alpha: bool = False) -> str:
355359
if with_alpha:
356360
tmp += f"{int(self._internal_value[3]*255):02X}"
357361
return tmp
362+
363+
def to_hsv(self) -> HSV_Array_Float:
364+
return colorsys.rgb_to_hsv(*self.to_rgb())
358365

359366
def invert(self, with_alpha=False) -> ManimColor:
360367
return ManimColor(1.0 - self._internal_value, with_alpha)
@@ -381,6 +388,15 @@ def from_rgba(
381388
@classmethod
382389
def from_hex(cls, hex: str, alpha: float = 1.0) -> ManimColor:
383390
return cls(hex, alpha)
391+
392+
@classmethod
393+
def from_hsv(
394+
cls,
395+
hsv: HSV_Array_Float | HSV_Tuple_Float,
396+
alpha: float = 1.0
397+
) -> ManimColor:
398+
rgb = colorsys.hsv_to_rgb(*hsv)
399+
return cls(rgb, alpha)
384400

385401
@classmethod
386402
def parse(

0 commit comments

Comments
 (0)