c5eb05f64e7d85739910eb2f89e1e4f428095070
[tabakrolletjie.git] / tabakrolletjie / transforms.py
1 """ Image transformations for use with the image loader. """
2
3 import pygame.surface
4
5
6 class Transform(object):
7
8     ARGS = []
9
10     def __init__(self, **kw):
11         for k in self.ARGS:
12             setattr(self, k, kw.pop(k))
13         assert not kw
14         self._repr = "%s: <%s>" % (
15             self.__class__.__name__,
16             ", ".join("%s=%s" % (k, getattr(self, k)) for k in self.ARGS))
17         return
18
19     def __repr__(self):
20         return self._repr
21
22     def __hash__(self):
23         return hash(self._repr)
24
25     def __eq__(self, other):
26         if not isinstance(other, Transform):
27             return NotImplemented
28         return self._repr == other._repr
29
30     def apply(self, surface):
31         raise NotImplementedError("Transforms should implement .apply")
32
33
34 class NullTransform(Transform):
35     """ Do nothing. """
36
37     def apply(self, surface):
38         return surface
39
40
41 class Overlay(Transform):
42     """ Apply a colour overlay. """
43
44     ARGS = ["colour"]
45
46     def apply(self, surface):
47         over = pygame.surface.Surface(surface.get_size())
48         over = over.convert_alpha()
49         over.fill(self.colour)
50         surface.blit(over, (0, 0), None)
51         return surface