From 682679eb3dfa7f1bd905495646cc04d4c7f8db69 Mon Sep 17 00:00:00 2001 From: DigiDuncan Date: Mon, 26 Feb 2024 01:11:27 -0500 Subject: [PATCH 1/3] gross but fast color random speedup --- arcade/types.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/arcade/types.py b/arcade/types.py index 5263da3e3..e85514ed8 100644 --- a/arcade/types.py +++ b/arcade/types.py @@ -408,14 +408,15 @@ def random( :param b: Fixed value for blue channel :param a: Fixed value for alpha channel """ + rand = random.randint(0, 0xFFFFFFFF) if r is None: - r = random.randint(0, 255) + r = (rand & 0xFF000000) >> 24 if g is None: - g = random.randint(0, 255) + g = (rand & 0x00FF0000) >> 16 if b is None: - b = random.randint(0, 255) + b = (rand & 0x0000FF00) >> 8 if a is None: - a = random.randint(0, 255) + a = (rand & 0x000000FF) return cls(r, g, b, a) From 8006aff622e95253a6595a7b9aab7d7ae95b51de Mon Sep 17 00:00:00 2001 From: DigiDuncan Date: Mon, 26 Feb 2024 01:31:04 -0500 Subject: [PATCH 2/3] woah, constants --- arcade/types.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/arcade/types.py b/arcade/types.py index e85514ed8..79f7b6942 100644 --- a/arcade/types.py +++ b/arcade/types.py @@ -39,6 +39,15 @@ MAX_UINT24 = 0xFFFFFF MAX_UINT32 = 0xFFFFFFFF +MASK_RGBA_R = 0xFF000000 +MASK_RGBA_G = 0x00FF0000 +MASK_RGBA_B = 0x0000FF00 +MASK_RGBA_A = 0x000000FF + +MASK_RGB_R = 0xFF0000 +MASK_RGB_G = 0x00FF00 +MASK_RGB_B = 0x0000FF + ChannelType = TypeVar('ChannelType') RGB = Tuple[ChannelType, ChannelType, ChannelType] @@ -408,15 +417,15 @@ def random( :param b: Fixed value for blue channel :param a: Fixed value for alpha channel """ - rand = random.randint(0, 0xFFFFFFFF) + rand = random.randint(0, MAX_UINT32) if r is None: - r = (rand & 0xFF000000) >> 24 + r = (rand & MASK_RGBA_R) >> 24 if g is None: - g = (rand & 0x00FF0000) >> 16 + g = (rand & MASK_RGBA_G) >> 16 if b is None: - b = (rand & 0x0000FF00) >> 8 + b = (rand & MASK_RGBA_B) >> 8 if a is None: - a = (rand & 0x000000FF) + a = (rand & MASK_RGBA_A) return cls(r, g, b, a) From 903fde5165e7b0a7742603a858251599d2b43222 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Mon, 26 Feb 2024 01:49:01 -0500 Subject: [PATCH 3/3] Fix unit tests to account for DigiDuncan's changes to Color randomization --- tests/unit/color/test_color_type.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/unit/color/test_color_type.py b/tests/unit/color/test_color_type.py index 82cf2109b..8b2bf5eb5 100644 --- a/tests/unit/color/test_color_type.py +++ b/tests/unit/color/test_color_type.py @@ -206,7 +206,14 @@ def test_deepcopy_color_inheritance(): @pytest.fixture def randint_is_constant(monkeypatch): - monkeypatch.setattr('random.randint', Mock(return_value=RANDINT_RETURN_RESULT)) + """ + Replace the randomized color uint32 with a known signal value. + + Since the color randomization masks out channels, we use a known + repeated value (128, or 0x80 in hex) to represent a channel fetched + from random rather than taken from user input. + """ + monkeypatch.setattr('random.randint', Mock(return_value=0x80808080)) def test_color_random(randint_is_constant):