changeset 24:50babb330261

Forgot to add mutators...
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 01 Sep 2013 15:35:48 +0200
parents 499fb96796e9
children e93eac7cf8c2
files nagslang/mutators.py
diffstat 1 files changed, 51 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nagslang/mutators.py	Sun Sep 01 15:35:48 2013 +0200
@@ -0,0 +1,51 @@
+'''Mutations to apply to images'''
+
+import pygame
+from pygame.transform import rotate
+
+
+class Mutator(object):
+    def __init__(self, func, *args):
+        self._func = func
+        self._args = tuple(args)
+
+    def __call__(self, image):
+        return self._func(image, *self._args)
+
+    def __hash__(self):
+        return hash((self._func, self._args))
+
+    def __eq__(self, other):
+        return (self._func is other._func) and (self._args == other._args)
+
+    def __repor__(self):
+        return '<%s %r>' % (self.__class__.__name__, self._args)
+
+
+class Colour(Mutator):
+    '''Overlay a colour onto an image'''
+    def __init__(self, colour, blend=pygame.locals.BLEND_RGBA_MULT):
+        super(Colour, self).__init__(Colour.colour, colour, blend)
+
+    @classmethod
+    def colour(self, image, colour, blend):
+        image = image.copy()
+        overlay = pygame.surface.Surface(image.get_size(),
+                                         pygame.locals.SRCALPHA, image)
+        overlay.fill(colour)
+        image.blit(overlay, (0, 0), None, blend)
+        return image
+
+
+# Identity mutator
+NULL = Mutator(lambda x: x)
+
+# Rotation
+R90 = Mutator(rotate, 90)
+R180 = Mutator(rotate, 180)
+R270 = Mutator(rotate, -90)
+
+# Colour
+RED = Colour((255, 0, 0))
+GREEN = Colour((0, 255, 0))
+BLUE = Colour((0, 0, 255))