Mercurial > nagslang
view nagslang/mutators.py @ 31:c62ed518e5c8
Fix repor and add type-check to __eq__.
author | Simon Cross <hodgestar@gmail.com> |
---|---|
date | Sun, 01 Sep 2013 16:14:35 +0200 |
parents | 50babb330261 |
children | b9430b4a48da |
line wrap: on
line source
'''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): 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) # Colour RED = Colour((255, 0, 0)) GREEN = Colour((0, 255, 0)) BLUE = Colour((0, 0, 255))