Skip to content

Rewrite color_gradient to always return a list of ManimColors #4380

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

Merged
merged 4 commits into from
Aug 11, 2025
Merged
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
2 changes: 0 additions & 2 deletions manim/mobject/graphing/probability.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 0 additions & 3 deletions manim/mobject/text/text_mobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 16 additions & 11 deletions manim/utils/color/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand All @@ -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)
Expand Down
Loading