Skip to content

Commit 599ce42

Browse files
committed
Qt editor alpha handling.
1 parent da9fdd8 commit 599ce42

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

lib/matplotlib/backends/qt_editor/figureoptions.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@
1515
import os.path as osp
1616
import re
1717

18+
import matplotlib
19+
from matplotlib import cm, markers, colors as mcolors
1820
import matplotlib.backends.qt_editor.formlayout as formlayout
1921
from matplotlib.backends.qt_compat import QtGui
20-
from matplotlib import cm, markers
21-
from matplotlib.colors import colorConverter, rgb2hex
2222

2323

2424
def get_icon(name):
25-
import matplotlib
2625
basedir = osp.join(matplotlib.rcParams['datapath'], 'images')
2726
return QtGui.QIcon(osp.join(basedir, name))
2827

28+
2929
LINESTYLES = {'-': 'Solid',
3030
'--': 'Dashed',
3131
'-.': 'DashDot',
@@ -112,17 +112,19 @@ def prepare_data(d, init):
112112
curvelabels = sorted(linedict, key=cmp_key)
113113
for label in curvelabels:
114114
line = linedict[label]
115-
color = rgb2hex(colorConverter.to_rgb(line.get_color()))
116-
ec = rgb2hex(colorConverter.to_rgb(line.get_markeredgecolor()))
117-
fc = rgb2hex(colorConverter.to_rgb(line.get_markerfacecolor()))
115+
color = mcolors.to_hex(
116+
mcolors.to_rgba(line.get_color(), line.get_alpha()),
117+
keep_alpha=True)
118+
ec = mcolors.to_hex(line.get_markeredgecolor(), keep_alpha=True)
119+
fc = mcolors.to_hex(line.get_markerfacecolor(), keep_alpha=True)
118120
curvedata = [
119121
('Label', label),
120122
sep,
121123
(None, '<b>Line</b>'),
122124
('Line Style', prepare_data(LINESTYLES, line.get_linestyle())),
123125
('Draw Style', prepare_data(DRAWSTYLES, line.get_drawstyle())),
124126
('Width', line.get_linewidth()),
125-
('Color', color),
127+
('Color (RGBA)', color),
126128
sep,
127129
(None, '<b>Marker</b>'),
128130
('Style', prepare_data(MARKERS, line.get_marker())),
@@ -200,7 +202,8 @@ def apply_callback(data):
200202
line.set_linestyle(linestyle)
201203
line.set_drawstyle(drawstyle)
202204
line.set_linewidth(linewidth)
203-
line.set_color(color)
205+
rgba = mcolors.to_rgba(color)
206+
line.set_color(rgba)
204207
if marker is not 'none':
205208
line.set_marker(marker)
206209
line.set_markersize(markersize)

lib/matplotlib/backends/qt_editor/formlayout.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
import datetime
5252
import warnings
5353

54-
from matplotlib.colors import colorConverter, is_color_like, rgb2hex
54+
from matplotlib import colors as mcolors
5555
from matplotlib.backends.qt_compat import QtGui, QtWidgets, QtCore
5656
from matplotlib.externals import six
5757

@@ -77,7 +77,8 @@ def __init__(self, parent=None):
7777

7878
def choose_color(self):
7979
color = QtWidgets.QColorDialog.getColor(
80-
self._color, self.parentWidget(), '')
80+
self._color, self.parentWidget(), "",
81+
QtWidgets.QColorDialog.ShowAlphaChannel)
8182
if color.isValid():
8283
self.set_color(color)
8384

@@ -96,30 +97,25 @@ def set_color(self, color):
9697
color = QtCore.Property(QtGui.QColor, get_color, set_color)
9798

9899

99-
def col2hex(color):
100-
"""Convert matplotlib color to hex before passing to Qt"""
101-
return rgb2hex(colorConverter.to_rgb(color))
102-
103-
104100
def to_qcolor(color):
105101
"""Create a QColor from a matplotlib color"""
106102
qcolor = QtGui.QColor()
107-
color = str(color)
108103
try:
109-
color = col2hex(color)
104+
rgba = mcolors.to_rgba(color)
110105
except ValueError:
111106
warnings.warn('Ignoring invalid color %r' % color)
112107
return qcolor # return invalid QColor
113-
qcolor.setNamedColor(color) # set using hex color
114-
return qcolor # return valid QColor
108+
qcolor.setRgbF(*rgba)
109+
return qcolor
115110

116111

117112
class ColorLayout(QtWidgets.QHBoxLayout):
118113
"""Color-specialized QLineEdit layout"""
119114
def __init__(self, color, parent=None):
120115
QtWidgets.QHBoxLayout.__init__(self)
121116
assert isinstance(color, QtGui.QColor)
122-
self.lineedit = QtWidgets.QLineEdit(color.name(), parent)
117+
self.lineedit = QtWidgets.QLineEdit(
118+
mcolors.to_hex(color.getRgbF(), keep_alpha=True), parent)
123119
self.lineedit.editingFinished.connect(self.update_color)
124120
self.addWidget(self.lineedit)
125121
self.colorbtn = ColorButton(parent)
@@ -133,7 +129,7 @@ def update_color(self):
133129
self.colorbtn.color = qcolor # defaults to black if not qcolor.isValid()
134130

135131
def update_text(self, color):
136-
self.lineedit.setText(color.name())
132+
self.lineedit.setText(mcolors.to_hex(color.getRgbF(), keep_alpha=True))
137133

138134
def text(self):
139135
return self.lineedit.text()
@@ -259,7 +255,8 @@ def setup(self):
259255
continue
260256
elif tuple_to_qfont(value) is not None:
261257
field = FontLayout(value, self)
262-
elif label.lower() not in BLACKLIST and is_color_like(value):
258+
elif (label.lower() not in BLACKLIST
259+
and mcolors.is_color_like(value)):
263260
field = ColorLayout(to_qcolor(value), self)
264261
elif isinstance(value, six.string_types):
265262
field = QtWidgets.QLineEdit(value, self)
@@ -322,7 +319,8 @@ def get(self):
322319
continue
323320
elif tuple_to_qfont(value) is not None:
324321
value = field.get_font()
325-
elif isinstance(value, six.string_types) or is_color_like(value):
322+
elif (isinstance(value, six.string_types)
323+
or mcolors.is_color_like(value)):
326324
value = six.text_type(field.text())
327325
elif isinstance(value, (list, tuple)):
328326
index = int(field.currentIndex())

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy