8000 Merge pull request #2017 from drevicko/fix-form-colors · matplotlib/matplotlib@4a6b612 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a6b612

Browse files
committed
Merge pull request #2017 from drevicko/fix-form-colors
qt4_editor formlayout now works with colour tuples (fixes Issue #1690)
2 parents 2cc7e21 + 0ecc702 commit 4a6b612

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

lib/matplotlib/backends/qt4_editor/formlayout.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

129142
class 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

Comments
 (0)
0