Skip to content

Refactor color handling into a new Color type #1639

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

Merged
merged 99 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
17ac553
Initial Color tuple subclass which doesn't break tests
pushfoo Mar 14, 2023
64f7973
Add basic from_ class methods to Color type
pushfoo Mar 14, 2023
3bc4bd0
Move color module tests into new tests/color folder
pushfoo Mar 14, 2023
39a2393
Fix Color.__new__: it's not __init__, must return a value
pushfoo Mar 14, 2023
d4b3e65
Add tests for Color.from_uint24
pushfoo Mar 14, 2023
9ef80ca
Remove uint24_to_three_byte_color
pushfoo Mar 14, 2023
a075ce3
Add Color.__repr__ to make test failures easier to read
pushfoo Mar 14, 2023
a9a899f
Remove unneeded RGB import from drawing support
pushfoo Mar 14, 2023
8c23a19
Convert arcade.color to new Color type
pushfoo Mar 14, 2023
cd4dfc4
Convert arcade.csscolor to new Color type
pushfoo Mar 14, 2023
12e318c
Fix docstring for Color.normalized
pushfoo Mar 14, 2023
6f883b2
Add tests for Color.normalized
pushfoo Mar 14, 2023
23abefa
Remove leftover uint24 import in arcade.__all__
pushfoo Mar 14, 2023
82abee2
Make tests pass by accounting for extra imports
pushfoo Mar 14, 2023
af1b772
Remove get_four_float_color from SpriteList
pushfoo Mar 14, 2023
2543231
Remove get_four_float_color & tests
pushfoo Mar 14, 2023
cd2462b
Remove make_transparent_color
pushfoo Mar 14, 2023
c72c1c3
Replace range error message func with Exception hierarchy
pushfoo Mar 15, 2023
580009b
Port + add tests for Colors from uint32s
pushfoo Mar 15, 2023
a033dbc
Remove uint32_to_four_byte_color + tests
pushfoo Mar 15, 2023
f54d9c2
Add Color.from_normalized + tests
pushfoo Mar 15, 2023
5f49d6a
Use constants for bound checking source ints
pushfoo Mar 15, 2023
2a192b3
Switch SpriteList.color to using Color.from_normalized
pushfoo Mar 15, 2023
dee8a2a
Remove float_to_byte_color + tests
pushfoo Mar 15, 2023
ec4ec7e
Switch shadertoy_demo_3.py to Color.normalized
pushfoo Mar 15, 2023
cd57b0a
Remove redundant get_three_float_color function from drawing_support.py
pushfoo Mar 15, 2023
b7143e4
Remove now-obsolete typing imports
pushfoo Mar 15, 2023
d0b7c8c
Revert accidental win32 platform flag change
pushfoo Mar 15, 2023
3c141b8
Fix typing error on FloatOutsideRangeError.__init__
pushfoo Mar 15, 2023
8487b8a
Improve Color test helper legibility & flexibilty
pushfoo Mar 16, 2023
773380d
Remove too-clever functions & use plain itertools
pushfoo Mar 17, 2023
9f87e2d
Move hex code parsing onto Color object
pushfoo Mar 17, 2023
c5b2aee
Add RGBALike Union type
pushfoo Mar 17, 2023
bb750e3
Make text import get_four_byte_color from correct location
pushfoo Mar 18, 2023
f2bf508
Make get_four_byte_color use Color type
pushfoo Mar 18, 2023
5825b42
Use tuple's generic subscripting to ease Color typing
pushfoo Mar 18, 2023
af1ff83
Use type: ignore workaround for mypy bug
pushfoo Mar 18, 2023
97c48dc
Update BasicSprite to use new Color type
pushfoo Mar 18, 2023
33dd52f
Update colored filler sprites to use new Color type
pushfoo Mar 18, 2023
3dc1a28
Add a top-level docstring for new Color type
pushfoo Mar 18, 2023
e9cb67c
Fix typing & fill behavior for Texture
pushfoo Mar 18, 2023
c6b0bda
Typing updates to use Color for texture generation
pushfoo Mar 18, 2023
4b99c53
Switch BasicSprite.draw_hit_box to use RGBALike
pushfoo Mar 18, 2023
111091a
Switch arcade/draw_commands to new Color type & remove get_four_byte_…
pushfoo Mar 18, 2023
64931e7
Switch Scene to using new Color typing
pushfoo Mar 18, 2023
92c5d43
Switch SpriteList to use updated Color typing
pushfoo Mar 18, 2023
879bcdd
Improve BasicSprite and SpriteList color explanations
pushfoo Mar 18, 2023
f95083b
Add mention of using plain RGBA tuples to the Color docstring
pushfoo Mar 18, 2023
00eb415
Switch RenderTextureTarget to new Color typing
pushfoo Mar 18, 2023
376f15d
Switch lights to use new Color typing
pushfoo Mar 18, 2023
c6dfdf9
Switch particle fireworks example to use RGBA tuples
pushfoo Mar 18, 2023
84e0d32
Switch arcade.text module to use new Color typing & remove get_four_b…
pushfoo Mar 18, 2023
9eb0bdd
Update widgets to preliminary compliance with new typing
pushfoo Mar 18, 2023
4bb03fb
Subclass typing.Tuple for 3.8 compatibility
pushfoo Mar 19, 2023
2ea93de
Fix unused / incorrect import in gui.widgets.text
pushfoo Mar 19, 2023
60125e6
Add from_iterable helper function
pushfoo Mar 19, 2023
afe887f
Make r, b, and b args to Color.__new__ mandatory
pushfoo Mar 19, 2023
eeae186
Use Color.from_iterable in arcade.draw_commands
pushfoo Mar 19, 2023
5cb773d
Use Color.from_iterable in arcade.text
pushfoo Mar 19, 2023
9923ee2
Use Color.from_iterable in arcade.sprite.colored
pushfoo Mar 19, 2023
e9e9cf0
Clean up BasicSprite.color
pushfoo Mar 19, 2023
1c161a9
Remove extraneous imports from arcade.drawing_support
pushfoo Mar 19, 2023
4f0be22
Clean up unused import in arcade.experimental.lights
pushfoo Mar 19, 2023
0bdeb75
Remove unused import in texture generation
pushfoo Mar 19, 2023
c5f1e6d
Update typing + remove get_four_byte_color in PerfGraph
pushfoo Mar 19, 2023
3a01a67
Use proper inspect.getmembers call to get Colors in lines_buffered ex…
pushfoo Mar 20, 2023
407d292
Use new Color typing + remove get_four_byte_color
pushfoo Mar 20, 2023
c53236d
Fix indent in arcade.shape_list docstring
pushfoo Mar 20, 2023
86228a3
Add an RGBANormalized type alias
pushfoo Mar 20, 2023
fcce06f
Add RGBA255OrNormalized to annotate arcade.application
pushfoo Mar 20, 2023
bf26639
Update arcade.application to use new Color typing
pushfoo Mar 20, 2023
13f38c8
Fix missed annotation update in draw_commands
pushfoo Mar 20, 2023
d0c4fd8
Update color annotation in isometric support function
pushfoo Mar 20, 2023
8a34d7b
Update color annotation on RenderTextureTarget
pushfoo Mar 20, 2023
cfded9a
Update Color annotation in arcade.gui.surface.Surface
pushfoo Mar 20, 2023
fe3bba4
Update typing for widget bases
pushfoo Mar 20, 2023
e982c16
Update Color annotations in buttons
pushfoo Mar 20, 2023
2d9da5c
Use Color.from_iterable in BasicSprite.color setter
pushfoo Mar 20, 2023
c92d82e
Use Color.from_iterable in SpriteList.color setter
pushfoo Mar 20, 2023
772e396
Remove get_four_byte_color usage in examples
pushfoo Mar 20, 2023
4b91491
Remove get_four_byte_color from arcade.gui.widgets.text
pushfoo Mar 20, 2023
48f9e32
Remove get_four_byte_color from examples
pushfoo Mar 20, 2023
451b06d
Remove get_four_byte_color tests
pushfoo Mar 20, 2023
cee1890
Rename test_drawing_support.py to test_rotate_point.py
pushfoo Mar 20, 2023
f9b05da
Remove get_four_byte_color entirely
pushfoo Mar 20, 2023
3cab64e
Clean up unused imports
pushfoo Mar 20, 2023
fd4e273
Add docstrings for custom exceptions including einarf's advice on nor…
pushfoo Mar 20, 2023
5ac0144
Specify rgb positionally in Color.from_normalized
pushfoo Mar 20, 2023
9ce5390
Make Color.from_normalized consistent with the other converter classm…
pushfoo Mar 21, 2023
7874021
Update Color.from_normalized usage in SpriteList
pushfoo Mar 21, 2023
65b15aa
Rework intensity class method + add tests for it
pushfoo Mar 21, 2023
a5a4807
Make Color docstring examples consistent with print output
pushfoo Mar 21, 2023
ac6abff
Make Color classmethods use consistent internal calling convention
pushfoo Mar 21, 2023
6b17e29
Fix arcade.text type annotation inconsistency to ease search and replace
pushfoo Mar 21, 2023
6f2319e
Change window_commands to accomodate Color
pushfoo Mar 21, 2023
c1a8a11
Refactor color generics + remove RGBALike and ColorLike
pushfoo Mar 21, 2023
a95a387
Remove accidentally included file meant to be git stashed
pushfoo Mar 21, 2023
7f933c7
Merge branch 'development' into color_refactor
einarf Mar 29, 2023
638a5ea
Fix merge conflict typing issue
pushfoo Mar 29, 2023
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
16 changes: 0 additions & 16 deletions arcade/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,7 @@ def configure_logging(level: Optional[int] = None):
# pyglet.options['win32_gdi_font'] = True

# Imports from modules that don't do anything circular
from .drawing_support import get_four_byte_color
from .drawing_support import get_three_float_color
from .drawing_support import get_four_float_color
from .drawing_support import get_points_for_thick_line
from .drawing_support import make_transparent_color
from .drawing_support import uint24_to_three_byte_color
from .drawing_support import uint32_to_four_byte_color
from .drawing_support import color_from_hex_string
from .drawing_support import float_to_byte_color

# Complex imports with potential circularity
from .window_commands import close_window
Expand Down Expand Up @@ -291,7 +283,6 @@ def configure_logging(level: Optional[int] = None):
'check_for_collision_with_list',
'check_for_collision_with_lists',
'close_window',
'color_from_hex_string',
'disable_timings',
'draw_arc_filled',
'draw_arc_outline',
Expand Down Expand Up @@ -325,13 +316,10 @@ def configure_logging(level: Optional[int] = None):
'enable_timings',
'exit',
'finish_render',
'float_to_byte_color',
'get_closest_sprite',
'get_display_size',
'get_distance_between_sprites',
'get_sprites_in_rect',
'get_four_byte_color',
'get_four_float_color',
'get_controllers',
'get_game_controllers',
'get_image',
Expand All @@ -343,7 +331,6 @@ def configure_logging(level: Optional[int] = None):
'get_sprites_at_point',
'SpatialHash',
'get_timings',
'get_three_float_color',
'create_text_sprite',
'clear_timings',
'get_window',
Expand All @@ -359,7 +346,6 @@ def configure_logging(level: Optional[int] = None):
'make_circle_texture',
'make_soft_circle_texture',
'make_soft_square_texture',
'make_transparent_color',
'open_window',
'pause',
'print_timings',
Expand All @@ -374,8 +360,6 @@ def configure_logging(level: Optional[int] = None):
'start_render',
'stop_sound',
'timings_enabled',
'uint24_to_three_byte_color',
'uint32_to_four_byte_color',
'unschedule',
'schedule_once',
'cleanup_texture_cache',
Expand Down
42 changes: 30 additions & 12 deletions arcade/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from arcade import set_window
from arcade.color import TRANSPARENT_BLACK
from arcade.context import ArcadeContext
from arcade.types import Color
from arcade.types import Color, RGBA255, RGBA255OrNormalized
from arcade import SectionManager
from arcade.utils import is_raspberry_pi

