Refactor Mobject.set_rgba_array_by_color

This commit is contained in:
Grant Sanderson 2022-03-22 10:35:49 -07:00
parent c0b7b55e49
commit 8b1f0a8749

View file

@ -935,33 +935,25 @@ class Mobject(object):
return self return self
def set_rgba_array_by_color(self, color=None, opacity=None, name="rgbas", recurse=True): def set_rgba_array_by_color(self, color=None, opacity=None, name="rgbas", recurse=True):
max_len = 0
if color is not None: if color is not None:
rgbs = np.array([color_to_rgb(c) for c in listify(color)]) rgbs = np.array([color_to_rgb(c) for c in listify(color)])
max_len = len(rgbs)
if opacity is not None: if opacity is not None:
opacities = listify(opacity) opacities = np.array(listify(opacity))
max_len = max(max_len, len(opacities))
# Color only for mob in self.get_family(recurse):
if color is not None and opacity is None: if max_len > len(mob.data[name]):
for mob in self.get_family(recurse): mob.data[name] = resize_array(mob.data[name], max_len)
mob.data[name] = resize_array(mob.data[name], len(rgbs)) size = len(mob.data[name])
mob.data[name][:, :3] = rgbs if color is not None:
mob.data[name][:, :3] = resize_array(rgbs, size)
# Opacity only if opacity is not None:
if color is None and opacity is not None: mob.data[name][:, 3] = resize_array(opacities, size)
for mob in self.get_family(recurse):
mob.data[name] = resize_array(mob.data[name], len(opacities))
mob.data[name][:, 3] = opacities
# Color and opacity
if color is not None and opacity is not None:
rgbas = np.array([
[*rgb, o]
for rgb, o in zip(*make_even(rgbs, opacities))
])
for mob in self.get_family(recurse):
mob.data[name] = rgbas.copy()
return self return self
def set_color(self, color, opacity=None, recurse=True): def set_color(self, color, opacity=None, recurse=True):
self.set_rgba_array_by_color(color, opacity, recurse=False) self.set_rgba_array_by_color(color, opacity, recurse=False)
# Recurse to submobjects differently from how set_rgba_array_by_color # Recurse to submobjects differently from how set_rgba_array_by_color