8000 Merge pull request #9038 from anntzer/len-4-color-rcparams · matplotlib/matplotlib@c9c6f6b · GitHub
[go: up one dir, main page]

Skip to content

Commit c9c6f6b

Browse files
authored
Merge pull request #9038 from anntzer/len-4-color-rcparams
Allow tuples of 4 floats as color rcparams.
2 parents c0bceb1 + 1f0378e commit c9c6f6b

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/matplotlib/rcsetup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,8 @@ def validate_color(s):
385385
# get rid of grouping symbols
386386
stmp = ''.join([c for c in s if c.isdigit() or c == '.' or c == ','])
387387
vals = stmp.split(',')
388-
if len(vals) != 3:
389-
msg = '\nColor tuples must be length 3'
388+
if len(vals) not in [3, 4]:
389+
msg = '\nColor tuples must be of length 3 or 4'
390390
else:
391391
try:
392392
colorarg = [float(val) for val in vals]

lib/matplotlib/tests/test_rcparams.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from matplotlib.rcsetup import (validate_bool_maybe_none,
2424
validate_stringlist,
2525
validate_colorlist,
26+
validate_color,
2627
validate_bool,
2728
validate_nseq_int,
2829
validate_nseq_float,
@@ -324,6 +325,27 @@ def generate_validator_testcases(valid):
324325
'fail': (('fish', ValueError),
325326
),
326327
},
328+
{'validator': validate_color,
329+
'success': (('None', 'none'),
330+
('none', 'none'),
331+
('AABBCC', '#AABBCC'), # RGB hex code
332+
('AABBCC00', '#AABBCC00'), # RGBA hex code
333+
('tab:blue', 'tab:blue'), # named color
334+
('C0', 'C0'), # color from cycle
335+
('(0, 1, 0)', [0.0, 1.0, 0.0]), # RGB tuple
336+
((0, 1, 0), (0, 1, 0)), # non-string version
337+
('(0, 1, 0, 1)', [0.0, 1.0, 0.0, 1.0]), # RGBA tuple
338+
((0, 1, 0, 1), (0, 1, 0, 1)), # non-string version
339+
('(0, 1, "0.5")', [0.0, 1.0, 0.5]), # unusual but valid
340+
341+
),
342+
'fail': (('tab:veryblue', ValueError), # invalid name
343+
('C123', ValueError), # invalid RGB(A) code and cycle index
344+
('(0, 1)', ValueError), # tuple with length < 3
345+
('(0, 1, 0, 1, 0)', ValueError), # tuple with length > 4
346+
('(0, 1, none)', ValueError), # cannot cast none to float
347+
),
348+
},
327349
{'validator': validate_hist_bins,
328350
'success': (('auto', 'auto'),
329351
('10', 10),

0 commit comments

Comments
 (0)
0