source: nagslang/mutators.py@ 24:50babb330261

Last change on this file since 24:50babb330261 was 24:50babb330261, checked in by Stefano Rivera <stefano@…>, 8 years ago

Forgot to add mutators...

File size: 1.3 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 return (self._func is other._func) and (self._args == other._args)
20
21 def __repor__(self):
22 return '<%s %r>' % (self.__class__.__name__, self._args)
23
24
25class Colour(Mutator):
26 '''Overlay a colour onto an image'''
27 def __init__(self, colour, blend=pygame.locals.BLEND_RGBA_MULT):
28 super(Colour, self).__init__(Colour.colour, colour, blend)
29
30 @classmethod
31 def colour(self, image, colour, blend):
32 image = image.copy()
33 overlay = pygame.surface.Surface(image.get_size(),
34 pygame.locals.SRCALPHA, image)
35 overlay.fill(colour)
36 image.blit(overlay, (0, 0), None, blend)
37 return image
38
39
40# Identity mutator
41NULL = Mutator(lambda x: x)
42
43# Rotation
44R90 = Mutator(rotate, 90)
45R180 = Mutator(rotate, 180)
46R270 = Mutator(rotate, -90)
47
48# Colour
49RED = Colour((255, 0, 0))
50GREEN = Colour((0, 255, 0))
51BLUE = Colour((0, 0, 255))
Note: See TracBrowser for help on using the repository browser.