mirror of
https://github.com/3b1b/manim.git
synced 2025-08-05 16:49:03 +00:00
Refactor Mobject.set_rgba_array_by_color
This commit is contained in:
parent
c0b7b55e49
commit
8b1f0a8749
1 changed files with 13 additions and 21 deletions
|
@ -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
|
||||||
|
|
Loading…
Add table
Reference in a new issue