Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Direct computation of color hex to speed things up

Grant Sanderson983a3e0357d8
diff --git a/camera/camera.py b/camera/camera.py
index 111eae03b..6c7c369fa 100644
--- a/camera/camera.py
+++ b/camera/camera.py
@@ -241,14 +241,22 @@ class Camera(object):
canvas.symbol((0, 0), symbol, pen, fill)
def get_pen_and_fill(self, vmobject):
- pen = aggdraw.Pen(
- self.color_to_hex_l(self.get_stroke_color(vmobject)),
- max(vmobject.stroke_width, 0)
- )
- fill = aggdraw.Brush(
- self.color_to_hex_l(self.get_fill_color(vmobject)),
- opacity = int(self.color_max_val*vmobject.get_fill_opacity())
- )
+ stroke_width = max(vmobject.get_stroke_width(), 0)
+ if stroke_width == 0:
+ pen = None
+ else:
+ stroke_rgb = self.get_stroke_rgb(vmobject)
+ stroke_hex = rgb_to_hex(stroke_rgb)
+ pen = aggdraw.Pen(stroke_hex, stroke_width)
+
+ fill_opacity = int(self.color_max_val*vmobject.get_fill_opacity())
+ if fill_opacity == 0:
+ fill = None
+ else:
+ fill_rgb = self.get_fill_rgb(vmobject)
+ fill_hex = rgb_to_hex(fill_rgb)
+ fill = aggdraw.Brush(fill_hex, fill_opacity)
+
return (pen, fill)
def color_to_hex_l(self, color):
@@ -257,14 +265,14 @@ class Camera(object):
except:
return Color(BLACK).get_hex_l()
- def get_stroke_color(self, vmobject):
- return vmobject.get_stroke_color()
+ def get_stroke_rgb(self, vmobject):
+ return vmobject.get_stroke_rgb()
- def get_fill_color(self, vmobject):
- return vmobject.get_fill_color()
+ def get_fill_rgb(self, vmobject):
+ return vmobject.get_fill_rgb()
def get_pathstring(self, vmobject):
- result = ""
+ result = ""
for mob in [vmobject]+vmobject.get_subpath_mobjects():
points = mob.points
# points = self.adjust_out_of_range_points(points)
diff --git a/helpers.py b/helpers.py
index 8e1af2383..d0e3c962a 100644
--- a/helpers.py
+++ b/helpers.py
@@ -126,7 +126,7 @@ def rgba_to_color(rgba):
return rgb_to_color(rgba[:3])
def rgb_to_hex(rgb):
- return Color(rgb = rgb).get_hex_l()
+ return "#" + "".join('%02x'%int(255*x) for x in rgb)
def invert_color(color):
return rgb_to_color(1.0 - color_to_rgb(color))
diff --git a/mobject/vectorized_mobject.py b/mobject/vectorized_mobject.py
index fbf055b04..7bb8bdf35 100644
--- a/mobject/vectorized_mobject.py
+++ b/mobject/vectorized_mobject.py
@@ -120,6 +120,9 @@ class VMobject(Mobject):
)
return self
+ def get_fill_rgb(self):
+ return self.fill_rgb
+
def get_fill_color(self):
try:
self.fill_rgb = np.clip(self.fill_rgb, 0.0, 1.0)
@@ -130,6 +133,9 @@ class VMobject(Mobject):
def get_fill_opacity(self):
return np.clip(self.fill_opacity, 0, 1)
+ def get_stroke_rgb(self):
+ return self.stroke_rgb
+
def get_stroke_color(self):
try:
self.stroke_rgb = np.clip(self.stroke_rgb, 0, 1)