69
69
# logger = _config.logger
70
70
import random
71
71
import re
72
- from collections .abc import Sequence
72
+ from collections .abc import Iterable , Sequence
73
73
from typing import TypeVar , Union , overload
74
74
75
75
import numpy as np
@@ -1407,9 +1407,9 @@ def invert_color(color: ManimColorT) -> ManimColorT:
1407
1407
1408
1408
1409
1409
def color_gradient (
1410
- reference_colors : Sequence [ParsableManimColor ],
1410
+ reference_colors : Iterable [ParsableManimColor ],
1411
1411
length_of_output : int ,
1412
- ) -> list [ManimColor ] | ManimColor :
1412
+ ) -> list [ManimColor ]:
1413
1413
"""Create a list of colors interpolated between the input array of colors with a
1414
1414
specific number of colors.
1415
1415
@@ -1422,20 +1422,25 @@ def color_gradient(
1422
1422
1423
1423
Returns
1424
1424
-------
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.
1427
1427
"""
1428
1428
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 )
1434
1439
floors = alphas .astype ("int" )
1435
1440
alphas_mod1 = alphas % 1
1436
1441
# End edge case
1437
1442
alphas_mod1 [- 1 ] = 1
1438
- floors [- 1 ] = len ( rgbs ) - 2
1443
+ floors [- 1 ] = num_colors - 2
1439
1444
return [
1440
1445
rgb_to_color ((rgbs [i ] * (1 - alpha )) + (rgbs [i + 1 ] * alpha ))
1441
1446
for i , alpha in zip (floors , alphas_mod1 )
0 commit comments