Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: HSL color ordering in ManimColor #4202

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions manim/utils/color/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,8 @@ def to_hsl(self) -> HSL_Array_Float:
HSL_Array_Float
An HSL array of 3 floats from 0.0 to 1.0.
"""
return np.array(colorsys.rgb_to_hls(*self.to_rgb()))
hls = colorsys.rgb_to_hls(*self.to_rgb())
return np.array([hls[0], hls[2], hls[1]])

def invert(self, with_alpha: bool = False) -> Self:
"""Return a new, linearly inverted version of this :class:`ManimColor` (no
Expand Down Expand Up @@ -906,7 +907,7 @@ def from_hsl(
The :class:`ManimColor` with the corresponding RGB values to the given HSL
array.
"""
rgb = colorsys.hls_to_rgb(*hsl)
rgb = colorsys.hls_to_rgb(hsl[0], hsl[2], hsl[1])
return cls._from_internal(ManimColor(rgb, alpha)._internal_value)

@overload
Expand Down
16 changes: 13 additions & 3 deletions tests/module/utils/test_manim_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,19 @@ def test_to_hsv() -> None:

def test_to_hsl() -> None:
color = ManimColor((0x1, 0x2, 0x3, 0x4))
nt.assert_array_equal(
color.to_hsl(), colorsys.rgb_to_hls(0x1 / 255, 0x2 / 255, 0x3 / 255)
)
hls = colorsys.rgb_to_hls(0x1 / 255, 0x2 / 255, 0x3 / 255)

nt.assert_array_equal(color.to_hsl(), np.array([hls[0], hls[2], hls[1]]))


def test_from_hsl() -> None:
hls = colorsys.rgb_to_hls(0x1 / 255, 0x2 / 255, 0x3 / 255)
hsl = np.array([hls[0], hls[2], hls[1]])

color = ManimColor.from_hsl(hsl)
rgb = np.array([0x1 / 255, 0x2 / 255, 0x3 / 255])

nt.assert_allclose(color.to_rgb(), rgb)


def test_invert() -> None:
Expand Down