Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- init python:
- def recolor_hsv(img, h, s, v, weights = (.33,.33,.33)): #recolor by hsv values
- return ImValueMap(im.MatrixColor(img,
- matrix_hue(h*360-45)
- * im.matrix.colorize((180*(1-v),0,180*(1-v)), # purple shadows, darker on lighter colors
- "#fff")#plain highlights,
- * 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
- ),
- ramp3(0,int((1-v)*255),255, 128+((v-0.5)*50))) #alter value at a curve (ish) (midpoint enhancement at extremes)
- import colorsys
- import math
- #a more pure hue rotation
- def matrix_hue(hue):
- v = 0.58
- hue = hue * math.pi / 180
- c = math.cos(hue)
- s = math.sin(hue)
- vv_invc = v*v * (1-c)
- vsin = v*math.sin(hue)
- return im.matrix(vv_invc + c, vv_invc -vsin, vv_invc +vsin, 0, 0,
- vv_invc +vsin, vv_invc + c, vv_invc -vsin, 0, 0,
- vv_invc -vsin, vv_invc +vsin, vv_invc + c, 0, 0,
- 0, 0, 0, 1, 0);
- class ImValueMap(im.ImageBase):
- def __init__(self, img, vmap=im.identity, **properties):
- img = im.image(img)
- super(ImValueMap, self).__init__(img, vmap, **properties)
- self.image = img
- self.vmap = vmap
- def get_hash(self):
- return self.image.get_hash()
- def load(self):
- surf = im.cache.get(self.image)
- rv = renpy.display.pgrender.surface(surf.get_size(), True)
- renpy.display.module.map(surf, rv, self.vmap, self.vmap, self.vmap, im.identity)
- return rv
- def predict_files(self):
- return self.image.predict_files()
- def ramp3(start, midpoint, end, mid_val = 128):
- """
- Returns a 256 character linear ramp, where the first character has
- the value start, the midpoint-th character has the value mid_val,
- and the last character has the value end. Such a
- ramp can be used as a map argument of im.Map.
- """
- rv = im.ramp_cache.get((start, midpoint, mid_val, end), None)
- if rv is None:
- chars = [ ]
- for i in range(midpoint):
- i = i / float(midpoint-1) #i is 0 to one
- chars.append(chr(int( start*(1-i) + mid_val*i ) ) )
- for i in range(255-midpoint):
- i = i / (255.0-midpoint)
- chars.append(chr(int( mid_val*(1-i) + end*i ) ) )
- rv = "".encode("utf-8").join(chars)
- im.ramp_cache[start, midpoint, mid_val, end] = rv
- return rv
Advertisement
Add Comment
Please, Sign In to add comment