Factor light fitting rendering out into a function.
[tabakrolletjie.git] / tabakrolletjie / transforms.py
index 70ff036160618b51c2c5cd3651310172a23f6a36..88f516169e02dfe46ff22523eb28a353ad421a75 100644 (file)
@@ -2,6 +2,8 @@
 
 import pygame.surface
 
+import pygame.locals as pgl
+
 
 class Transform(object):
 
@@ -11,11 +13,22 @@ class Transform(object):
         for k in self.ARGS:
             setattr(self, k, kw.pop(k))
         assert not kw
-        self.hash = "%s: <%s>" % (
+        self._repr = "%s: <%s>" % (
             self.__class__.__name__,
             ", ".join("%s=%s" % (k, getattr(self, k)) for k in self.ARGS))
         return
 
+    def __repr__(self):
+        return self._repr
+
+    def __hash__(self):
+        return hash(self._repr)
+
+    def __eq__(self, other):
+        if not isinstance(other, Transform):
+            return NotImplemented
+        return self._repr == other._repr
+
     def apply(self, surface):
         raise NotImplementedError("Transforms should implement .apply")
 
@@ -38,3 +51,26 @@ class Overlay(Transform):
         over.fill(self.colour)
         surface.blit(over, (0, 0), None)
         return surface
+
+
+class Multiply(Transform):
+    """ Apply a colour by multiplying. """
+
+    ARGS = ["colour"]
+
+    def apply(self, surface):
+        mult = pygame.surface.Surface(surface.get_size())
+        mult = mult.convert_alpha()
+        mult.fill(self.colour)
+        surface.blit(mult, (0, 0), None, pgl.BLEND_RGBA_MULT)
+        return surface
+
+
+class Alpha(Transform):
+    """ Make translucent. """
+
+    ARGS = ["alpha"]
+
+    def apply(self, surface):
+        surface.fill((255, 255, 255, self.alpha), None, pgl.BLEND_RGBA_MULT)
+        return surface