Skip to content
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

Allow all color types mask.from_threshold and fix typing for PixelArray methods #3164

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 6 additions & 4 deletions buildconfig/stubs/pygame/pixelarray.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: ...
Expand Down
4 changes: 2 additions & 2 deletions docs/reST/ref/mask.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``)
Expand Down
4 changes: 2 additions & 2 deletions src_c/mask.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
12 changes: 7 additions & 5 deletions test/mask_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)):
Expand Down
4 changes: 0 additions & 4 deletions test/pixelarray_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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
Expand Down
Loading