source: nagslang/mutators.py @ 31:c62ed518e5c8

Last change on this file since 31:c62ed518e5c8 was 31:c62ed518e5c8, checked in by Simon Cross <hodgestar@…>, 7 years ago

Fix repor and add type-check to eq.

File size: 1.4 KB
Line 
1'''Mutations to apply to images'''
2
3import pygame
4from pygame.transform import rotate
5
6
7class Mutator(object):
8    def __init__(self, func, *args):
9        self._func = func
10        self._args = tuple(args)
11
12    def __call__(self, image):
13        return self._func(image, *self._args)
14
15    def __hash__(self):
16        return hash((self._func, self._args))
17
18    def __eq__(self, other):
19        if not isinstance(other, Mutator):
20            return NotImplemented
21        return (self._func is other._func) and (self._args == other._args)
22
23    def __repr__(self):
24        return '<%s %r>' % (self.__class__.__name__, self._args)
25
26
27class Colour(Mutator):
28    '''Overlay a colour onto an image'''
29    def __init__(self, colour, blend=pygame.locals.BLEND_RGBA_MULT):
30        super(Colour, self).__init__(Colour.colour, colour, blend)
31
32    @classmethod
33    def colour(self, image, colour, blend):
34        image = image.copy()
35        overlay = pygame.surface.Surface(image.get_size(),
36                                         pygame.locals.SRCALPHA, image)
37        overlay.fill(colour)
38        image.blit(overlay, (0, 0), None, blend)
39        return image
40
41
42# Identity mutator
43NULL = Mutator(lambda x: x)
44
45# Rotation
46R90 = Mutator(rotate, 90)
47R180 = Mutator(rotate, 180)
48R270 = Mutator(rotate, -90)
49
50# Colour
51RED = Colour((255, 0, 0))
52GREEN = Colour((0, 255, 0))
53BLUE = Colour((0, 0, 255))
Note: See TracBrowser for help on using the repository browser.