48
48
import sys
49
49
STDERR = sys .stderr
50
50
51
- from matplotlib .backends .qt4_compat import QtGui ,QtCore
52
- if not hasattr (QtGui ,'QFormLayout' ):
53
- raise ImportError , "Warning: formlayout requires PyQt4 >v4.3 or PySide"
51
+ from matplotlib .colors import is_color_like
52
+ from matplotlib .colors import rgb2hex
53
+ from matplotlib .colors import colorConverter
54
+
55
+ from matplotlib .backends .qt4_compat import QtGui , QtCore
54
56
55
57
(QWidget , QLineEdit , QComboBox , QLabel , QSpinBox , QIcon ,QStyle ,
56
58
QDialogButtonBox , QHBoxLayout , QVBoxLayout , QDialog , QColor , QPushButton ,
68
70
(Qt , SIGNAL , SLOT , QObject , QSize ,pyqtSignature , pyqtProperty ) = \
69
71
(QtCore .Qt , QtCore .SIGNAL , QtCore .SLOT , QtCore .QObject , QtCore .QSize ,
70
72
QtCore .Slot , QtCore .Property )
73
+ if not hasattr (QtGui , 'QFormLayout' ):
74
+ raise ImportError ("Warning: formlayout requires PyQt4 >v4.3 or PySide" )
71
75
72
76
import datetime
73
77
@@ -103,41 +107,24 @@ def set_color(self, color):
103
107
104
108
color = pyqtProperty ("QColor" , get_color , set_color )
105
109
110
+ def col2hex (color ):
111
+ """Convert matplotlib color to hex before passing to Qt"""
112
+ return rgb2hex (colorConverter .to_rgb (color ))
106
113
107
- def text_to_qcolor (text ):
108
- """
109
- Create a QColor from specified string
110
- Avoid warning from Qt when an invalid QColor is instantiated
111
- """
112
- color = QColor ()
114
+ def to_qcolor (color ):
115
+ """Create a QColor from a matplotlib color"""
116
+ qcolor = QColor ()
113
117
if isinstance (text , QObject ):
114
118
# actually a QString, which is not provided by the new PyQt4 API:
115
119
text = str (text )
116
- if not isinstance (text , (unicode , str )):
117
- return color
118
- if text .startswith ('#' ) and len (text )== 7 :
119
- correct = '#0123456789abcdef'
120
- for char in text :
121
- if char .lower () not in correct :
122
- return color
123
- elif text not in list (QColor .colorNames ()):
124
- return color
125
- color .setNamedColor (text )
126
- return color
127
-
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
120
+ try :
121
+ color = col2hex (color )
122
+ except ValueError :
123
+ #print('WARNING: ignoring invalid color %r' % color)
124
+ return qcolor # return invalid QColor
125
+ qcolor .setNamedColor (color ) # set using hex color
126
+ return qcolor # return valid QColor
127
+
141
128
142
129
class ColorLayout (QHBoxLayout ):
143
130
"""Color-specialized QLineEdit layout"""
@@ -154,10 +141,10 @@ def __init__(self, color, parent=None):
154
141
self .update_text )
155
142
self .addWidget (self .colorbtn )
156
143
157
- def update_color (self , text ):
158
- color = text_to_qcolor ( text )
144
+ def update_color (self , color ):
145
+ qcolor = to_qcolor ( color )
159
146
if color .isValid ():
160
- self .colorbtn .color = color
147
+ self .colorbtn .color = qcolor
161
148
162
149
def update_text (self , color ):
163
150
self .lineedit .setText (color .name ())
@@ -281,8 +268,8 @@ def setup(self):
281
268
continue
282
269
elif tuple_to_qfont (value ) is not None :
283
270
field = FontLayout (value , self )
284
- elif is_matplotlib_color (value ):
285
- field = ColorLayout (QColor (value ), self )
271
+ elif is_color_like (value ):
272
+ field = ColorLayout (to_qcolor (value ), self )
286
273
elif isinstance (value , (str , unicode )):
287
274
field = QLineEdit (value , self )
288
275
elif isinstance (value , (list , tuple )):
@@ -342,7 +329,7 @@ def get(self):
342
329
continue
343
330
elif tuple_to_qfont (value ) is not None :
344
331
value = field .get_font ()
345
- elif isinstance (value , (str , unicode )) or is_matplotlib_color (value ):
332
+ elif isinstance (value , (str , unicode )) or is_color_like (value ):
346
333
value = unicode (field .text ())
347
334
elif isinstance (value , (list , tuple )):
348
335
index = int (field .currentIndex ())
0 commit comments