diff --git a/buildconfig/stubs/pygame/pixelarray.pyi b/buildconfig/stubs/pygame/pixelarray.pyi index a10bb66e42..128748fe9b 100644 --- a/buildconfig/stubs/pygame/pixelarray.pyi +++ b/buildconfig/stubs/pygame/pixelarray.pyi @@ -1,8 +1,10 @@ from typing import Any, Dict, Tuple, Union, overload from pygame.surface import Surface +from pygame.color import Color +from pygame.typing import SequenceLike -from pygame.typing import ColorLike, SequenceLike +_ColorLike = int | Color | tuple[int, int, int] | tuple[int, int, int, int] class PixelArray: surface: Surface @@ -34,14 +36,14 @@ class PixelArray: def make_surface(self) -> Surface: ... def replace( self, - color: ColorLike, - repcolor: ColorLike, + color: _ColorLike, + repcolor: _ColorLike, distance: float = 0, weights: SequenceLike[float] = (0.299, 0.587, 0.114), ) -> None: ... def extract( self, - color: ColorLike, + color: _ColorLike, distance: float = 0, weights: SequenceLike[float] = (0.299, 0.587, 0.114), ) -> PixelArray: ... diff --git a/docs/reST/ref/mask.rst b/docs/reST/ref/mask.rst index 886d65a7c0..ef2716c0ef 100644 --- a/docs/reST/ref/mask.rst +++ b/docs/reST/ref/mask.rst @@ -71,10 +71,10 @@ to store which parts collide. :param color: color used to check if the surface's pixels are within the given ``threshold`` range, this parameter is ignored if the optional ``othersurface`` parameter is supplied - :type color: Color or int or tuple(int, int, int, [int]) or list[int, int, int, [int]] + :type color: :data:`pygame.typing.ColorLike` :param threshold: (optional) the threshold range used to check the difference between two colors (default is ``(0, 0, 0, 255)``) - :type threshold: Color or int or tuple(int, int, int, [int]) or list[int, int, int, [int]] + :type threshold: :data:`pygame.typing.ColorLike` :param Surface othersurface: (optional) used to check whether the pixels of the first surface are within the given ``threshold`` range of the pixels from this surface (default is ``None``) diff --git a/src_c/mask.c b/src_c/mask.c index 074ddfd383..df22d24066 100644 --- a/src_c/mask.c +++ b/src_c/mask.c @@ -1130,13 +1130,13 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs) } if (!pg_MappedColorFromObj(rgba_obj_color, surf, &color, - PG_COLOR_HANDLE_INT)) { + PG_COLOR_HANDLE_ALL)) { return NULL; } if (rgba_obj_threshold) { if (!pg_MappedColorFromObj(rgba_obj_threshold, surf, &color_threshold, - PG_COLOR_HANDLE_INT)) { + PG_COLOR_HANDLE_ALL)) { return NULL; } } diff --git a/test/mask_test.py b/test/mask_test.py index 5740bbc2bb..0a46719f0f 100644 --- a/test/mask_test.py +++ b/test/mask_test.py @@ -6325,21 +6325,19 @@ def alternate(func, set_value, unset_value, width, height): def test_from_threshold(self): """Does mask.from_threshold() work correctly?""" - a = [16, 24, 32] + bpp = [16, 24, 32] - for i in a: + for i in bpp: surf = pygame.surface.Surface((70, 70), 0, i) surf.fill((100, 50, 200), (20, 20, 20, 20)) mask = pygame.mask.from_threshold( surf, (100, 50, 200, 255), (10, 10, 10, 255) ) - rects = mask.get_bounding_rects() - self.assertEqual(mask.count(), 400) self.assertEqual(mask.get_bounding_rects(), [pygame.Rect((20, 20, 20, 20))]) - for i in a: + for i in bpp: surf = pygame.surface.Surface((70, 70), 0, i) surf2 = pygame.surface.Surface((70, 70), 0, i) surf.fill((100, 100, 100)) @@ -6356,6 +6354,10 @@ def test_from_threshold(self): self.assertEqual(mask.count(), 100) self.assertEqual(mask.get_bounding_rects(), [pygame.Rect((40, 40, 10, 10))]) + for color in [(20, 20, 20), 10, "green", pygame.Color("blue")]: + surf = pygame.surface.Surface((10, 10)) + pygame.mask.from_threshold(surf, color, color) + def test_zero_size_from_surface(self): """Ensures from_surface can create masks from zero sized surfaces.""" for size in ((100, 0), (0, 100), (0, 0)): diff --git a/test/pixelarray_test.py b/test/pixelarray_test.py index 942931c2ad..af8117ceeb 100644 --- a/test/pixelarray_test.py +++ b/test/pixelarray_test.py @@ -1020,7 +1020,6 @@ def test_iter(self): self.assertEqual(iterations, 5) def test_replace(self): - # print("replace start") for bpp in (8, 16, 24, 32): sf = pygame.Surface((10, 10), 0, bpp) sf.fill((255, 0, 0)) @@ -1041,10 +1040,8 @@ def test_replace(self): self.assertEqual(ar[3][6], oval) self.assertEqual(ar[8][9], oval) self.assertEqual(ar[9][9], oval) - # print("replace end") def test_extract(self): - # print("extract start") for bpp in (8, 16, 24, 32): sf = pygame.Surface((10, 10), 0, bpp) sf.fill((0, 0, 255)) @@ -1070,7 +1067,6 @@ def test_extract(self): self.assertEqual(newar[3][6], white) self.assertEqual(newar[8][9], black) self.assertEqual(newar[9][9], black) - # print("extract end") def test_2dslice_assignment(self): w = 2 * 5 * 8