3b1b-manim/utils/color.py

70 lines
1.8 KiB
Python
Raw Normal View History

import numpy as np
import random
from colour import Color
2018-04-01 10:51:54 -07:00
from constants import WHITE
from constants import PALETTE
from utils.bezier import interpolate
def color_to_rgb(color):
return np.array(Color(color).get_rgb())
def color_to_rgba(color, alpha = 1):
return np.append(color_to_rgb(color), [alpha])
def rgb_to_color(rgb):
try:
return Color(rgb = rgb)
except:
return Color(WHITE)
def rgba_to_color(rgba):
return rgb_to_color(rgba[:3])
def rgb_to_hex(rgb):
return "#" + "".join('%02x'%int(255*x) for x in rgb)
def invert_color(color):
return rgb_to_color(1.0 - color_to_rgb(color))
def color_to_int_rgb(color):
return (255*color_to_rgb(color)).astype('uint8')
def color_to_int_rgba(color, alpha = 255):
return np.append(color_to_int_rgb(color), alpha)
def color_gradient(reference_colors, length_of_output):
if length_of_output == 0:
return reference_colors[0]
rgbs = map(color_to_rgb, reference_colors)
alphas = np.linspace(0, (len(rgbs) - 1), length_of_output)
floors = alphas.astype('int')
alphas_mod1 = alphas % 1
#End edge case
alphas_mod1[-1] = 1
floors[-1] = len(rgbs) - 2
return [
rgb_to_color(interpolate(rgbs[i], rgbs[i+1], alpha))
for i, alpha in zip(floors, alphas_mod1)
]
def interpolate_color(color1, color2, alpha):
rgb = interpolate(color_to_rgb(color1), color_to_rgb(color2), alpha)
return rgb_to_color(rgb)
def average_color(*colors):
rgbs = np.array(map(color_to_rgb, colors))
mean_rgb = np.apply_along_axis(np.mean, 0, rgbs)
return rgb_to_color(mean_rgb)
def random_bright_color():
color = random_color()
curr_rgb = color_to_rgb(color)
new_rgb = interpolate(
curr_rgb, np.ones(len(curr_rgb)), 0.5
)
return Color(rgb = new_rgb)
def random_color():
return random.choice(PALETTE)