Expand Down Expand Up @@ -240,14 +240,20 @@ def ctx(self) -> ArcadeContext:

def clear(
self,
color: Optional[Color] = None,
color: Optional[RGBA255OrNormalized] = None,
normalized: bool = False,
viewport: Optional[Tuple[int, int, int, int]] = None,
):
"""Clears the window with the configured background color
set through :py:attr:`arcade.Window.background_color`.

:param Color color: Optional color overriding the current background color
:param color: (Optional) override the current background color
with one of the following:

1. A :py:class:`~arcade.types.Color` instance
2. A 4-length RGBA :py:class:`tuple` of byte values (0 to 255)
3. A 4-length RGBA :py:class:`tuple` of normalized floats (0.0 to 1.0)

:param bool normalized: If the color format is normalized (0.0 -> 1.0) or byte values
:param Tuple[int, int, int, int] viewport: The viewport range to clear
"""
Expand All @@ -263,22 +269,28 @@ def background_color(self) -> Color:

Examples::

# Use Arcade's built in color values
# Use Arcade's built in Color values
window.background_color = arcade.color.AMAZON

# Specify RGB value directly (red)
window.background_color = 255, 0, 0
# Set the background color with a custom Color instance
MY_RED = arcade.types.Color(255, 0, 0)
window.background_color = MY_RED

If the background color is an ``RGB`` value instead of ``RGBA``
we assume alpha value 255.
# Set the backgrund color directly from an RGBA tuple
window.background_color = 255, 0, 0, 255

# (Discouraged)
# Set the background color directly from an RGB tuple
# RGB tuples will assume 255 as the opacity / alpha value
window.background_color = 255, 0, 0

:type: Color
"""
return self._background_color

@background_color.setter
def background_color(self, value: Color):
self._background_color = value
def background_color(self, value: RGBA255):
self._background_color = Color.from_iterable(value)

def run(self) -> None:
"""
Expand Down Expand Up @@ -957,14 +969,20 @@ def add_section(self, section, at_index: Optional[int] = None, at_draw_order: Op

def clear(
self,
color: Optional[Color] = None,
color: Optional[RGBA255OrNormalized] = None,
normalized: bool = False,
viewport: Optional[Tuple[int, int, int, int]] = None,
):
"""Clears the View's Window with the configured background color
set through :py:attr:`arcade.Window.background_color`.

:param Color color: Optional color overriding the current background color
:param color: (Optional) override the current background color
with one of the following:

1. A :py:class:`~arcade.types.Color` instance
2. A 4-length RGBA :py:class:`tuple` of byte values (0 to 255)
3. A 4-length RGBA :py:class:`tuple` of normalized floats (0.0 to 1.0)

:param bool normalized: If the color format is normalized (0.0 -> 1.0) or byte values
:param Tuple[int, int, int, int] viewport: The viewport range to clear
"""
Expand Down
Loading