From 5b9813d25cdd800a4db61d2907178787475271aa Mon Sep 17 00:00:00 2001 From: DigiDuncan Date: Mon, 26 Feb 2024 03:11:09 -0500 Subject: [PATCH 1/6] =?UTF-8?q?swizzle,=20swizzle,=20swizzle...=20?= =?UTF-8?q?=F0=9F=8E=B5=F0=9F=8E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- arcade/types.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/arcade/types.py b/arcade/types.py index 5263da3e3..7af02f193 100644 --- a/arcade/types.py +++ b/arcade/types.py @@ -3,8 +3,6 @@ """ from __future__ import annotations -from __future__ import annotations - import sys from array import array import ctypes @@ -419,6 +417,14 @@ def random( return cls(r, g, b, a) + def swizzle(self, swizzle_string: str) -> Tuple[int, ...]: + ret = [] + for c in swizzle_string.lower(): + if c not in 'rgba': + raise Exception("Swizzle string must only contain characters in [RGBArgba].") + ret.append(getattr(self, c)) + return tuple(ret) + ColorLike = Union[RGB, RGBA255] From cf0aae009f311495481612ded016628fa0f478ed Mon Sep 17 00:00:00 2001 From: DigiDuncan Date: Tue, 27 Feb 2024 12:42:29 -0500 Subject: [PATCH 2/6] better output as per pushfoo's request --- arcade/types.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arcade/types.py b/arcade/types.py index 7af02f193..6c30261d4 100644 --- a/arcade/types.py +++ b/arcade/types.py @@ -421,7 +421,7 @@ def swizzle(self, swizzle_string: str) -> Tuple[int, ...]: ret = [] for c in swizzle_string.lower(): if c not in 'rgba': - raise Exception("Swizzle string must only contain characters in [RGBArgba].") + raise ValueError(f"Swizzle string must only contain characters in [RGBArgba], not {c}.") ret.append(getattr(self, c)) return tuple(ret) From 2b36905228747a2207dd69745db4e0199385b156 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Tue, 27 Feb 2024 14:04:10 -0500 Subject: [PATCH 3/6] Add basic swizzle tests w/o itertools --- tests/unit/color/test_color_type.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/unit/color/test_color_type.py b/tests/unit/color/test_color_type.py index 82cf2109b..cd0e56725 100644 --- a/tests/unit/color/test_color_type.py +++ b/tests/unit/color/test_color_type.py @@ -201,6 +201,26 @@ def test_deepcopy_color_inheritance(): assert isinstance(deep, ColorSubclass) +@pytest.mark.parametrize("klass", [Color, ColorSubclass]) +def test_swizzle(klass): + color_instance = klass(*range(1,5)) + + assert color_instance.swizzle("r") == (1,) + assert color_instance.swizzle("g") == (2,) + assert color_instance.swizzle("b") == (3,) + assert color_instance.swizzle("a") == (4,) + assert color_instance.swizzle("R") == (1,) + assert color_instance.swizzle("G") == (2,) + assert color_instance.swizzle("B") == (3,) + assert color_instance.swizzle("A") == (4,) + + assert color_instance.swizzle("ra") == (1, 4) + assert color_instance.swizzle("RA") == (1, 4) + + assert color_instance.swizzle("aabbggrr") == (4, 4, 3, 3, 2, 2, 1, 1) + assert color_instance.swizzle("AABBGGRR") == (4, 4, 3, 3, 2, 2, 1, 1) + + RANDINT_RETURN_RESULT = 128 From b93de3e4bd467af24206606315e95a7357fec780 Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Tue, 27 Feb 2024 14:08:19 -0500 Subject: [PATCH 4/6] More readable instantiation --- tests/unit/color/test_color_type.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/color/test_color_type.py b/tests/unit/color/test_color_type.py index cd0e56725..a823dbf3c 100644 --- a/tests/unit/color/test_color_type.py +++ b/tests/unit/color/test_color_type.py @@ -203,7 +203,7 @@ def test_deepcopy_color_inheritance(): @pytest.mark.parametrize("klass", [Color, ColorSubclass]) def test_swizzle(klass): - color_instance = klass(*range(1,5)) + color_instance = klass(1, 2, 3, a=4) assert color_instance.swizzle("r") == (1,) assert color_instance.swizzle("g") == (2,) From ca9ef9168d193f47e699363668d9674abe8d483f Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Tue, 27 Feb 2024 14:26:17 -0500 Subject: [PATCH 5/6] Add doc for swizzle --- arcade/types.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/arcade/types.py b/arcade/types.py index 6c30261d4..f92063539 100644 --- a/arcade/types.py +++ b/arcade/types.py @@ -418,6 +418,34 @@ def random( return cls(r, g, b, a) def swizzle(self, swizzle_string: str) -> Tuple[int, ...]: + """ + Get a tuple of channel values in the same order as the passed string. + + This imitates swizzling `as implemented in GLSL `_ + + .. code-block:: python + + >>> from arcade.types import Color + >>> color = Color(180, 90, 0, 255) + >>> color.swizzle("abgr") + (255, 0, 90, 180) + + You can also use any length of swizzle string and use capital + letters. Any capitals will be treated as lower case equivalents. + + .. code-block: python + + >>> from arcade.types import Color + >>> color = Color(180, 90, 0, 255) + >>> color.swizzle("ABGR") + (255, 0, 90, 180) + + + :param swizzle_string: + A string of channel names as letters in ``"RGBArgba"``. + :return: + A tuple in the same order as the input string. + """ ret = [] for c in swizzle_string.lower(): if c not in 'rgba': From 53f77b51b141ac3f78b3ea88ec1f745ed36b967d Mon Sep 17 00:00:00 2001 From: pushfoo <36696816+pushfoo@users.noreply.github.com> Date: Tue, 27 Feb 2024 14:31:57 -0500 Subject: [PATCH 6/6] Add edge case to swizzle tests --- tests/unit/color/test_color_type.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/unit/color/test_color_type.py b/tests/unit/color/test_color_type.py index a823dbf3c..43946288e 100644 --- a/tests/unit/color/test_color_type.py +++ b/tests/unit/color/test_color_type.py @@ -205,6 +205,9 @@ def test_deepcopy_color_inheritance(): def test_swizzle(klass): color_instance = klass(1, 2, 3, a=4) + # Edge case + assert color_instance.swizzle("") == tuple() + assert color_instance.swizzle("r") == (1,) assert color_instance.swizzle("g") == (2,) assert color_instance.swizzle("b") == (3,)