Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Common Tasks
#11
Color Surface
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:
Reply


Messages In This Thread
Common Tasks - by metulburr - Nov-05-2017, 11:55 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:22 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:23 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:23 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:24 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:26 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:26 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:26 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:27 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:27 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:27 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:28 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:28 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:28 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:28 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:28 PM
RE: Common Tasks - by metulburr - Feb-01-2020, 07:40 PM
RE: Common Tasks - by metulburr - Feb-02-2020, 06:20 PM
RE: Common Tasks - by metulburr - Feb-06-2020, 11:57 AM
RE: Common Tasks - by metulburr - Jul-06-2020, 10:46 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020