From 175d6da4e116ed2a28f0164553059aaae6773c3e Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Mon, 16 Nov 2020 03:32:07 +0100 Subject: [PATCH 1/6] change background_color and background_opacity in camera to properties --- manim/camera/camera.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/manim/camera/camera.py b/manim/camera/camera.py index 9835f4a411..7ecb706ef4 100644 --- a/manim/camera/camera.py +++ b/manim/camera/camera.py @@ -88,12 +88,13 @@ def __init__(self, video_quality_config, background=None, **kwargs): "pixel_width", "frame_height", "frame_width", - "frame_rate", - "background_color", - "background_opacity", + "frame_rate" ]: 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. From 04c1d22b847fc40deceb9829525281d4c8842f53 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Mon, 16 Nov 2020 03:32:37 +0100 Subject: [PATCH 2/6] add camera property to scene for convenience --- manim/scene/scene.py | 4 ++++ 1 file changed, 4 insertions(+) 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. From ab47d1a8505d2b706288c4a85d90c86a622759c5 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Mon, 16 Nov 2020 03:32:55 +0100 Subject: [PATCH 3/6] add unit test for changed background color --- tests/test_color.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/test_color.py b/tests/test_color.py index 259d83fcfb..1ab7da3060 100644 --- a/tests/test_color.py +++ b/tests/test_color.py @@ -1,8 +1,28 @@ 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(): + import manim.utils.color as C + + S = Scene() + S.camera.background_color = C.RED + S.renderer.update_frame(S) + assert np.all(S.renderer.get_frame()[0, 0] == np.array([252, 98, 85, 255])) + + S.camera.background_color = C.BLUE + S.renderer.update_frame(S) + assert np.all(S.renderer.get_frame()[0, 0] == np.array([88, 196, 221, 255])) + + S.camera.background_color = C.GREEN + S.camera.background_opacity = 0.5 + S.renderer.update_frame(S) + assert np.all(S.renderer.get_frame()[0, 0] == np.array([131, 193, 103, 127])) \ No newline at end of file From eedc55bac1c828aec7dbedbf562d7ed0809a6116 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Mon, 16 Nov 2020 03:43:49 +0100 Subject: [PATCH 4/6] black --- manim/camera/camera.py | 2 +- tests/test_color.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/manim/camera/camera.py b/manim/camera/camera.py index 7ecb706ef4..63f61d89f0 100644 --- a/manim/camera/camera.py +++ b/manim/camera/camera.py @@ -88,7 +88,7 @@ def __init__(self, video_quality_config, background=None, **kwargs): "pixel_width", "frame_height", "frame_width", - "frame_rate" + "frame_rate", ]: setattr(self, attr, kwargs.get(attr, config[attr])) diff --git a/tests/test_color.py b/tests/test_color.py index 1ab7da3060..3235e80e2e 100644 --- a/tests/test_color.py +++ b/tests/test_color.py @@ -25,4 +25,4 @@ def test_background_color(): S.camera.background_color = C.GREEN S.camera.background_opacity = 0.5 S.renderer.update_frame(S) - assert np.all(S.renderer.get_frame()[0, 0] == np.array([131, 193, 103, 127])) \ No newline at end of file + assert np.all(S.renderer.get_frame()[0, 0] == np.array([131, 193, 103, 127])) From 7ef9e644b8f2a259605113aea5f17d90b98bbdbe Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Mon, 16 Nov 2020 14:25:32 +0100 Subject: [PATCH 5/6] test background color with colors defined in test --- tests/test_color.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_color.py b/tests/test_color.py index 3235e80e2e..7e144aaf4e 100644 --- a/tests/test_color.py +++ b/tests/test_color.py @@ -14,15 +14,15 @@ def test_background_color(): import manim.utils.color as C S = Scene() - S.camera.background_color = C.RED + S.camera.background_color = "#ff0000" S.renderer.update_frame(S) - assert np.all(S.renderer.get_frame()[0, 0] == np.array([252, 98, 85, 255])) + assert np.all(S.renderer.get_frame()[0, 0] == np.array([255, 0, 0, 255])) - S.camera.background_color = C.BLUE + S.camera.background_color = "#436f80" S.renderer.update_frame(S) - assert np.all(S.renderer.get_frame()[0, 0] == np.array([88, 196, 221, 255])) + assert np.all(S.renderer.get_frame()[0, 0] == np.array([67, 111, 128, 255])) - S.camera.background_color = C.GREEN + 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([131, 193, 103, 127])) + assert np.all(S.renderer.get_frame()[0, 0] == np.array([187, 255, 187, 127])) From 619a9473bda0e7a90c5e653e86c9582bed221055 Mon Sep 17 00:00:00 2001 From: Benjamin Hackl Date: Mon, 16 Nov 2020 14:26:24 +0100 Subject: [PATCH 6/6] remove unnecessary import --- tests/test_color.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_color.py b/tests/test_color.py index 7e144aaf4e..354d512e80 100644 --- a/tests/test_color.py +++ b/tests/test_color.py @@ -11,8 +11,6 @@ def test_import_color(): def test_background_color(): - import manim.utils.color as C - S = Scene() S.camera.background_color = "#ff0000" S.renderer.update_frame(S)