Skip to content

Make _get_rgba_face actually always return a RGBA. - #9911

Merged
jklymak merged 1 commit into
matplotlib:masterfrom
anntzer:get_rgba_face
Jan 20, 2018
Merged

jklymak merged 1 commit into
matplotlib:masterfrom
anntzer:get_rgba_face

Conversation

@anntzer

@anntzer anntzer commented Dec 2, 2017 •

Copy link
Copy Markdown
Contributor

_get_rgba_face is an internal function that is only use to set the
graphicscontext's foreground color (in Line2D.draw); it is always
effectively called as gc.set_foreground(line._get_rgba_face(...), isRGBA=True). So it makes sense to have it actually always return a
RGBA quadruplet, including when the color is "none" (in which case
mcolors.to_rgba returns (0, 0, 0, 0) regardless of alpha, which works
just fine).

This removes the need for third party graphicscontexts to handle
non-RGBA-quadruplets (specifically, None) even when set_foreground is
actually called with isRGBA=True.

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@anntzer
anntzer force-pushed the get_rgba_face branch 2 times, most recently from 56c8925 to 2d962a6 Compare December 2, 2017 09:12
Comment thread lib/matplotlib/lines.py Outdated
edgecolor = self.get_markeredgecolor()
if (isinstance(edgecolor, six.string_types)
and edgecolor.lower() == 'none'):
if cbook._str_equal(edgecolor, "none"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Original code has edgecolor.lower(), I do not see any normalization in set_markeredgecolor/get_markeredgecolor. Did I miss something?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

Comment thread lib/matplotlib/lines.py
else:
rgbaFace = mcolors.to_rgba(facecolor, self._alpha)
return rgbaFace
return mcolors.to_rgba(self._get_markerfacecolor(alt=alt), self._alpha)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The same as above

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@anntzer
anntzer force-pushed the get_rgba_face branch 3 times, most recently from 8743dce to 85191a0 Compare December 3, 2017 05:34
Comment thread lib/matplotlib/cbook/__init__.py Outdated

This helper solely exists to handle the case where *obj* is a numpy array.
"""
return isinstance(obj, six.string_types) and obj.lower() == s

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this not call lower?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

duh...

@tacaswell tacaswell added this to the v2.2 milestone Dec 20, 2017

@efiring efiring left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Apart from the docstrings I flagged, it looks fine.

Comment thread lib/matplotlib/cbook/__init__.py Outdated
def _str_equal(obj, s):
"""Return whether *obj* is a string equal to string *s*.

This helper solely exists to handle the case where *obj* is a numpy array.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I find this confusing. How does it handle that case? It returns False, so that np.array('abc') is not equal to 'abc'. Where is this needed? And why is it only the numpy array case that needs to be handled? I can imagine there might be other places in the code where this might be useful because 'obj' might be None, or a number, or...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

if obj is a number or None or etc., then, obj == s will return False just fine. The problem with ndarrays is that np.array(...) == s will return an array, which cannot be used in a boolean switch context.

The fact that np.array("abc") will compare unequal to "abc" did not change with this PR, we were previously already using isinstance().

Perhaps the docstring can be clarified to

Return whether obj is a string equal to string s.

This helper solely exists to handle the case where obj is a numpy array, because in such cases, a naive obj == s would yield an array, which cannot be used in a boolean context.

How does that sound? Feel free to propose further edits.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's good, thank you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

edited accordingly (both instances)

_get_rgba_face is an internal function that is only use to set the
graphicscontext's foreground color (in Line2D.draw); it is always
effectively called as `gc.set_foreground(line._get_rgba_face(...),
isRGBA=True)`.  So it makes sense to have it actually always return a
RGBA quadruplet, including when the color is "none" (in which case
`mcolors.to_rgba` returns (0, 0, 0, 0) regardless of alpha, which works
just fine).

This removes the need for third party graphicscontexts to handle
non-RGBA-quadruplets (specifically, None) even when set_foreground is
actually called with isRGBA=True.
@jklymak
jklymak merged commit 5fa8c5b into matplotlib:master Jan 20, 2018
@anntzer
anntzer deleted the get_rgba_face branch January 20, 2018 06:44
@QuLogic QuLogic modified the milestones: needs sorting, v2.2.0 Feb 12, 2018
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.

6 participants