@@ -125,6 +125,19 @@ def text_to_qcolor(text):
125125 color .setNamedColor (text )
126126 return color
127127
128+ def is_matplotlib_color (value ):
129+ """
130+ Check if value is a color passed to us from matplotlib.
131+ It could either be a valid color string or a 3-tuple of floats between 0. and 1.
132+ """
133+ if text_to_qcolor (value ).isValid ():
134+ return True
135+ if isinstance (value ,tuple ) and len (value )== 3 and all (map (lambda v : isinstance (v ,float ),value )):
136+ for c in value :
137+ if c < 0. or c > 1. :
138+ return False
139+ return True
140+ return False
128141
129142class ColorLayout (QHBoxLayout ):
130143 """Color-specialized QLineEdit layout"""
@@ -268,7 +281,7 @@ def setup(self):
268281 continue
269282 elif tuple_to_qfont (value ) is not None :
270283 field = FontLayout (value , self )
271- elif text_to_qcolor (value ). isValid ( ):
284+ elif is_matplotlib_color (value ):
272285 field = ColorLayout (QColor (value ), self )
273286 elif isinstance (value , (str , unicode )):
274287 field = QLineEdit (value , self )
@@ -329,7 +342,7 @@ def get(self):
329342 continue
330343 elif tuple_to_qfont (value ) is not None :
331344 value = field .get_font ()
332- elif isinstance (value , (str , unicode )):
345+ elif isinstance (value , (str , unicode )) or is_matplotlib_color ( value ) :
333346 value = unicode (field .text ())
334347 elif isinstance (value , (list , tuple )):
335348 index = int (field .currentIndex ())
0 commit comments