Skip to content

Commit 593010b

Browse files
thehugwizardSonnyRoomexpre-commit-ci[bot]JasonGrace2282
authored
Bugfix: HSL color ordering in ManimColor (#4202)
* Bugfix: colorsys uses HLS (hue, luminance, saturation) ordering when converting to/from rgb. Manim says it uses HSL ordering. Fixed order of parameters in call to colorsys for ManimColor.from_hsl and ManimColor.to_hsl. Added tests ensure correct HSL ordering. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update to fix the correct color tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: SonnyRoomex <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Aarush Deshpande <[email protected]>
1 parent 3c6a1ee commit 593010b

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

manim/utils/color/core.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ def to_hsl(self) -> HSL_Array_Float:
601601
HSL_Array_Float
602602
An HSL array of 3 floats from 0.0 to 1.0.
603603
"""
604-
return np.array(colorsys.rgb_to_hls(*self.to_rgb()))
604+
hls = colorsys.rgb_to_hls(*self.to_rgb())
605+
return np.array([hls[0], hls[2], hls[1]])
605606

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

912913
@overload

tests/module/utils/test_manim_color.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,19 @@ def test_to_hsv() -> None:
116116

117117
def test_to_hsl() -> None:
118118
color = ManimColor((0x1, 0x2, 0x3, 0x4))
119-
nt.assert_array_equal(
120-
color.to_hsl(), colorsys.rgb_to_hls(0x1 / 255, 0x2 / 255, 0x3 / 255)
121-
)
119+
hls = colorsys.rgb_to_hls(0x1 / 255, 0x2 / 255, 0x3 / 255)
120+
121+
nt.assert_array_equal(color.to_hsl(), np.array([hls[0], hls[2], hls[1]]))
122+
123+
124+
def test_from_hsl() -> None:
125+
hls = colorsys.rgb_to_hls(0x1 / 255, 0x2 / 255, 0x3 / 255)
126+
hsl = np.array([hls[0], hls[2], hls[1]])
127+
128+
color = ManimColor.from_hsl(hsl)
129+
rgb = np.array([0x1 / 255, 0x2 / 255, 0x3 / 255])
130+
131+
nt.assert_allclose(color.to_rgb(), rgb)
122132

123133

124134
def test_invert() -> None:

0 commit comments

Comments
 (0)