From 47c9d9e6196b21393ce997ad5b5ad01e9cf6aa5f Mon Sep 17 00:00:00 2001 From: Laurent Gautier Date: Sat, 8 Jan 2022 21:55:04 -0500 Subject: [PATCH 1/2] Remove dependency on numpy. Make gradients between 4 and 5 times faster while at it. --- pgzero/screen.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/pgzero/screen.py b/pgzero/screen.py index cdb27ef..b67e1a2 100644 --- a/pgzero/screen.py +++ b/pgzero/screen.py @@ -1,4 +1,3 @@ -import numpy as np import pygame import pygame.draw @@ -113,6 +112,29 @@ def textbox(self, *args, **kwargs): ptext.drawbox(*args, surf=self._surf, **kwargs) +def blit_gradient(start, stop, dest_surface): + """Blit a gradient into a destination surface. + + The function does not return anything as the gradient is written + in-place in the destination surface. + + Args: + start: The starting (top) color as a tuple (red, green, blue). + stop: The stopping (bottom) color as a tuple (red, green, blue). + dest_surface: A pygame.Surface to write the gradient into. + Returns: + None.""" + surface_compact = pygame.Surface((2, 2)) + pixelarray = pygame.PixelArray(surface_compact) + pixelarray[0][0] = start + pixelarray[0][1] = stop + pixelarray[1][0] = start + pixelarray[1][1] = stop + pygame.transform.smoothscale(surface_compact, + pygame.PixelArray(dest_surface).shape, + dest_surface=dest_surface) + + class Screen: """Interface to the screen.""" def _set_surface(self, surface): @@ -132,13 +154,7 @@ def fill(self, color, gcolor=None): if gcolor: start = make_color(color) stop = make_color(gcolor) - pixs = pygame.surfarray.pixels3d(self.surface) - h = self.height - - gradient = np.dstack( - [np.linspace(a, b, h) for a, b in zip(start, stop)][:3] - ) - pixs[...] = gradient + blit_gradient(start, stop, self.surface) else: self.surface.fill(make_color(color)) From c7d22fd76d3b4103864491680f6586f6cbefa769 Mon Sep 17 00:00:00 2001 From: Laurent Gautier Date: Sun, 9 Jan 2022 11:16:25 -0500 Subject: [PATCH 2/2] Linting. --- pgzero/screen.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pgzero/screen.py b/pgzero/screen.py index b67e1a2..02ae343 100644 --- a/pgzero/screen.py +++ b/pgzero/screen.py @@ -114,10 +114,10 @@ def textbox(self, *args, **kwargs): def blit_gradient(start, stop, dest_surface): """Blit a gradient into a destination surface. - + The function does not return anything as the gradient is written in-place in the destination surface. - + Args: start: The starting (top) color as a tuple (red, green, blue). stop: The stopping (bottom) color as a tuple (red, green, blue).