diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index ae60b108f541..9c02211007ee 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -385,8 +385,8 @@ def validate_color(s): # get rid of grouping symbols stmp = ''.join([c for c in s if c.isdigit() or c == '.' or c == ',']) vals = stmp.split(',') - if len(vals) != 3: - msg = '\nColor tuples must be length 3' + if len(vals) not in [3, 4]: + msg = '\nColor tuples must be of length 3 or 4' else: try: colorarg = [float(val) for val in vals] diff --git a/lib/matplotlib/tests/test_rcparams.py b/lib/matplotlib/tests/test_rcparams.py index e84a90ee34cf..06650de2b37b 100644 --- a/lib/matplotlib/tests/test_rcparams.py +++ b/lib/matplotlib/tests/test_rcparams.py @@ -23,6 +23,7 @@ from matplotlib.rcsetup import (validate_bool_maybe_none, validate_stringlist, validate_colorlist, + validate_color, validate_bool, validate_nseq_int, validate_nseq_float, @@ -324,6 +325,27 @@ def generate_validator_testcases(valid): 'fail': (('fish', ValueError), ), }, + {'validator': validate_color, + 'success': (('None', 'none'), + ('none', 'none'), + ('AABBCC', '#AABBCC'), # RGB hex code + ('AABBCC00', '#AABBCC00'), # RGBA hex code + ('tab:blue', 'tab:blue'), # named color + ('C0', 'C0'), # color from cycle + ('(0, 1, 0)', [0.0, 1.0, 0.0]), # RGB tuple + ((0, 1, 0), (0, 1, 0)), # non-string version + ('(0, 1, 0, 1)', [0.0, 1.0, 0.0, 1.0]), # RGBA tuple + ((0, 1, 0, 1), (0, 1, 0, 1)), # non-string version + ('(0, 1, "0.5")', [0.0, 1.0, 0.5]), # unusual but valid + + ), + 'fail': (('tab:veryblue', ValueError), # invalid name + ('C123', ValueError), # invalid RGB(A) code and cycle index + ('(0, 1)', ValueError), # tuple with length < 3 + ('(0, 1, 0, 1, 0)', ValueError), # tuple with length > 4 + ('(0, 1, none)', ValueError), # cannot cast none to float + ), + }, {'validator': validate_hist_bins, 'success': (('auto', 'auto'), ('10', 10),