Below is to record How I create a list of Gradients colors for Manim or Matplotlib.




from manim import *
from os import system
import random
import numpy as np
from colour import Color



## define below two functions.
def hex_to_RGB(hex_str):
    """ #FFFFFF -> [255,255,255]"""
    #Pass 16 to the integer function for change of base
    return [int(hex_str[i:i+2], 16) for i in range(1,6,2)]

def get_color_gradient(c1, c2, n):
    """
    Given two hex colors, returns a color gradient
    with n colors.
    """
    assert n > 1
    c1_rgb = np.array(hex_to_RGB(c1))/255
    c2_rgb = np.array(hex_to_RGB(c2))/255
    mix_pcts = [x/(n-1) for x in range(n)]
    rgb_colors = [((1-mix)*c1_rgb + (mix*c2_rgb)) for mix in mix_pcts]
    return [Color("#" + "".join([format(int(round(val*255)), "02x") for val in item])) for item in rgb_colors]


###create Gradients colors. then use colorssss[xxx] in Manim or colorssss[xxx].get_hex() in matplotlib.
tmp = ["#be0aff", "#580aff", "#147df5", "#0aefff", "#0aff99",
                 "#a1ff0a", "#deff0a", "#ffd300", "#ff8700", "#ff0000", "#be0aff"]

bases = []
for t in range(30):
    for c in tmp:
       bases.append(c)
colorssss = []
for index in range(len(bases)):
    if index < len(bases) - 1:
       start = bases[index]
       end = bases[index + 1]
       colors = get_color_gradient(start, end, 40)
       for c in colors:
           colorssss.append(c)
print(len(colorssss))

##take example, for Manim
Circle(radius=smallRaduis, color=colorssss[self.frameIndex],fill_opacity=1, stroke_width=3,
                            stroke_color=BLACK)

##take example, for matplotlib.
line = LineCollection(realTmp, colors=newColors[:len(realTmp)])

By Admin

Think-Math Website