6969# logger = _config.logger
7070import random
7171import re
72- from collections .abc import Sequence
72+ from collections .abc import Iterable , Sequence
7373from typing import TypeVar , Union , overload
7474
7575import numpy as np
@@ -1407,9 +1407,9 @@ def invert_color(color: ManimColorT) -> ManimColorT:
14071407
14081408
14091409def 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