diff --git a/arcade/application.py b/arcade/application.py index 6779af70a..9c5a16a58 100644 --- a/arcade/application.py +++ b/arcade/application.py @@ -18,7 +18,7 @@ import arcade from arcade.clock import GLOBAL_CLOCK, GLOBAL_FIXED_CLOCK, _setup_clock, _setup_fixed_clock -from arcade.color import TRANSPARENT_BLACK +from arcade.color import BLACK from arcade.context import ArcadeContext from arcade.types import LBWH, Color, Rect, RGBANormalized, RGBOrA255 from arcade.utils import is_raspberry_pi @@ -268,7 +268,7 @@ def __init__( self.push_handlers(on_resize=self._on_resize) self._ctx: ArcadeContext = ArcadeContext(self, gc_mode=gc_mode, gl_api=gl_api) - self._background_color: Color = TRANSPARENT_BLACK + self._background_color: Color = BLACK self._current_view: View | None = None @@ -1240,7 +1240,9 @@ def __init__( self, window: Window | None = None, background_color: RGBOrA255 | None = None ) -> None: self.window = arcade.get_window() if window is None else window - self.background_color: RGBOrA255 | None = background_color + self._background_color: Color | None = background_color and Color.from_iterable( + background_color + ) def clear( self, @@ -1250,7 +1252,7 @@ def clear( ) -> None: """ Clears the window with the configured background color - set through :py:attr:`arcade.Window.background_color`. + set through :py:attr:`arcade.View.background_color`. Args: color(optional): @@ -1570,3 +1572,32 @@ def center_y(self) -> float: An alias for `arcade.Window.center_y` """ return self.window.center_y + + @property + def background_color(self) -> Color | None: + """ + Get or set the background color for this view. + This affects what color the window will contain when + :py:meth:`~arcade.View.clear` is called. + + Examples:: + + # Use Arcade's built in Color values + view.background_color = arcade.color.AMAZON + + # Set the background color with a custom Color instance + MY_RED = arcade.types.Color(255, 0, 0) + view.background_color = MY_RED + + # Set the background color directly from an RGBA tuple + view.background_color = 255, 0, 0, 255 + + # Set the background color directly from an RGB tuple + # RGB tuples will assume 255 as the opacity / alpha value + view.background_color = 255, 0, 0 + """ + return self._background_color + + @background_color.setter + def background_color(self, value: RGBOrA255) -> None: + self._background_color = Color.from_iterable(value) diff --git a/tests/unit/window/test_view.py b/tests/unit/window/test_view.py index a1b0e28ce..35147fe4c 100644 --- a/tests/unit/window/test_view.py +++ b/tests/unit/window/test_view.py @@ -1,6 +1,6 @@ from unittest.mock import Mock -from arcade import Window, View +from arcade import Window, View, color, get_image def test_on_show_view_called(window): @@ -24,3 +24,11 @@ def test_on_hide_view_called(window): window.show_view(view2) hide_mock.assert_called_once() + +def test_view_background_color(window): + view = View(window, color.ARCADE_GREEN) + assert view.background_color == color.ARCADE_GREEN + window.clear() + # assert get_image(0, 0, 1, 1).getpixel((0, 0)) == color.BLACK + view.clear() + # assert get_image(0, 0, 1, 1).getpixel((0, 0)) == color.ARCADE_GREEN