view nagslang/mutators.py @ 56:b9430b4a48da

Now with a werewolf
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 01 Sep 2013 18:51:06 +0200
parents c62ed518e5c8
children 72a91d64c088
line wrap: on
line source

'''Mutations to apply to images'''

import pygame
from pygame.transform import rotate, flip


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):
        if not isinstance(other, Mutator):
            return NotImplemented
        return (self._func is other._func) and (self._args == other._args)

    def __repr__(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)

FLIP_H = Mutator(flip, True, False)
FLIP_V = Mutator(flip, False, True)

# Colour
RED = Colour((255, 0, 0))
GREEN = Colour((0, 255, 0))
BLUE = Colour((0, 0, 255))