Add a blend_add transform
[naja.git] / naja / resources / mutators.py
index 2a730de5663a3fb39b71e0ea354f02d364781347..36ef2297671d7cc5382d93d6f68430eea163faf3 100644 (file)
@@ -1,6 +1,11 @@
 '''Mutations to apply to images'''
 
-from pygame.transform import rotate, flip
+from pygame.transform import flip, rotate, scale
+
+from naja.constants import EIGHT_BIT_SCALE
+
+from pygame import surface
+import pygame.locals as pgl
 
 
 class Mutator(object):
@@ -27,6 +32,29 @@ def rotator(angle):
     return Mutator(rotate, angle)
 
 
+def scaler(size):
+    return Mutator(scale, size)
+
+
+def scale_multiplier(image, factor):
+    size = image.get_width() * factor, image.get_height() * factor
+    return scale(image, size)
+
+
+def blend_add(image, colour):
+    """Overlay the image with the given colour using BLEND_ADD"""
+    blend = surface.Surface(image.get_size())
+    blend.fill(colour)
+    # We return a copy
+    blended_image = image.copy()
+    blended_image.blit(blend, (0, 0), special_flags=pgl.BLEND_ADD)
+    return blended_image
+
+
+def blender(colour):
+    return Mutator(blend_add, tuple(colour))
+
+
 # Identity mutator
 NULL = Mutator(lambda x: x)
 
@@ -37,3 +65,5 @@ R270 = rotator(-90)
 
 FLIP_H = Mutator(flip, True, False)
 FLIP_V = Mutator(flip, False, True)
+
+EIGHT_BIT = Mutator(scale_multiplier, EIGHT_BIT_SCALE)