Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Make it easier to change background color (#716)

Benjamin Hackl396548408aa9
diff --git a/manim/camera/camera.py b/manim/camera/camera.py
index 9835f4a41..63f61d89f 100644
--- a/manim/camera/camera.py
+++ b/manim/camera/camera.py
@@ -89,11 +89,12 @@ class Camera(object):
"frame_height",
"frame_width",
"frame_rate",
- "background_color",
- "background_opacity",
]:
setattr(self, attr, kwargs.get(attr, config[attr]))
+ for attr in ["background_color", "background_opacity"]:
+ setattr(self, f"_{attr}", kwargs.get(attr, config[attr]))
+
# This one is in the same boat as the above, but it doesn't have the
# same name as the corresponding key so it has to be handled on its own
self.max_allowable_norm = config["frame_width"]
@@ -117,6 +118,24 @@ class Camera(object):
self.canvas = None
return copy.copy(self)
+ @property
+ def background_color(self):
+ return self._background_color
+
+ @background_color.setter
+ def background_color(self, color):
+ self._background_color = color
+ self.init_background()
+
+ @property
+ def background_opacity(self):
+ return self._background_opacity
+
+ @background_opacity.setter
+ def background_opacity(self, alpha):
+ self._background_opacity = alpha
+ self.init_background()
+
def type_or_raise(self, mobject):
"""Return the type of mobject, if it is a type that can be rendered.
diff --git a/manim/scene/scene.py b/manim/scene/scene.py
index ee6983feb..145a31022 100644
--- a/manim/scene/scene.py
+++ b/manim/scene/scene.py
@@ -80,6 +80,10 @@ class Scene(Container):
self.setup()
+ @property
+ def camera(self):
+ return self.renderer.camera
+
def render(self):
"""
Render this Scene.
diff --git a/tests/test_color.py b/tests/test_color.py
index 259d83fcf..354d512e8 100644
--- a/tests/test_color.py
+++ b/tests/test_color.py
@@ -1,8 +1,26 @@
import pytest
-from manim import Camera, tempconfig, config
+import numpy as np
+
+from manim import Camera, Scene, tempconfig, config
def test_import_color():
import manim.utils.color as C
C.WHITE
+
+
+def test_background_color():
+ S = Scene()
+ S.camera.background_color = "#ff0000"
+ S.renderer.update_frame(S)
+ assert np.all(S.renderer.get_frame()[0, 0] == np.array([255, 0, 0, 255]))
+
+ S.camera.background_color = "#436f80"
+ S.renderer.update_frame(S)
+ assert np.all(S.renderer.get_frame()[0, 0] == np.array([67, 111, 128, 255]))
+
+ S.camera.background_color = "#bbffbb"
+ S.camera.background_opacity = 0.5
+ S.renderer.update_frame(S)
+ assert np.all(S.renderer.get_frame()[0, 0] == np.array([187, 255, 187, 127]))