Color

Set of color filters

Alpha & color

pgext.color.setColor(surface, tuple color)

Set all pixels with alpha > 0 to color. If color has Alpha channel (RGBA) then function is slower because alpha blending is in use. With this you can create nice "fog-like" effect.
Alpha channel is supported from version 1.18
Original surface is modified.

Example
pgext.color.setColor(image, (120,0,200))
pgext.color.setColor(image, (120,0,200, 80))
   

pgext.color.setAlpha(surface, int shift [, int method=0])

Change alpha channel value of surface. You can use only 32bit RGBA surfaces.
Original surface is modified.
method:
    0: alpha += shift
    1: alpha = shift
    2: alpha = alpha * (shift/100.0)

Example
# set 40% alpha
pgext.color.setAlpha(image, 40, 2)
  

pgext.color.alphaMask(surface, mask [, method=0])

Create alpha-masked copy of surface.
You can use specify how to apply mask to surface alpha. Here is list of available methods:
Method 0:  original.alpha = (mask.value/100) * original.alpha
Method 1:  original.alpha = mask.alpha
Method 2:  original.alpha = (original.alpha / 255) * (mask.alpha / 255)

# Bitmask
Method 3:  original.alpha = (mask.alpha > 0) ? original.alpha : 0
Changed in version 1.14.
Changed: Original surface is not modified and function returns new surface.

Examples
# method 0
image = pygame.image.load("source_rgb.png")
image_mask = pygame.image.load("source_mask.png")
image = pgext.color.alphaMask(image, image_mask)
pygame.image.save(image, "out.png")
   
# method 1
image = pygame.image.load("source_rgb.png")
image_mask = pygame.image.load("source_rgba.png")
image = pgext.color.alphaMask(image, image_mask, 1)
pygame.image.save(image, "out.png")
   

HSV/HSL

These methods are slower than other color filters because every pixel is converted to HSV/HSL (hue, saturation, value/lightness). You can use faster alternatives like desaturate, greyscale, brightness, multiply.

pgext.color.hue(surface, int shift [, int method=0 ])

Change surface hue by shift.
Original surface is modified.
method:
    0: hue += shift
    1: hue = shift

Example
pgext.color.hue(image, 80)
  

pgext.color.saturation(surface, int shift [, int method=0 ])

Change surface saturation by/to shift (depends on method).
Original surface is modified.
method:
    0: saturation += shift
    1: saturation = shift
    2: saturation = saturation * (shift/100.0)

Example
pgext.color.saturation(image, 30, 2)
  

pgext.color.value(surface, int shift [, int method=0 ])

Change surface value (HSV) by/to shift (depends on method).
Original surface is modified.
method:
    0: value += shift
    1: value = shift
    2: value = value * (shift/100.0)

Example
pgext.color.value(image, -50)
  

pgext.color.lightness(surface, int shift [, int method=0 ])

Change surface lightness (HSL) by/to shift (depends on method).
Original surface is modified.
method:
    0: lightness += shift
    1: lightness = shift
    2: lightness = lightness * (shift/100.0)

Example
pgext.color.lightness(image, 20)
  

Other effects

pgext.color.multiply(surface, float shift [, bool r_channel=1 [, bool g_channel=1 [, bool b_channel=1 ]]])

Multiply RGB channels by shift. Usable for faster brightness manipulation. You can specify channels to be multiplied by shift.
Original surface is modified.
surface[RGB] = surface[RGB] * shift

Examples
# brighter
pgext.color.multiply(image, 2.5)

# darker
pgext.color.multiply(image, 0.4)

# set R and B channels to 0
pgext.color.multiply(image, 0.0, 1, 0, 1)
    

pgext.color.greyscale(surface [, method=0 ])

Set all pixels' color to greyscale.
Original surface is modified.

Example
# method 0 (average RGB)
pgext.color.greyscale(image)

# method 1 (value)
pgext.color.greyscale(image, 1)

# method 2 (lightness)
pgext.color.greyscale(image, 2)
    

pgext.color.invert(surface)

Original surface is modified.
  

pgext.color.contrast(surface, contrast_value)

Adjust surface contrast.
Original surface is modified.

Example
pgext.color.contrast(image, 80)
pgext.color.contrast(image, -80)
   

pgext.color.brightness(surface, contrast_value)

Adjust surface brightness.
Original surface is modified.

Example
pgext.color.brightness(image, 80)
pgext.color.brightness(image, -80)
   

pgext.color.desaturate(surface, float value)

Decrease saturation of surface.
Original surface is modified.

Example
pgext.color.desaturate(image, 0.5)
  

pgext.color.colorize(surface, int check_hue, int new_hue [, int value_shift=0 [, int saturation_shift=0 ]])

Pixels with hue == check_hue will be changed to new_hue.
Original surface is modified.

Example
pgext.color.colorize(image, 0, 200, -30, 10)