Skip to content

Commit 5a2b338

Browse files
Rewrite color_gradient to always return a list of ManimColors (#4380)
Co-authored-by: Francisco Manríquez Novoa <[email protected]>
1 parent c4dc0ea commit 5a2b338

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

manim/mobject/graphing/probability.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ def get_division_along_dimension(
105105
p_list_complete = self.complete_p_list(p_list)
106106
colors_in_gradient = color_gradient(colors, len(p_list_complete))
107107

108-
assert isinstance(colors_in_gradient, list)
109-
110108
last_point = self.get_edge_center(-vect)
111109
parts = VGroup()
112110
for factor, color in zip(p_list_complete, colors_in_gradient):

manim/mobject/text/text_mobject.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -741,9 +741,6 @@ def _get_settings_from_gradient(
741741
settings.append(TextSetting(i, i + 1, **args))
742742

743743
for word, gradient in self.t2g.items():
744-
if isinstance(gradient, str) or len(gradient) == 1:
745-
color = gradient if isinstance(gradient, str) else gradient[0]
746-
gradient = [ManimColor(color)]
747744
colors = (
748745
color_gradient(gradient, len(word))
749746
if len(gradient) != 1

manim/utils/color/core.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
# logger = _config.logger
7070
import random
7171
import re
72-
from collections.abc import Sequence
72+
from collections.abc import Iterable, Sequence
7373
from typing import TypeVar, Union, overload
7474

7575
import numpy as np
@@ -1407,9 +1407,9 @@ def invert_color(color: ManimColorT) -> ManimColorT:
14071407

14081408

14091409
def color_gradient(
1410-
reference_colors: Sequence[ParsableManimColor],
1410+
reference_colors: Iterable[ParsableManimColor],
14111411
length_of_output: int,
1412-
) -> list[ManimColor] | ManimColor:
1412+
) -> list[ManimColor]:
14131413
"""Create a list of colors interpolated between the input array of colors with a
14141414
specific number of colors.
14151415
@@ -1422,20 +1422,25 @@ def color_gradient(
14221422
14231423
Returns
14241424
-------
1425-
list[ManimColor] | ManimColor
1426-
A :class:`ManimColor` or a list of interpolated :class:`ManimColor`'s.
1425+
list[ManimColor]
1426+
A list of interpolated :class:`ManimColor`'s.
14271427
"""
14281428
if length_of_output == 0:
1429-
return ManimColor(reference_colors[0])
1430-
if len(reference_colors) == 1:
1431-
return [ManimColor(reference_colors[0])] * length_of_output
1432-
rgbs = [color_to_rgb(color) for color in reference_colors]
1433-
alphas = np.linspace(0, (len(rgbs) - 1), length_of_output)
1429+
return []
1430+
parsed_colors = [ManimColor(color) for color in reference_colors]
1431+
num_colors = len(parsed_colors)
1432+
if num_colors == 0:
1433+
raise ValueError("Expected 1 or more reference colors. Got 0 colors.")
1434+
if num_colors == 1:
1435+
return parsed_colors * length_of_output
1436+
1437+
rgbs = [color.to_rgb() for color in parsed_colors]
1438+
alphas = np.linspace(0, (num_colors - 1), length_of_output)
14341439
floors = alphas.astype("int")
14351440
alphas_mod1 = alphas % 1
14361441
# End edge case
14371442
alphas_mod1[-1] = 1
1438-
floors[-1] = len(rgbs) - 2
1443+
floors[-1] = num_colors - 2
14391444
return [
14401445
rgb_to_color((rgbs[i] * (1 - alpha)) + (rgbs[i + 1] * alpha))
14411446
for i, alpha in zip(floors, alphas_mod1)

0 commit comments

Comments
 (0)