51
51
import datetime
52
52
import warnings
53
53
54
- from matplotlib . colors import colorConverter , is_color_like , rgb2hex
54
+ from matplotlib import colors as mcolors
55
55
from matplotlib .backends .qt_compat import QtGui , QtWidgets , QtCore
56
56
from matplotlib .externals import six
57
57
@@ -77,7 +77,8 @@ def __init__(self, parent=None):
77
77
78
78
def choose_color (self ):
79
79
color = QtWidgets .QColorDialog .getColor (
80
- self ._color , self .parentWidget (), '' )
80
+ self ._color , self .parentWidget (), "" ,
81
+ QtWidgets .QColorDialog .ShowAlphaChannel )
81
82
if color .isValid ():
82
83
self .set_color (color )
83
84
@@ -96,30 +97,25 @@ def set_color(self, color):
96
97
color = QtCore .Property (QtGui .QColor , get_color , set_color )
97
98
98
99
99
- def col2hex (color ):
100
- """Convert matplotlib color to hex before passing to Qt"""
101
- return rgb2hex (colorConverter .to_rgb (color ))
102
-
103
-
104
100
def to_qcolor (color ):
105
101
"""Create a QColor from a matplotlib color"""
106
102
qcolor = QtGui .QColor ()
107
- color = str (color )
108
103
try :
109
- color = col2hex (color )
104
+ rgba = mcolors . to_rgba (color )
110
105
except ValueError :
111
106
warnings .warn ('Ignoring invalid color %r' % color )
112
107
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
115
110
116
111
117
112
class ColorLayout (QtWidgets .QHBoxLayout ):
118
113
"""Color-specialized QLineEdit layout"""
119
114
def __init__ (self , color , parent = None ):
120
115
QtWidgets .QHBoxLayout .__init__ (self )
121
116
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 )
123
119
self .lineedit .editingFinished .connect (self .update_color )
124
120
self .addWidget (self .lineedit )
125
121
self .colorbtn = ColorButton (parent )
@@ -133,7 +129,7 @@ def update_color(self):
133
129
self .colorbtn .color = qcolor # defaults to black if not qcolor.isValid()
134
130
135
131
def update_text (self , color ):
136
- self .lineedit .setText (color .name ( ))
132
+ self .lineedit .setText (mcolors . to_hex ( color .getRgbF (), keep_alpha = True ))
137
133
138
134
def text (self ):
139
135
return self .lineedit .text ()
@@ -259,7 +255,8 @@ def setup(self):
259
255
continue
260
256
elif tuple_to_qfont (value ) is not None :
261
257
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 )):
263
260
field = ColorLayout (to_qcolor (value ), self )
264
261
elif isinstance (value , six .string_types ):
265
262
field = QtWidgets .QLineEdit (value , self )
@@ -322,7 +319,8 @@ def get(self):
322
319
continue
323
320
elif tuple_to_qfont (value ) is not None :
324
321
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 )):
326
324
value = six .text_type (field .text ())
327
325
elif isinstance (value , (list , tuple )):
328
326
index = int (field .currentIndex ())
0 commit comments