Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated (#2469) #2470

Merged
merged 5 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions arcade/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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):
Expand Down Expand Up @@ -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)
10 changes: 9 additions & 1 deletion tests/unit/window/test_view.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Loading