8000 Qt editor alpha handling. · matplotlib/matplotlib@87ab70a · GitHub
[go: up one dir, main page]

Skip to content

Commit 87ab70a

Browse files
committed
Qt editor alpha handling.
1 parent 0cbcf69 commit 87ab70a

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',
@@ -114,23 +114,25 @@ def prepare_data(d, init):
114114
curvelabels = sorted(linedict, key=cmp_key)
115115
for label in curvelabels:
116116
line = linedict[label]
117-
color = rgb2hex(colorConverter.to_rgb(line.get_color()))
118-
ec = rgb2hex(colorConverter.to_rgb(line.get_markeredgecolor()))
119-
fc = rgb2hex(colorConverter.to_rgb(line.get_markerfacecolor()))
117+
color = mcolors.to_hex(
118+
mcolors.to_rgba(line.get_color(), line.get_alpha()),
119+
keep_alpha=True)
120+
ec = mcolors.to_hex(line.get_markeredgecolor(), keep_alpha=True)
121+
fc = mcolors.to_hex(line.get_markerfacecolor(), keep_alpha=True)
120122
curvedata = [
121123
('Label', label),
122124
sep,
123125
(None, '<b>Line</b>'),
124-
('Line Style', prepare_data(LINESTYLES, line.get_linestyle())),
125-
('Draw Style', prepare_data(DRAWSTYLES, line.get_drawstyle())),
126+
('Line style', prepare_data(LINESTYLES, line.get_linestyle())),
127+
('Draw style', prepare_data(DRAWSTYLES, line.get_drawstyle())),
126128
('Width', line.get_linewidth()),
127-
('Color', color),
129+
('Color (RGBA)', color),
128130
sep,
129131
(None, '<b>Marker</b>'),
130132
('Style', prepare_data(MARKERS, line.get_marker())),
131133
('Size', line.get_markersize()),
132-
('Facecolor', fc),
133-
('Edgecolor', ec)]
134+
('Face color (RGBA)', fc),
135+
('Edge color (RGBA)', ec)]
134136
curves.append([curvedata, label, ""])
135137
# Is there a curve displayed?
136138
has_curve = bool(curves)
@@ -206,7 +208,9 @@ def apply_callback(data):
206208
line.set_linestyle(linestyle)
207209
line.set_drawstyle(drawstyle)
208210
line.set_linewidth(linewidth)
209-
line.set_color(color)
211+
rgba = mcolors.to_rgba(color)
212+
line.set_color(rgba[:3])
213+
line.set_alpha(rgba[-1])
210214
if marker is not 'none':
211215
line.set_marker(marker)
212216
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)
0