diff --git a/manim/camera/camera.py b/manim/camera/camera.py index 9835f4a411..63f61d89f0 100644 --- a/manim/camera/camera.py +++ b/manim/camera/camera.py @@ -89,11 +89,12 @@ def __init__(self, video_quality_config, background=None, **kwargs): "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 @@ def __deepcopy__(self, memo): 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 ee6983feb1..145a310222 100644 --- a/manim/scene/scene.py +++ b/manim/scene/scene.py @@ -80,6 +80,10 @@ def __init__(self, renderer=None, **kwargs): 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 259d83fcfb..354d512e80 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]))