8000 SC 24/02/2005 · matplotlib/matplotlib@8761974 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8761974

Browse files
author
Steve Chaplin
committed
SC 24/02/2005
svn path=/trunk/matplotlib/; revision=984
1 parent 60ef597 commit 8761974

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
New entries should be added at the top
22

3+
2005-02-24 colors.py change ColorConverter.to_rgb() so it always returns rgb
4+
(and not rgba), allow cnames keys to be cached, change the exception
5+
raised from RuntimeError to ValueError (like hex2color())
6+
hex2color() use a regular expression to check the color string is
7+
valid - SC
8+
39
2005-02-23 Added rc param ps.useafm so backend ps can use native afm
410
fonts or truetype. afme breaks mathtext but causes much
511
smaller font sizes and may result in images that display

lib/matplotlib/colors.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
Finally, legal html names for colors, like 'red', 'burlywood' and
2929
'chartreuse' are supported.
3030
"""
31+
import re
3132

3233
from numerix import array, arange, take, put, Float, Int, where, \
3334
zeros, asarray, sort, searchsorted, sometrue, ravel, divide
@@ -336,10 +337,14 @@ def rgb2hex(rgb):
336337
'Given a len 3 rgb tuple of 0-1 floats, return the hex string'
337338
return '#%02x%02x%02x' % tuple([round(val*255) for val in rgb])
338339

340+
hexColorPattern = re.compile("\A#[a-fA-F0-9]{6}\Z")
341+
339342
def hex2color(s):
340-
"Convert hex string (like html uses, eg, #efefef) to a r,g,b tuple"
341-
if s[0]!='#' or len(s)!=7:
342-
raise ValueError('s must be a hex string like "#efefef"')
343+
"Convert hex string 's' (like html uses, eg, #efefef) to a r,g,b tuple"
344+
if not isinstance(s, str):
345+
raise TypeError('hex2color requires a string argument')
346+
if not hexColorPattern.match(s):
347+
raise ValueError('invalid hex color string "%s"' % s)
343348
return tuple([int(n, 16)/255.0 for n in (s[1:3], s[3:5], s[5:7])])
344349

345350
class ColorConverter:
@@ -368,28 +373,29 @@ def to_rgb(self, arg):
368373
try: self.cache[arg]
369374
except KeyError: pass
370375

371-
color = None
372-
try: float(arg)
373-
except:
376+
try:
374377
if is_string_like(arg):
375-
hex = cnames.get(arg)
376-
if hex is not None: arg = hex
377-
if len(arg)==7 and arg[0]=='#':
378-
color = hex2color(arg)
379-
if color is None:
380-
# see if it looks like rgb. If so, just return arg
381-
try: float(arg[2])
382-
except: color = self.colors.get(arg, (0.0, 0.0, 0.0))
383-
else: color = tuple(arg)
384-
else:
385-
if arg>=0 and arg<=1:
386-
color = (arg,arg,arg)
387-
else:
388-
msg = 'Floating point color arg must be between 0 and 1\n' +\
389-
'Found %1.2f' % arg
390-
raise RuntimeError(msg)
391-
392-
self.cache[arg] = color
378+
str1 = cnames.get(arg, arg)
379+
if str1.startswith('#'):
380+
color = hex2color(str1)
381+
else:
382+
color = self.colors[arg]
383+
elif isinstance(arg, float):
384+
if 0<=arg<=1:
385+
color = (arg,arg,arg)
386+
else:
387+
raise ValueError('Floating point color arg must be between 0 and 1')
388+
else: # assume tuple (or list)
389+
assert isinstance(arg[2], (float,int))
390+
color = tuple(arg[:3])
391+
392+
self.cache[arg] = color # raise exception if color not set
393+
394+
except KeyError:
395+
raise ValueError('to_rgb: Invalid rgb arg "%s"' % (str(arg)))
396+
except Exception, exc:
397+
raise ValueError('to_rgb: Invalid rgb arg "%s"\n%s' % (str(arg), exc))
398+
393399
return color
394400

395401
def to_rgba(self, arg, alpha=1.0):

0 commit comments

Comments
 (0)
0