5151import datetime
5252import warnings
5353
54- from matplotlib . colors import colorConverter , is_color_like , rgb2hex
54+ from matplotlib import colors as mcolors
5555from matplotlib .backends .qt_compat import QtGui , QtWidgets , QtCore
5656from 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-
104100def 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
117112class 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