Skip to content

New color conversion machinery. #6382

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

Merged
merged 11 commits into from
May 14, 2016
Merged

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented May 8, 2016

The main goal is to support #rrggbbaa hex color spec (#5461, #6196). The API is also considerably simplified: the conversion API is now reduced to

  • is_color_like(c)
  • to_rgba(c, alpha=None)
  • to_rgba_array(c, alpha=None)
  • to_hex(c, alpha=None)

Full backwards compatibility is maintained (perhaps the compatibility layer should be deprecated in a later PR).

@efiring
Copy link
Member

efiring commented May 8, 2016

Based on an initial scan of the code, I like it. It's much clearer and cleaner than the original.

@anntzer
Copy link
Contributor Author

anntzer commented May 8, 2016

Can someone help with the test failures on python-2.7/numpy-1.6? I have never been able to locally get 100% matching images in the tests even on recent pythons and numpys so that'll be hard for me to investigate.
It would be nice to get the docs to build too because they also provide quite a bit of testing, but again I can't do much with the given error.

@efiring
Copy link
Member

efiring commented May 8, 2016

The 2.7 test failures that have just now started appearing are all in the svg images, not png. Either something has changed in Travis, or there is a bad 2.7-specific interaction between the most recent commits and the svg backend.

@anntzer
Copy link
Contributor Author

anntzer commented May 8, 2016

Perhaps it would be nice to add Python (most recent) / numpy 1.6 and Python 2.7 / numpy (most recent) to the test matrix to simplify the debugging here.

Also, according to AppVeyor this passes with Python 2.7 / numpy 1.8...

@anntzer anntzer force-pushed the rgba-support branch 2 times, most recently from 3cc1575 to 526a0e8 Compare May 8, 2016 07:06
@anntzer
Copy link
Contributor Author

anntzer commented May 8, 2016

Got it, this was due to a change in the behavior of the builtin round between Python2 and Python3 (Py2 rounds away from zero, Py3 rounds to even). np.round always rounds to even so use it in to_hex. Tests now pass.

seaborn messes up with the color mapping (for good reasons IMO, and it was part of the public API anyways). I guess this means we should maintain it as public, and hide away the cache. Instead, the color mapping should detect changes to itself and reset the cache as needed.

@efiring
Copy link
Member

efiring commented May 8, 2016

Regarding the docs build, it seems that the problem occurs as Travis is trying to download and install basemap from github. It has been failing more often than not today--but not always.

@jenshnielsen
Copy link
Member

For the docs issue see #6379

@efiring
Copy link
Member

efiring commented May 8, 2016

@anntzer, your new version of to_rgba_array is nice and compact, but I think it is very inefficient for one important case where we want it to be fast: when its argument is already a valid rgba array, and potentially a large one.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone May 8, 2016
@tacaswell tacaswell closed this May 8, 2016
@tacaswell tacaswell reopened this May 8, 2016
@anntzer
Copy link
Contributor Author

anntzer commented May 8, 2016

The latest commit should cover the performance issues.

@efiring
Copy link
Member

efiring commented May 8, 2016

Another odd 2.7-only failure: in test_boxplot.

@anntzer
Copy link
Contributor Author

anntzer commented May 8, 2016

Apparently returning builtin floats instead of numpy floats solves the issue...

if color is not None:
raise ValueError(
'Illegal format string "%s"; two color symbols' % fmt)
color = c
elif c == 'C' and i < len(chars) - 1:
color_cycle_number = int(chars[i + 1])
color = mcolors.colorConverter._get_nth_color(color_cycle_number)
color = mcolors.to_rgba("C{}".format(color_cycle_number))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still like the idea of using get_nth_color as an API here. We may change the exact formatting and specifics of this in the future. (Plus, why parse the string twice?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous version may look format independent, but if you check the parsing code just around it it's clearly also dependent on the exact format of CN colors. Plus, it uses a private API. (And I wouldn't worry too much about the performance implications of doing an extra regexp match.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a wash either way. The elif statement above is encoding the details of the formatting. Using _get_nth_color only buries half the body.

On the other hand, it is our policy that we can use mpl private API anywhere in the mpl code base (that is the point of the private APIs).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to think that private APIs are module-level (or class-level) private (rather than project-level), but don't feel strongly about this so either way works for me.

@mdboom
Copy link
Member

mdboom commented May 9, 2016

Thanks for this much needed improvement. I have nothing to add to @efiring's comments.

@anntzer
Copy link
Contributor Author

anntzer commented May 9, 2016

Moved 1-letter codes to their own mapping in _color_data.
Do we want to deprecate the old API in code too? IMO the main reason to do so is not because it is ugly :-), but because it favors silently dropping alpha (to_rgb and rgb2hex).

@tacaswell
Copy link
Member

I am 👎 on dropping the old API.

It is a very old part of the public API so may have wide-spread use in user code and it is not onerous to continue supporting.

@anntzer
Copy link
Contributor Author

anntzer commented May 9, 2016

Fair enough, but what about at least removing it from the docs (via sphinx' autodoc-skip-member), i.e. deprecate-in-docs? By the way, the current API entry is rather ugly: http://matplotlib.org/api/colors_api.html#matplotlib.colors.ColorConverter

@tacaswell
Copy link
Member

I am fine with removing it from the docs / re-writing those docs.

On Mon, May 9, 2016 at 12:17 PM Antony Lee notifications@github.com wrote:

Fair enough, but what about at least removing it from the docs (via
sphinx' autodoc-skip-member), i.e. deprecate-in-docs? By the way, the
current API entry is rather ugly:
http://matplotlib.org/api/colors_api.html#matplotlib.colors.ColorConverter


You are receiving this because you modified the open/close state.
Reply to this email directly or view it on GitHub
#6382 (comment)

@anntzer
Copy link
Contributor Author

anntzer commented May 9, 2016

Now that we're there, what about replacing the "do-nothing" plt.colors function (only there for its docstring) by the matplotlib.colors module itself? The module docstring can be rewritten in a way suitable for interactive doc reading too.

@tacaswell tacaswell merged commit 22a7b95 into matplotlib:master May 14, 2016
@tacaswell
Copy link
Member

@anntzer Thanks for this and thank you for putting up with our slow code review.

@anntzer anntzer deleted the rgba-support branch May 14, 2016 21:44
@anntzer
Copy link
Contributor Author

anntzer commented May 14, 2016

No worries, thanks. Also updated #6383 accordingly.

tacaswell added a commit to tacaswell/matplotlib that referenced this pull request May 16, 2016
ENH/API: New color conversion machinery
efiring added a commit that referenced this pull request May 16, 2016
Merge pull request #6382 from anntzer/rgba-support
@QuLogic
Copy link
Member

QuLogic commented Jun 15, 2016

Backport to v2.x is 3281bcd.

@QuLogic QuLogic modified the milestones: 2.0 (style change major release), 2.1 (next point release) Jun 15, 2016
@QuLogic QuLogic mentioned this pull request Jan 16, 2017
tacaswell added a commit to QuLogic/matplotlib that referenced this pull request Jan 16, 2017
This dictionary was added in matplotlib#5775 sha: 9655f45 (and not yet released)
and was removed from internal use by matplotlib#6382 sha: (master) 22a7b95 / (2.x
backport) 3281bcd
@anntzer anntzer mentioned this pull request Nov 4, 2017
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants