A community-maintained Python framework for creating mathematical animations.
Rewrite `color_gradient` to always return a list of ManimColors (#4380)
Henrik Skov Midtiby5a2b3384f1d3
diff --git a/manim/mobject/graphing/probability.py b/manim/mobject/graphing/probability.pyindex 9b88179bd..4604d3b60 100644--- a/manim/mobject/graphing/probability.py+++ b/manim/mobject/graphing/probability.py@@ -105,8 +105,6 @@ class SampleSpace(Rectangle):p_list_complete = self.complete_p_list(p_list)colors_in_gradient = color_gradient(colors, len(p_list_complete))- assert isinstance(colors_in_gradient, list)-last_point = self.get_edge_center(-vect)parts = VGroup()for factor, color in zip(p_list_complete, colors_in_gradient):diff --git a/manim/mobject/text/text_mobject.py b/manim/mobject/text/text_mobject.pyindex c35f874ed..42b57b4ae 100644--- a/manim/mobject/text/text_mobject.py+++ b/manim/mobject/text/text_mobject.py@@ -741,9 +741,6 @@ class Text(SVGMobject):settings.append(TextSetting(i, i + 1, **args))for word, gradient in self.t2g.items():- if isinstance(gradient, str) or len(gradient) == 1:- color = gradient if isinstance(gradient, str) else gradient[0]- gradient = [ManimColor(color)]colors = (color_gradient(gradient, len(word))if len(gradient) != 1diff --git a/manim/utils/color/core.py b/manim/utils/color/core.pyindex af25992e5..62a93a142 100644--- a/manim/utils/color/core.py+++ b/manim/utils/color/core.py@@ -69,7 +69,7 @@ import colorsys# logger = _config.loggerimport randomimport re-from collections.abc import Sequence+from collections.abc import Iterable, Sequencefrom typing import TypeVar, Union, overloadimport numpy as np@@ -1407,9 +1407,9 @@ def invert_color(color: ManimColorT) -> ManimColorT:def color_gradient(- reference_colors: Sequence[ParsableManimColor],+ reference_colors: Iterable[ParsableManimColor],length_of_output: int,-) -> list[ManimColor] | ManimColor:+) -> list[ManimColor]:"""Create a list of colors interpolated between the input array of colors with aspecific number of colors.@@ -1422,20 +1422,25 @@ def color_gradient(Returns-------- list[ManimColor] | ManimColor- A :class:`ManimColor` or a list of interpolated :class:`ManimColor`'s.+ list[ManimColor]+ A list of interpolated :class:`ManimColor`'s."""if length_of_output == 0:- return ManimColor(reference_colors[0])- if len(reference_colors) == 1:- return [ManimColor(reference_colors[0])] * length_of_output- rgbs = [color_to_rgb(color) for color in reference_colors]- alphas = np.linspace(0, (len(rgbs) - 1), length_of_output)+ return []+ parsed_colors = [ManimColor(color) for color in reference_colors]+ num_colors = len(parsed_colors)+ if num_colors == 0:+ raise ValueError("Expected 1 or more reference colors. Got 0 colors.")+ if num_colors == 1:+ return parsed_colors * length_of_output++ rgbs = [color.to_rgb() for color in parsed_colors]+ alphas = np.linspace(0, (num_colors - 1), length_of_output)floors = alphas.astype("int")alphas_mod1 = alphas % 1# End edge casealphas_mod1[-1] = 1- floors[-1] = len(rgbs) - 2+ floors[-1] = num_colors - 2return [rgb_to_color((rgbs[i] * (1 - alpha)) + (rgbs[i + 1] * alpha))for i, alpha in zip(floors, alphas_mod1)