Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c077c56

Browse files
committed
Qt editor alpha handling.
1 parent 46dd689 commit c077c56

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

lib/matplotlib/backends/qt_editor/figureoptions.py

Lines changed: 16 additions & 12 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,23 +112,25 @@ 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>'),
122-
('Line Style', prepare_data(LINESTYLES, line.get_linestyle())),
123-
('Draw Style', prepare_data(DRAWSTYLES, line.get_drawstyle())),
124+
('Line style', prepare_data(LINESTYLES, line.get_linestyle())),
125+
('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())),
129131
('Size', line.get_markersize()),
130-
('Facecolor', fc),
131-
('Edgecolor', ec)]
132+
('Face color (RGBA)', fc),
133+
('Edge color (RGBA)', ec)]
132134
curves.append([curvedata, label, ""])
133135
# Is there a curve displayed?
134136
has_curve = bool(curves)
@@ -204,7 +206,9 @@ def apply_callback(data):
204206
line.set_linestyle(linestyle)
205207
line.set_drawstyle(drawstyle)
206208
line.set_linewidth(linewidth)
207-
line.set_color(color)
209+
rgba = mcolors.to_rgba(color)
210+
line.set_color(rgba[:3])
211+
line.set_alpha(rgba[-1])
208212
if marker is not 'none':
209213
line.set_marker(marker)
210214
line.set_markersize(markersize)

lib/matplotlib/backends/qt_editor/formlayout.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@
4646

4747
DEBUG = False
4848

49-
import six
50-
5149
import copy
5250
import datetime
5351
import warnings
5452

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

5858

@@ -74,7 +74,8 @@ def __init__(self, parent=None):
7474

7575
def choose_color(self):
7676
color = QtWidgets.QColorDialog.getColor(
77-
self._color, self.parentWidget(), '')
77+
self._color, self.parentWidget(), "",
78+
QtWidgets.QColorDialog.ShowAlphaChannel)
7879
if color.isValid():
7980
self.set_color(color)
8081

@@ -93,30 +94,25 @@ def set_color(self, color):
9394
color = QtCore.Property(QtGui.QColor, get_color, set_color)
9495

9596

96-
def col2hex(color):
97-
"""Convert matplotlib color to hex before passing to Qt"""
98-
return rgb2hex(colorConverter.to_rgb(color))
99-
100-
10197
def to_qcolor(color):
10298
"""Create a QColor from a matplotlib color"""
10399
qcolor = QtGui.QColor()
104-
color = str(color)
105100
try:
106-
color = col2hex(color)
101+
rgba = mcolors.to_rgba(color)
107102
except ValueError:
108103
warnings.warn('Ignoring invalid color %r' % color)
109104
return qcolor # return invalid QColor
110-
qcolor.setNamedColor(color) # set using hex color
111-
return qcolor # return valid QColor
105+
qcolor.setRgbF(*rgba)
106+
return qcolor
112107

113108

114109
class ColorLayout(QtWidgets.QHBoxLayout):
115110
"""Color-specialized QLineEdit layout"""
116111
def __init__(self, color, parent=None):
117112
QtWidgets.QHBoxLayout.__init__(self)
118113
assert isinstance(color, QtGui.QColor)
119-
self.lineedit = QtWidgets.QLineEdit(color.name(), parent)
114+
self.lineedit = QtWidgets.QLineEdit(
115+
mcolors.to_hex(color.getRgbF(), keep_alpha=True), parent)
120116
self.lineedit.editingFinished.connect(self.update_color)
121117
self.addWidget(self.lineedit)
122118
self.colorbtn = ColorButton(parent)
@@ -130,7 +126,7 @@ def update_color(self):
130126
self.colorbtn.color = qcolor # defaults to black if not qcolor.isValid()
131127

132128
def update_text(self, color):
133-
self.lineedit.setText(color.name())
129+
self.lineedit.setText(mcolors.to_hex(color.getRgbF(), keep_alpha=True))
134130

135131
def text(self):
136132
return self.lineedit.text()
@@ -256,7 +252,8 @@ def setup(self):
256252
continue
257253
elif tuple_to_qfont(value) is not None:
258254
field = FontLayout(value, self)
259-
elif label.lower() not in BLACKLIST and is_color_like(value):
255+
elif (label.lower() not in BLACKLIST
256+
and mcolors.is_color_like(value)):
260257
field = ColorLayout(to_qcolor(value), self)
261258
elif isinstance(value, six.string_types):
262259
field = QtWidgets.QLineEdit(value, self)
@@ -319,7 +316,8 @@ def get(self):
319316
continue
320317
elif tuple_to_qfont(value) is not None:
321318
value = field.get_font()
322-
elif isinstance(value, six.string_types) or is_color_like(value):
319+
elif (isinstance(value, six.string_types)
320+
or mcolors.is_color_like(value)):
323321
value = six.text_type(field.text())
324322
elif isinstance(value, (list, tuple)):
325323
index = int(field.currentIndex())

0 commit comments

Comments
 (0)