diff --git a/manim/mobject/graphing/probability.py b/manim/mobject/graphing/probability.py index 9b88179bdc..4604d3b603 100644 --- a/manim/mobject/graphing/probability.py +++ b/manim/mobject/graphing/probability.py @@ -105,8 +105,6 @@ def get_division_along_dimension( 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.py index c35f874ed5..42b57b4ae1 100644 --- a/manim/mobject/text/text_mobject.py +++ b/manim/mobject/text/text_mobject.py @@ -741,9 +741,6 @@ def _get_settings_from_gradient( 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) != 1 diff --git a/manim/utils/color/core.py b/manim/utils/color/core.py index af25992e59..62a93a1426 100644 --- a/manim/utils/color/core.py +++ b/manim/utils/color/core.py @@ -69,7 +69,7 @@ # logger = _config.logger import random import re -from collections.abc import Sequence +from collections.abc import Iterable, Sequence from typing import TypeVar, Union, overload import 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 a specific 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 case alphas_mod1[-1] = 1 - floors[-1] = len(rgbs) - 2 + floors[-1] = num_colors - 2 return [ rgb_to_color((rgbs[i] * (1 - alpha)) + (rgbs[i + 1] * alpha)) for i, alpha in zip(floors, alphas_mod1)