34
34
35
35
36
36
def hex2color (s ):
37
- "Convert hex string (like html uses, eg, #efefef) to a r,g,b tuple"
38
- return tuple ([int (n , 16 )/ 255.0 for n in (s [1 :3 ], s [3 :5 ], s [5 :7 ])])
37
+ "Convert hex string (like html uses, eg, #efefef) to a r,g,b tuple"
38
+ return tuple ([int (n , 16 )/ 255.0 for n in (s [1 :3 ], s [3 :5 ], s [5 :7 ])])
39
39
40
40
41
41
class RGBA (traits .HasTraits ):
42
- # r,g,b,a in the range 0-1 with default color 0,0,0,1 (black)
43
- r = traits .Range (0. , 1. , 0. )
44
- g = traits .Range (0. , 1. , 0. )
45
- b = traits .Range (0. , 1. , 0. )
46
- a = traits .Range (0. , 1. , 1. )
42
+ # r,g,b,a in the range 0-1 with default color 0,0,0,1 (black)
43
+ r = traits .Range (0. , 1. , 0. )
44
+ g = traits .Range (0. , 1. , 0. )
45
+ b = traits .Range (0. , 1. , 0. )
46
+ a = traits .Range (0. , 1. , 1. )
47
47
48
- def __init__ (self , r = 0. , g = 0. , b = 0. , a = 1. ):
49
- self .r = r
50
- self .g = g
51
- self .b = b
52
- self .a = a
48
+ def __init__ (self , r = 0. , g = 0. , b = 0. , a = 1. ):
49
+ self .r = r
50
+ self .g = g
51
+ self .b = b
52
+ self .a = a
53
53
54
- def __repr__ (self ):
55
- return 'r,g,b,a = (%1.2f, %1.2f, %1.2f, %1.2f)' % \
56
- (self .r , self .g , self .b , self .a )
54
+ def __repr__ (self ):
55
+ return 'r,g,b,a = (%1.2f, %1.2f, %1.2f, %1.2f)' % \
56
+ (self .r , self .g , self .b , self .a )
57
57
58
58
59
59
def tuple_to_rgba (ob , name , val ):
60
- tup = [float (x ) for x in val ]
61
- if len (tup )== 3 :
62
- r ,g ,b = tup
63
- return RGBA (r ,g ,b )
64
- elif len (tup )== 4 :
65
- r ,g ,b ,a = tup
66
- return RGBA (r ,g ,b ,a )
67
- else :
68
- raise ValueError
60
+ tup = [float (x ) for x in val ]
61
+ if len (tup )== 3 :
62
+ r ,g ,b = tup
63
+ return RGBA (r ,g ,b )
64
+ elif len (tup )== 4 :
65
+ r ,g ,b ,a = tup
66
+ return RGBA (r ,g ,b ,a )
67
+ else :
68
+ raise ValueError
69
69
tuple_to_rgba .info = 'a RGB or RGBA tuple of floats'
70
70
71
71
72
72
def hex_to_rgba (ob , name , val ):
73
- rgx = re .compile ('^#[0-9A-Fa-f]{6}$' )
74
-
75
- if not is_string_like (val ):
76
- raise TypeError
77
- if rgx .match (val ) is None :
78
- raise ValueError
79
- r ,g ,b = hex2color (val )
80
- return RGBA (r ,g ,b ,1.0 )
73
+ rgx = re .compile ('^#[0-9A-Fa-f]{6}$' )
74
+
75
+ if not is_string_like (val ):
76
+ raise TypeError
77
+ if rgx .match (val ) is None :
78
+ raise ValueError
79
+ r ,g ,b = hex2color (val )
80
+ return RGBA (r ,g ,b ,1.0 )
81
81
hex_to_rgba .info = 'a hex color string'
82
82
83
83
84
84
def colorname_to_rgba (ob , name , val ):
85
- hex = colors [val .lower ()]
86
- r ,g ,b = hex2color (hex )
87
- return RGBA (r ,g ,b ,1.0 )
85
+ hex = colors [val .lower ()]
86
+ r ,g ,b = hex2color (hex )
87
+ return RGBA (r ,g ,b ,1.0 )
88
88
colorname_to_rgba .info = 'a named color'
89
89
90
90
91
91
def float_to_rgba (ob , name , val ):
92
- val = float (val )
93
- return RGBA (val , val , val , 1. )
92
+ val = float (val )
93
+ return RGBA (val , val , val , 1. )
94
94
float_to_rgba .info = 'a grayscale intensity'
95
95
96
96
@@ -99,12 +99,12 @@ def float_to_rgba(ob, name, val):
99
99
100
100
101
101
def file_exists (ob , name , val ):
102
- fh = file (val , 'r' )
103
- return val
102
+ fh = file (val , 'r' )
103
+ return val
104
104
105
105
106
106
def path_exists (ob , name , val ):
107
- os .path .exists (val )
107
+ os .path .exists (val )
108
108
linestyles = ('-' , '--' , '-.' , ':' , 'steps' , 'None' )
109
109
TICKLEFT , TICKRIGHT , TICKUP , TICKDOWN = range (4 )
110
110
linemarkers = (None , '.' , ',' , 'o' , '^' , 'v' , '<' , '>' , 's' ,
@@ -119,23 +119,23 @@ def path_exists(ob, name, val):
119
119
120
120
121
121
class LineRC (traits .HasTraits ):
122
- linewidth = traits .Float (0.5 )
123
- linestyle = traits .Trait (* linestyles )
124
- color = Color
125
- marker = traits .Trait (* linemarkers )
126
- markerfacecolor = Color
127
- markeredgecolor = Color
128
- markeredgewidth = traits .Float (0.5 )
129
- markersize = traits .Float (6 )
130
- antialiased = flexible_true_trait
131
- data_clipping = flexible_false_trait
122
+ linewidth = traits .Float (0.5 )
123
+ linestyle = traits .Trait (* linestyles )
124
+ color = Color
125
+ marker = traits .Trait (* linemarkers )
126
+ markerfacecolor = Color
127
+ markeredgecolor = Color
128
+ markeredgewidth = traits .Float (0.5 )
129
+ markersize = traits .Float (6 )
130
+ antialiased = flexible_true_trait
131
+ data_clipping = flexible_false_trait
132
132
133
133
134
134
class PatchRC (traits .HasTraits ):
135
- linewidth = traits .Float (1.0 )
136
- facecolor = Color
137
- edgecolor = Color
138
- antialiased = flexible_true_trait
135
+ linewidth = traits .Float (1.0 )
136
+ facecolor = Color
137
+ edgecolor = Color
138
+ antialiased = flexible_true_trait
139
139
140
140
timezones = 'UTC' , 'US/Central' , 'ES/Eastern' # fixme: and many more
141
141
backends = ('GTKAgg' , 'Cairo' , 'GDK' , 'GTK' , 'Agg' ,
@@ -144,51 +144,51 @@ class PatchRC(traits.HasTraits):
144
144
145
145
146
146
class RC (traits .HasTraits ):
147
- backend = traits .Trait (* backends )
148
- interactive = flexible_false_trait
149
- toolbar = traits .Trait ('toolbar2' , 'classic' , None )
150
- timezone = traits .Trait (* timezones )
151
- lines = traits .Trait (LineRC ())
152
- patch = traits .Trait (PatchRC ())
147
+ backend = traits .Trait (* backends )
148
+ interactive = flexible_false_trait
149
+ toolbar = traits .Trait ('toolbar2' , 'classic' , None )
150
+ timezone = traits .Trait (* timezones )
151
+ lines = traits .Trait (LineRC ())
152
+ patch = traits .Trait (PatchRC ())
153
153
154
154
rc = RC ()
155
155
rc .lines .color = 'r'
156
156
if doprint :
157
- print ('RC' )
158
- rc .print_traits ()
159
- print ('RC lines' )
160
- rc .lines .print_traits ()
161
- print ('RC patches' )
162
- rc .patch .print_traits ()
157
+ print ('RC' )
158
+ rc .print_traits ()
159
+ print ('RC lines' )
160
+ rc .lines .print_traits ()
161
+ print ('RC patches' )
162
+ rc .patch .print_traits ()
163
163
164
164
165
165
class Patch (Artist , traits .HasTraits ):
166
- linewidth = traits .Float (0.5 )
167
- facecolor = Color
168
- fc = facecolor
169
- edgecolor = Color
170
- fill = flexible_true_trait
171
-
172
- def __init__ (self ,
173
- edgecolor = None ,
174
- facecolor = None ,
175
- linewidth = None ,
176
- antialiased = None ,
177
- fill = 1 ,
178
- ** kwargs
179
- ):
180
- Artist .__init__ (self )
181
-
182
- if edgecolor is None : edgecolor = rc .patch .edgecolor
183
- if facecolor is None : facecolor = rc .patch .facecolor
184
- if linewidth is None : linewidth = rc .patch .linewidth
185
- if antialiased is None : antialiased = rc .patch .antialiased
186
-
187
- self .edgecolor = edgecolor
188
- self .facecolor = facecolor
189
- self .linewidth = linewidth
190
- self .antialiased = antialiased
191
- self .fill = fill
166
+ linewidth = traits .Float (0.5 )
167
+ facecolor = Color
168
+ fc = facecolor
169
+ edgecolor = Color
170
+ fill = flexible_true_trait
171
+
172
+ def __init__ (self ,
173
+ edgecolor = None ,
174
+ facecolor = None ,
175
+ linewidth = None ,
176
+ antialiased = None ,
177
+ fill = 1 ,
178
+ ** kwargs
179
+ ):
180
+ Artist .__init__ (self )
181
+
182
+ if edgecolor is None : edgecolor = rc .patch .edgecolor
183
+ if facecolor is None : facecolor = rc .patch .facecolor
184
+ if linewidth is None : linewidth = rc .patch .linewidth
185
+ if antialiased is None : antialiased = rc .patch .antialiased
186
+
187
+ self .edgecolor = edgecolor
188
+ self .facecolor = facecolor
189
+ self .linewidth = linewidth
190
+ self .antialiased = antialiased
191
+ self .fill = fill
192
192
193
193
194
194
p = Patch ()
@@ -202,6 +202,6 @@ def __init__(self,
202
202
if p .fill_ : print ('fill' )
203
203
else : print ('no fill' )
204
204
if doprint :
205
- print ()
206
- print ('Patch' )
207
- p .print_traits ()
205
+ print ()
206
+ print ('Patch' )
207
+ p .print_traits ()
0 commit comments