Feb-01-2020, 07:27 PM
Color Surface
An example of 2 different methods to draw a new color over a surface within pygame
An example of 2 different methods to draw a new color over a surface within pygame
#no other libs requires other than pygame def colorize(image, newColor): """ Create a "colorized" copy of a surface (replaces RGB values with the given color, preserving the per-pixel alphas of original). :param image: Surface to create a colorized copy of :param newColor: RGB color to use (original alpha values are preserved) :return: New colorized Surface instance """ image = image.copy() # zero out RGB values image.fill((0, 0, 0, 255), None, pg.BLEND_RGBA_MULT) # add in new RGB values image.fill(newColor[0:3] + (0,), None, pg.BLEND_RGBA_ADD) return image #requires numpy module to be installed def color_surface(surface, red, green, blue): '''requires numpy module on users end''' arr = pg.surfarray.pixels3d(surface) arr[:,:,0] = red arr[:,:,1] = green arr[:,:,2] = blue
Recommended Tutorials: