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