changeset 4551 | d357e2acc856 |
parent 4528 | e09377955af8 |
child 4559 | 51a521ad7dd3 |
4550:80dd9e83dad9 | 4551:d357e2acc856 |
---|---|
40 ValueError: css color 'pcmyka(100,0,0,0)' has wrong number of components |
40 ValueError: css color 'pcmyka(100,0,0,0)' has wrong number of components |
41 ''' |
41 ''' |
42 import math, re, functools |
42 import math, re, functools |
43 from reportlab import isPy3, cmp |
43 from reportlab import isPy3, cmp |
44 from reportlab.lib.rl_accel import fp_str |
44 from reportlab.lib.rl_accel import fp_str |
45 from reportlab.lib.utils import asNative, isStr |
45 from reportlab.lib.utils import asNative, isStr, safer_globals |
46 import collections |
46 import collections |
47 from ast import literal_eval |
|
47 |
48 |
48 class Color: |
49 class Color: |
49 """This class is used to represent color. Components red, green, blue |
50 """This class is used to represent color. Components red, green, blue |
50 are in the range 0 (dark) to 1 (full intensity).""" |
51 are in the range 0 (dark) to 1 (full intensity).""" |
51 |
52 |
767 _re_css = re.compile(r'^\s*(pcmyk|cmyk|rgb|hsl)(a|)\s*\(\s*([^)]*)\)\s*$') |
768 _re_css = re.compile(r'^\s*(pcmyk|cmyk|rgb|hsl)(a|)\s*\(\s*([^)]*)\)\s*$') |
768 class cssParse: |
769 class cssParse: |
769 def pcVal(self,v): |
770 def pcVal(self,v): |
770 v = v.strip() |
771 v = v.strip() |
771 try: |
772 try: |
772 c=eval(v[:-1]) |
773 c=float(v[:-1]) |
773 if not isinstance(c,(float,int)): raise ValueError |
|
774 c=min(100,max(0,c))/100. |
774 c=min(100,max(0,c))/100. |
775 except: |
775 except: |
776 raise ValueError('bad percentage argument value %r in css color %r' % (v,self.s)) |
776 raise ValueError('bad percentage argument value %r in css color %r' % (v,self.s)) |
777 return c |
777 return c |
778 |
778 |
780 return int(self.pcVal(v)*255+0.5)/255. |
780 return int(self.pcVal(v)*255+0.5)/255. |
781 |
781 |
782 def rgbVal(self,v): |
782 def rgbVal(self,v): |
783 v = v.strip() |
783 v = v.strip() |
784 try: |
784 try: |
785 c=eval(v[:]) |
785 c=float(v) |
786 if not isinstance(c,(int,float)): raise ValueError |
786 if 0<c<=1: c *= 255 |
787 if isinstance(c,float) and 0<=c<=1: c *= 255 |
|
788 return int(min(255,max(0,c)))/255. |
787 return int(min(255,max(0,c)))/255. |
789 except: |
788 except: |
790 raise ValueError('bad argument value %r in css color %r' % (v,self.s)) |
789 raise ValueError('bad argument value %r in css color %r' % (v,self.s)) |
791 |
790 |
792 def hueVal(self,v): |
791 def hueVal(self,v): |
793 v = v.strip() |
792 v = v.strip() |
794 try: |
793 try: |
795 c=eval(v[:]) |
794 c=float(v) |
796 if not isinstance(c,(int,float)): raise ValueError |
|
797 return ((c%360+360)%360)/360. |
795 return ((c%360+360)%360)/360. |
798 except: |
796 except: |
799 raise ValueError('bad hue argument value %r in css color %r' % (v,self.s)) |
797 raise ValueError('bad hue argument value %r in css color %r' % (v,self.s)) |
800 |
798 |
801 def alphaVal(self,v,c=1,n='alpha'): |
799 def alphaVal(self,v,c=1,n='alpha'): |
802 try: |
800 try: |
803 a = eval(v.strip()) |
801 a = float(v) |
804 if not isinstance(a,(int,float)): raise ValueError |
|
805 return min(c,max(0,a)) |
802 return min(c,max(0,a)) |
806 except: |
803 except: |
807 raise ValueError('bad %s argument value %r in css color %r' % (n,v,self.s)) |
804 raise ValueError('bad %s argument value %r in css color %r' % (n,v,self.s)) |
808 |
805 |
809 _n_c = dict(pcmyk=(4,100,True,False),cmyk=(4,1,True,False),hsl=(3,1,False,True),rgb=(3,1,False,False)) |
806 _n_c = dict(pcmyk=(4,100,True,False),cmyk=(4,1,True,False),hsl=(3,1,False,True),rgb=(3,1,False,False)) |
862 if arg in self.extraColorsNS: return self.extraColorsNS[arg] |
859 if arg in self.extraColorsNS: return self.extraColorsNS[arg] |
863 C = getAllNamedColors() |
860 C = getAllNamedColors() |
864 s = arg.lower() |
861 s = arg.lower() |
865 if s in C: return C[s] |
862 if s in C: return C[s] |
866 try: |
863 try: |
867 return toColor(eval(arg)) |
864 return toColor(eval(arg,safer_globals())) |
868 except: |
865 except: |
869 pass |
866 pass |
870 |
867 |
871 try: |
868 try: |
872 return HexColor(arg) |
869 return HexColor(arg) |