code: def _blend(self, new, old, ratio):
          
          # Don't bother blending if none is required.
          if new is None:
              return old
  
          # Check colour blend cache for a quick answer.
          key = (min(new, old), max(new, old))
          if key in self._blends:
              return self._blends[key]
  
          # No quick answer - do it the long way...  First lookup the RGB values
          # for both colours and blend.
          (r1, g1, b1) = self.palette[new * 3:new * 3 + 3]
          (r2, g2, b2) = self.palette[old * 3:old * 3 + 3]
  
          # Helper function to blend RGB values.
          def f(c1, c2):
              return ((c1 * ratio) + (c2 * (100 - ratio))) // 100
  
          r = f(r1, r2)
          g = f(g1, g2)
          b = f(b1, b2)
  
          # Now do the reverse lookup...
          nearest = (256 ** 2) * 3
          match = 0
          for c in range(self.colours):
              (rc, gc, bc) = self.palette[c * 3:c * 3 + 3]
              diff = sqrt(((rc - r) * 0.3) ** 2 + ((gc - g) * 0.59) ** 2 +
                          ((bc - b) * 0.11) ** 2)
              if diff < nearest:
                  nearest = diff
                  match = c
  
          # Save off the answer and return it
          self._blends[key] = match
          return match
docs: Blend the new colour with the old according to the ratio.
  
          :param new: The new colour (or None if not required).
          :param old: The old colour.
          :param ratio: The ratio to blend new and old
          :returns: the new colour index to use for the required blend.