Use matplotlib colormaps

This commit is contained in:
Grant Sanderson
2021-01-09 22:10:40 -08:00
parent 0b41457d4e
commit 889acea380
3 changed files with 15 additions and 6 deletions

View File

@ -7,6 +7,7 @@ dependencies:
- pip - pip
- pip: - pip:
- pyreadline - pyreadline
- matplotlib
- mapbox-earcut - mapbox-earcut
- moderngl_window - moderngl_window
- screeninfo - screeninfo

View File

@ -3,8 +3,8 @@ import re
import moderngl import moderngl
import numpy as np import numpy as np
import copy import copy
from matplotlib.cm import get_cmap
from manimlib.constants import COLORMAPS
from manimlib.utils.directories import get_shader_dir from manimlib.utils.directories import get_shader_dir
from manimlib.utils.file_ops import find_file from manimlib.utils.file_ops import find_file
@ -122,6 +122,8 @@ class ShaderWrapper(object):
return self return self
# Helper functions related to shader code
def get_shader_code_from_file(filename): def get_shader_code_from_file(filename):
if not filename: if not filename:
return None return None
@ -153,8 +155,9 @@ def get_shader_code_from_file(filename):
def get_colormap_code(colormap="viridis"): def get_colormap_code(colormap="viridis"):
code = """ code = """
const vec3[9] COLOR_MAP_DATA = vec3[9](// INSERT DATA //); const vec3[9] COLOR_MAP_DATA = vec3[9](
// INSERT DATA //
);
vec3 colormap(float value, float min_val, float max_val){ vec3 colormap(float value, float min_val, float max_val){
float alpha = smoothstep(min_val, max_val, value); float alpha = smoothstep(min_val, max_val, value);
int disc_alpha = min(int(alpha * 8), 7); int disc_alpha = min(int(alpha * 8), 7);
@ -165,10 +168,14 @@ def get_colormap_code(colormap="viridis"):
); );
} }
""" """
data = COLORMAPS[colormap] colors = get_cmap(colormap).colors
sparse_colors = [
colors[int(n)]
for n in np.linspace(0, len(colors) - 1, 9)
]
insertion = "".join( insertion = "".join(
"vec3({}, {}, {}),".format(*vect) "vec3({}, {}, {}),".format(*color)
for vect in data for color in sparse_colors
) )
insertion = insertion[:-1] # Remove final comma insertion = insertion[:-1] # Remove final comma
return code.replace("// INSERT DATA //", insertion) return code.replace("// INSERT DATA //", insertion)

View File

@ -7,6 +7,7 @@ scipy
sympy sympy
tqdm tqdm
mapbox-earcut mapbox-earcut
matplotlib
moderngl moderngl
moderngl_window moderngl_window
pydub pydub