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

Skip to content

Commit e049a49

Browse files
committed
MNT: unify string handling
- convert the single-letter colors to hex in source - single path for looking up string -> hex -> rgb values
1 parent e392d03 commit e049a49

1 file changed

Lines changed: 21 additions & 22 deletions

File tree

lib/matplotlib/colors.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,17 @@ class ColorConverter(object):
105105
*colorConverter*, is needed.
106106
"""
107107
colors = {
108-
'b': (0.0, 0.0, 1.0),
109-
'g': (0.0, 0.5, 0.0),
110-
'r': (1.0, 0.0, 0.0),
111-
'c': (0.0, 0.75, 0.75),
112-
'm': (0.75, 0, 0.75),
113-
'y': (0.75, 0.75, 0),
114-
'k': (0.0, 0.0, 0.0),
115-
'w': (1.0, 1.0, 1.0), }
108+
'b': '#0000ff',
109+
'r': '#ff0000',
110+
'g': '#008000',
111+
'c': '#00bfbf',
112+
'm': '#bf00bf',
113+
'y': '#bfbf00',
114+
'k': '#000000',
115+
'w': '#ffffff'}
116116

117117
cache = {}
118+
CN_LOOKUPS = [colors, ] + [COLOR_NAMES[k] for k in ['xkcd', 'css4']]
118119

119120
def to_rgb(self, arg):
120121
"""
@@ -151,20 +152,18 @@ def to_rgb(self, arg):
151152
try:
152153
if cbook.is_string_like(arg):
153154
argl = arg.lower()
154-
color = self.colors.get(argl, None)
155-
if color is None:
156-
for k in ['xkcd', 'x11']:
157-
str1 = COLOR_NAMES[k].get(argl, argl)
158-
if str1 != argl:
159-
break
160-
if str1.startswith('#'):
161-
color = hex2color(str1)
162-
else:
163-
fl = float(argl)
164-
if fl < 0 or fl > 1:
165-
raise ValueError(
166-
'gray (string) must be in range 0-1')
167-
color = (fl,)*3
155+
for cmapping in self.CN_LOOKUPS:
156+
str1 = cmapping.get(argl, argl)
157+
if str1 != argl:
158+
break
159+
if str1.startswith('#'):
160+
color = hex2color(str1)
161+
else:
162+
fl = float(argl)
163+
if fl < 0 or fl > 1:
164+
raise ValueError(
165+
'gray (string) must be in range 0-1')
166+
color = (fl,)*3
168167
elif cbook.iterable(arg):
169168
if len(arg) > 4 or len(arg) < 3:
170169
raise ValueError(

0 commit comments

Comments
 (0)