valeatory

ren'py - recoloring images on a curve

Jul 19th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. init python:
  2.     def recolor_hsv(img, h, s, v, weights = (.33,.33,.33)): #recolor by hsv values
  3.         return ImValueMap(im.MatrixColor(img,
  4.                     matrix_hue(h*360-45)
  5.                     * im.matrix.colorize((180*(1-v),0,180*(1-v)), # purple shadows, darker on lighter colors
  6.                                         "#fff")#plain highlights,
  7.                     * im.matrix.saturation(s*min(v+.5, 1), weights)#desaturate to match, evenly (darker colors get a little less saturated), can't go below 0
  8.                 ),
  9.                 ramp3(0,int((1-v)*255),255, 128+((v-0.5)*50))) #alter value at a curve (ish) (midpoint enhancement at extremes)
  10.  
  11.     import colorsys
  12.     import math
  13.     #a more pure hue rotation
  14.     def matrix_hue(hue):
  15.         v = 0.58
  16.  
  17.         hue = hue * math.pi / 180
  18.         c = math.cos(hue)
  19.         s = math.sin(hue)
  20.         vv_invc = v*v * (1-c)
  21.         vsin = v*math.sin(hue)
  22.  
  23.  
  24.         return im.matrix(vv_invc + c, vv_invc -vsin, vv_invc +vsin, 0, 0,
  25.                          vv_invc +vsin, vv_invc + c, vv_invc -vsin, 0, 0,
  26.                          vv_invc -vsin, vv_invc +vsin, vv_invc + c, 0, 0,
  27.                          0, 0, 0, 1, 0);
  28.  
  29.     class ImValueMap(im.ImageBase):
  30.         def __init__(self, img, vmap=im.identity, **properties):
  31.  
  32.             img = im.image(img)
  33.  
  34.             super(ImValueMap, self).__init__(img, vmap, **properties)
  35.  
  36.             self.image = img
  37.             self.vmap = vmap
  38.  
  39.         def get_hash(self):
  40.             return self.image.get_hash()
  41.  
  42.         def load(self):
  43.  
  44.             surf = im.cache.get(self.image)
  45.  
  46.             rv = renpy.display.pgrender.surface(surf.get_size(), True)
  47.  
  48.             renpy.display.module.map(surf, rv, self.vmap, self.vmap, self.vmap, im.identity)
  49.  
  50.             return rv
  51.  
  52.         def predict_files(self):
  53.             return self.image.predict_files()
  54.  
  55.     def ramp3(start, midpoint, end, mid_val = 128):
  56.         """
  57.        Returns a 256 character linear ramp, where the first character has
  58.        the value start, the midpoint-th character has the value mid_val,
  59.        and the last character has the value end. Such a
  60.        ramp can be used as a map argument of im.Map.
  61.        """
  62.  
  63.         rv = im.ramp_cache.get((start, midpoint, mid_val, end), None)
  64.         if rv is None:
  65.  
  66.             chars = [ ]
  67.  
  68.             for i in range(midpoint):
  69.                 i = i / float(midpoint-1) #i is 0 to one
  70.                 chars.append(chr(int( start*(1-i) + mid_val*i ) ) )
  71.  
  72.             for i in range(255-midpoint):
  73.                 i = i / (255.0-midpoint)
  74.                 chars.append(chr(int( mid_val*(1-i) + end*i ) ) )
  75.  
  76.             rv = "".encode("utf-8").join(chars)
  77.             im.ramp_cache[start, midpoint, mid_val, end] = rv
  78.  
  79.         return rv
Advertisement
Add Comment
Please, Sign In to add comment