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
Changes from 3 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
4 changes: 2 additions & 2 deletions docs/reST/ref/mask.rst
Original file line number Diff line number Diff line change
@@ -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``)
4 changes: 2 additions & 2 deletions src_c/mask.c
Original file line number Diff line number Diff line change
@@ -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;
}
}
16 changes: 13 additions & 3 deletions src_c/pixelarray_methods.c
Original file line number Diff line number Diff line change
@@ -46,6 +46,16 @@ _get_color_from_object(PyObject *val, SDL_Surface *surf, Uint32 *color)
val, surf, color, PG_COLOR_HANDLE_INT | PG_COLOR_HANDLE_RESTRICT_SEQ);
}

static int
_get_color_from_object_all(PyObject *val, SDL_Surface *surf, Uint32 *color)
{
if (!val) {
return 0;
}

return pg_MappedColorFromObj(val, surf, color, PG_COLOR_HANDLE_ALL);
}

/**
* Retrieves a single pixel located at index from the surface pixel
* array.
@@ -375,8 +385,8 @@ _replace_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
format = surf->format;
bpp = PG_SURF_BytesPerPixel(surf);

if (!_get_color_from_object(delcolor, surf, &dcolor) ||
!_get_color_from_object(replcolor, surf, &rcolor)) {
if (!_get_color_from_object_all(delcolor, surf, &dcolor) ||
!_get_color_from_object_all(replcolor, surf, &rcolor)) {
return 0;
}

@@ -581,7 +591,7 @@ _extract_color(pgPixelArrayObject *array, PyObject *args, PyObject *kwds)
black = SDL_MapRGBA(format, 0, 0, 0, 255);
white = SDL_MapRGBA(format, 255, 255, 255, 255);

if (!_get_color_from_object(excolor, surf, &color)) {
if (!_get_color_from_object_all(excolor, surf, &color)) {
Py_DECREF(new_array);
return 0;
}
12 changes: 7 additions & 5 deletions test/mask_test.py
Original file line number Diff line number Diff line change
@@ -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)):
14 changes: 10 additions & 4 deletions test/pixelarray_test.py
Original file line number Diff line number Diff line change
@@ -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,13 @@ 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")

for color in [(20, 20, 20), 10, "green", pygame.Color("blue")]:
sf = pygame.Surface((10, 10))
ar = pygame.PixelArray(sf)
ar.replace(color, color)

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 +1072,11 @@ 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")

for color in [(20, 20, 20), 10, "green", pygame.Color("blue")]:
sf = pygame.Surface((10, 10))
ar = pygame.PixelArray(sf)
ar.extract(color)

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