1 #!/usr/bin/env python
2 import coloreffect
4 class C(coloreffect.ColorEffect):
5 def __init__(self):
6 coloreffect.ColorEffect.__init__(self)
7 self.OptionParser.add_option("--r",
8 action="store", type="string",
9 dest="rFunction", default="r",
10 help="red channel function")
11 self.OptionParser.add_option("--g",
12 action="store", type="string",
13 dest="gFunction", default="g",
14 help="green channel function")
15 self.OptionParser.add_option("--b",
16 action="store", type="string",
17 dest="bFunction", default="b",
18 help="blue channel function")
19 self.OptionParser.add_option("--tab",
20 action="store", type="string",
21 dest="tab",
22 help="The selected UI-tab when OK was pressed")
23 self.OptionParser.add_option("--scale",
24 action="store", type="string",
25 dest="scale",
26 help="The input (r,g,b) range")
28 def normalize(self, v):
29 if v<0:
30 return 0.0
31 if v > float(self.options.scale):
32 return float(self.options.scale)
33 return v
35 def _hexstr(self,r,g,b):
36 return '%02x%02x%02x' % (int(round(r)),int(round(g)),int(round(b)))
38 def colmod(self,_r,_g,_b):
39 factor = 255.0/float(self.options.scale)
40 r=float(_r)/factor
41 g=float(_g)/factor
42 b=float(_b)/factor
44 # add stuff to be accessible from within the custom color function here.
45 safeenv = {'__builtins__':{},'r':r,'g':g,'b':b}
47 try:
48 r2=self.normalize(eval(self.options.rFunction,safeenv))
49 g2=self.normalize(eval(self.options.gFunction,safeenv))
50 b2=self.normalize(eval(self.options.bFunction,safeenv))
51 except:
52 return self._hexstr(255.0,0.0,0.0)
53 return self._hexstr(r2*factor,g2*factor,b2*factor)
55 c = C()
56 c.affect()