Source

pygamegsoc12 / alchemymadness / sprite.py

Diff from to

alchemymadness/sprite.py

     changed should be set to True whenever mask_rect is modified
     """
     rect = Rect(0,0,10,10)
-    changed = False # True if sprite image need to be colored
+    changed = False
     fill_flags = pygame.locals.BLEND_RGBA_MULT
+    target_col = None
+    target_rect = None
 
     def __init__(self, color, *groups):
         """
     def color(self, value):
         self._color = value
         self.changed = True
+        self.dirty = 1
+
+    def mix_color(self, color, ratio=0.5):
+        """Mix a color with specified ratio
+
+        col: tuple containing rgb colour
+        ratio: color mix ratio (1 - max new color)
+        """
+        self.target_col = tuple(self.color[i] * (1 - ratio) + color[i] * ratio for i in range(len(self.color)))
+        self.diff_col = tuple(self.target_col[i] - self.color[i] for i in range(len(self.color)))
+
+    def update(self, time):
+        """Update color and mask_rect"""
+        if self.target_col is not None:
+            self.changed = True
+            diff = (self.diff_col[0] * (time/500.),
+                    self.diff_col[1] * (time/500.),
+                    self.diff_col[2] * (time/500.))
+            col = [self.color[0] + diff[0], self.color[1] + diff[1],
+                   self.color[2] + diff[2]]
+            for i in range(3):
+                if diff[i] > 0:
+                    col[i] = min(col[i], self.target_col[i])
+                else:
+                    col[i] = max(col[i], self.target_col[i])
+
+            self.color = tuple(col)
+            if self.color == self.target_col:
+                self.target_col = None