New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Colours like 'XeYYYY' don't get recognised properly if X, Y's are numbers #6701
Conversation
…sed as floats in scientific notation. fixed for Y>1, needs some decision in the Y=0,Y=1 cases.
For the record, this bug arose with a stylesheet having this line
With the fifth colour being interpreted as
Notice the lack of |
This does not seem like the right fix for the bug you are reporting. The code that parses rcparams / stylesheets has no way of escaping The bug that should be fixed here is in the validation of rcparams. In This seems to be fixing a different bug, in that attn @anntzer |
Remember that I am fully aware that my patch doesn't fix the bug itself. One possible way could be to prioritise |
@@ -367,12 +367,19 @@ def validate_color(s): | |||
return 'None' | |||
except AttributeError: | |||
pass | |||
|
|||
if isinstance(s, six.string_types): | |||
if len(s) == 7 or len(s) == 9: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not need this block as it will correctly fall through to L380
Yes, that is what I was suggesting as the proper fix. The only place a string without a The commit you just pushed looks good. Can you please add a test? |
OK as it happens often I'm changing my mind. I committed another fix that does what I said above. The bug as per my first comment seems fixed. |
Uhm, have we always made it a requirement for hex colors to have the hash? On Thu, Jul 7, 2016 at 9:41 AM, myyc notifications@github.com wrote:
|
I've appended a test to |
There are two cyclers... there is the cycler from the cycler package, and then there is our cycler that performs validation. The unit tests here depend on the original cyclers. |
It's used only in |
Well, what are you trying to test? Both are perfectly legitimate inputs in On Thu, Jul 7, 2016 at 11:04 AM, myyc notifications@github.com wrote:
|
@@ -367,12 +367,16 @@ def validate_color(s): | |||
return 'None' | |||
except AttributeError: | |||
pass | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a comment reminding future readers why this needs to happen first?
I haven't fully followed the discussion about the need for a prefix
by
where |
You guys should probably decide what you think is best as there are mixed opinions :) |
Another reason to put the fix in the color converter (as I suggested) rather than in rcsetup is that this issue is not limited, well, to the rcsetup. |
I agree with @anntzer. |
I do not think we currently support hex codes without leading However, I think the original commit on this PR (which has been force-pushed away) which raised if the float values for grayscale were outside of [0, 1] should also go in. In [1]: import matplotlib
In [2]: matplotlib.__version__
Out[2]: '2.0.0b1.post117+gb2b37fb'
In [3]: import matplotlib.colors as mc
In [4]: mc.to_rgb
Out[4]: <function matplotlib.colors.to_rgb>
In [5]: mc.to_rgb('aaaaaa')
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/home/tcaswell/source/p/matplotlib/lib/matplotlib/colors.py in to_rgba(c, alpha)
132 try:
--> 133 rgba = _colors_full_map.cache[c, alpha]
134 except (KeyError, TypeError): # Not in cache, or unhashable.
KeyError: ('aaaaaa', None)
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-5-bbb3c951fab9> in <module>()
----> 1 mc.to_rgb('aaaaaa')
/home/tcaswell/source/p/matplotlib/lib/matplotlib/colors.py in to_rgb(c)
233 """Convert `c` to an RGB color, silently dropping the alpha channel.
234 """
--> 235 return to_rgba(c)[:3]
236
237
/home/tcaswell/source/p/matplotlib/lib/matplotlib/colors.py in to_rgba(c, alpha)
133 rgba = _colors_full_map.cache[c, alpha]
134 except (KeyError, TypeError): # Not in cache, or unhashable.
--> 135 rgba = _to_rgba_no_colorcycle(c, alpha)
136 try:
137 _colors_full_map.cache[c, alpha] = rgba
/home/tcaswell/source/p/matplotlib/lib/matplotlib/colors.py in _to_rgba_no_colorcycle(c, alpha)
176 except ValueError:
177 pass
--> 178 raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
179 # tuple color.
180 # Python 2.7 / numpy 1.6 apparently require this to return builtin floats,
ValueError: Invalid RGBA argument: 'aaaaaa'
In [6]: mc.to_rgb('#aaaaaa')
Out[6]: (0.6666666666666666, 0.6666666666666666, 0.6666666666666666)
In [1]: import matplotlib.colors as mc
In [2]: mc.colorConverter
Out[2]: <matplotlib.colors.ColorConverter at 0x7fa58d67f400>
In [3]: mc.colorConverter.to_rgb('000000')
Out[3]: (0.0, 0.0, 0.0)
In [4]: mc.colorConverter.to_rgb('aaaaaa')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/home/tcaswell/source/p/matplotlib/lib/matplotlib/colors.py in to_rgb(self, arg)
305 else:
--> 306 fl = float(argl)
307 if fl < 0 or fl > 1:
ValueError: could not convert string to float: 'aaaaaa'
During handling of the above exception, another exception occurred:
ValueError Traceback (most recent call last)
<ipython-input-4-e922cd98d828> in <module>()
----> 1 mc.colorConverter.to_rgb('aaaaaa')
/home/tcaswell/source/p/matplotlib/lib/matplotlib/colors.py in to_rgb(self, arg)
326 except (KeyError, ValueError, TypeError) as exc:
327 raise ValueError(
--> 328 'to_rgb: Invalid rgb arg "%s"\n%s' % (str(arg), exc))
329 # Error messages could be improved by handling TypeError
330 # separately; but this should be rare and not too hard
ValueError: to_rgb: Invalid rgb arg "aaaaaa"
could not convert string to float: 'aaaaaa'
In [6]: import matplotlib
In [7]: matplotlib.__version__
Out[7]: '1.5.2' |
I think if someone passes |
How about we warn if we come across a string that could be miscontrued as something like that? Possibly leading to a deprecation of scientific notation? |
matplotlib.rcParams['axes.prop_cycle'] = cycler('color', ['8e4585', 'r']) | ||
|
||
assert mcolors.to_hex("C0") == '#8e4585' | ||
# if '8e4585' gets parsed as a float before it gets detected as a hex colour it will be interpreted as a very |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line needs to be wrapped to 80 chr
I propose the following:
|
Just to be clear, I think hex colors without a leading |
Great, sorry I misread. I am not where where, other than in the rcparams files (which have parsing issues), we accept hex with out a leading |
Ah, of course, I didn't realize there was the issue with parsing comments... forget what I said 😃 |
👍 Thanks @myyc |
FIX: hex parsing in rc files Colours like 'XeYYYY' don't get recognised properly if X, Y's are digits
Backported to v2.x as 3f72974 |
'8e1245'
gets recognised as8*10^(1245)
. While this is intended behaviour, all aroundmatplotlib
there seems to be some ambiguity as to whether hex colours should have a trailing hash (e.g.:hex2color
fails without hash while colours in acycler
in a stylesheet fail with a hash).This patch doesn't fix cases such as
0e1123
because they get interpreted as0.0
so it probably needs a more fundamental decision.Thoughts?