Add support for transforming images when loading them (pt II).
authorSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 19:09:20 +0000 (21:09 +0200)
committerSimon Cross <hodgestar@gmail.com>
Fri, 9 Sep 2016 19:09:20 +0000 (21:09 +0200)
tabakrolletjie/transforms.py [new file with mode: 0644]

diff --git a/tabakrolletjie/transforms.py b/tabakrolletjie/transforms.py
new file mode 100644 (file)
index 0000000..70ff036
--- /dev/null
@@ -0,0 +1,40 @@
+""" Image transformations for use with the image loader. """
+
+import pygame.surface
+
+
+class Transform(object):
+
+    ARGS = []
+
+    def __init__(self, **kw):
+        for k in self.ARGS:
+            setattr(self, k, kw.pop(k))
+        assert not kw
+        self.hash = "%s: <%s>" % (
+            self.__class__.__name__,
+            ", ".join("%s=%s" % (k, getattr(self, k)) for k in self.ARGS))
+        return
+
+    def apply(self, surface):
+        raise NotImplementedError("Transforms should implement .apply")
+
+
+class NullTransform(Transform):
+    """ Do nothing. """
+
+    def apply(self, surface):
+        return surface
+
+
+class Overlay(Transform):
+    """ Apply a colour overlay. """
+
+    ARGS = ["colour"]
+
+    def apply(self, surface):
+        over = pygame.surface.Surface(surface.get_size())
+        over = over.convert_alpha()
+        over.fill(self.colour)
+        surface.blit(over, (0, 0), None)
+        return surface