Skip to content

Commit 28db8df

Browse files
authored
Merge pull request #3164 from damusss/fix-color-typehint
Allow all color types mask.from_threshold and fix typing for PixelArray methods
2 parents c3bab54 + 41f8924 commit 28db8df

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

buildconfig/stubs/pygame/pixelarray.pyi

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from typing import Any, Dict, Tuple, Union, overload
22

33
from pygame.surface import Surface
4+
from pygame.color import Color
5+
from pygame.typing import SequenceLike
46

5-
from pygame.typing import ColorLike, SequenceLike
7+
_ColorLike = int | Color | tuple[int, int, int] | tuple[int, int, int, int]
68

79
class PixelArray:
810
surface: Surface
@@ -34,14 +36,14 @@ class PixelArray:
3436
def make_surface(self) -> Surface: ...
3537
def replace(
3638
self,
37-
color: ColorLike,
38-
repcolor: ColorLike,
39+
color: _ColorLike,
40+
repcolor: _ColorLike,
3941
distance: float = 0,
4042
weights: SequenceLike[float] = (0.299, 0.587, 0.114),
4143
) -> None: ...
4244
def extract(
4345
self,
44-
color: ColorLike,
46+
color: _ColorLike,
4547
distance: float = 0,
4648
weights: SequenceLike[float] = (0.299, 0.587, 0.114),
4749
) -> PixelArray: ...

docs/reST/ref/mask.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ to store which parts collide.
7171
:param color: color used to check if the surface's pixels are within the
7272
given ``threshold`` range, this parameter is ignored if the optional
7373
``othersurface`` parameter is supplied
74-
:type color: Color or int or tuple(int, int, int, [int]) or list[int, int, int, [int]]
74+
:type color: :data:`pygame.typing.ColorLike`
7575
:param threshold: (optional) the threshold range used to check the difference
7676
between two colors (default is ``(0, 0, 0, 255)``)
77-
:type threshold: Color or int or tuple(int, int, int, [int]) or list[int, int, int, [int]]
77+
:type threshold: :data:`pygame.typing.ColorLike`
7878
:param Surface othersurface: (optional) used to check whether the pixels of
7979
the first surface are within the given ``threshold`` range of the pixels
8080
from this surface (default is ``None``)

src_c/mask.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,13 +1130,13 @@ mask_from_threshold(PyObject *self, PyObject *args, PyObject *kwargs)
11301130
}
11311131

11321132
if (!pg_MappedColorFromObj(rgba_obj_color, surf, &color,
1133-
PG_COLOR_HANDLE_INT)) {
1133+
PG_COLOR_HANDLE_ALL)) {
11341134
return NULL;
11351135
}
11361136

11371137
if (rgba_obj_threshold) {
11381138
if (!pg_MappedColorFromObj(rgba_obj_threshold, surf, &color_threshold,
1139-
PG_COLOR_HANDLE_INT)) {
1139+
PG_COLOR_HANDLE_ALL)) {
11401140
return NULL;
11411141
}
11421142
}

test/mask_test.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6325,21 +6325,19 @@ def alternate(func, set_value, unset_value, width, height):
63256325
def test_from_threshold(self):
63266326
"""Does mask.from_threshold() work correctly?"""
63276327

6328-
a = [16, 24, 32]
6328+
bpp = [16, 24, 32]
63296329

6330-
for i in a:
6330+
for i in bpp:
63316331
surf = pygame.surface.Surface((70, 70), 0, i)
63326332
surf.fill((100, 50, 200), (20, 20, 20, 20))
63336333
mask = pygame.mask.from_threshold(
63346334
surf, (100, 50, 200, 255), (10, 10, 10, 255)
63356335
)
63366336

6337-
rects = mask.get_bounding_rects()
6338-
63396337
self.assertEqual(mask.count(), 400)
63406338
self.assertEqual(mask.get_bounding_rects(), [pygame.Rect((20, 20, 20, 20))])
63416339

6342-
for i in a:
6340+
for i in bpp:
63436341
surf = pygame.surface.Surface((70, 70), 0, i)
63446342
surf2 = pygame.surface.Surface((70, 70), 0, i)
63456343
surf.fill((100, 100, 100))
@@ -6356,6 +6354,10 @@ def test_from_threshold(self):
63566354
self.assertEqual(mask.count(), 100)
63576355
self.assertEqual(mask.get_bounding_rects(), [pygame.Rect((40, 40, 10, 10))])
63586356

6357+
for color in [(20, 20, 20), 10, "green", pygame.Color("blue")]:
6358+
surf = pygame.surface.Surface((10, 10))
6359+
pygame.mask.from_threshold(surf, color, color)
6360+
63596361
def test_zero_size_from_surface(self):
63606362
"""Ensures from_surface can create masks from zero sized surfaces."""
63616363
for size in ((100, 0), (0, 100), (0, 0)):

test/pixelarray_test.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,6 @@ def test_iter(self):
10201020
self.assertEqual(iterations, 5)
10211021

10221022
def test_replace(self):
1023-
# print("replace start")
10241023
for bpp in (8, 16, 24, 32):
10251024
sf = pygame.Surface((10, 10), 0, bpp)
10261025
sf.fill((255, 0, 0))
@@ -1041,10 +1040,8 @@ def test_replace(self):
10411040
self.assertEqual(ar[3][6], oval)
10421041
self.assertEqual(ar[8][9], oval)
10431042
self.assertEqual(ar[9][9], oval)
1044-
# print("replace end")
10451043

10461044
def test_extract(self):
1047-
# print("extract start")
10481045
for bpp in (8, 16, 24, 32):
10491046
sf = pygame.Surface((10, 10), 0, bpp)
10501047
sf.fill((0, 0, 255))
@@ -1070,7 +1067,6 @@ def test_extract(self):
10701067
self.assertEqual(newar[3][6], white)
10711068
self.assertEqual(newar[8][9], black)
10721069
self.assertEqual(newar[9][9], black)
1073-
# print("extract end")
10741070

10751071
def test_2dslice_assignment(self):
10761072
w = 2 * 5 * 8

0 commit comments

Comments
 (0)