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
Prev Previous commit
Next Next commit
New API for accessing the named colors mapping.
For use e.g. by `seaborn.set(color_codes=True)`.
  • Loading branch information
anntzer committed May 14, 2016
commit 72f34be30f74b7049e4bfc5680673cc15e028cf3
26 changes: 22 additions & 4 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@
from ._color_data import XKCD_COLORS, CSS4_COLORS


class _ColorMapping(dict):
def __init__(self, mapping, cache):
super(_ColorMapping, self).__init__(mapping)
self._cache = cache

def __setitem__(self, key, value):
super(_ColorMapping, self).__setitem__(key, value)
self._cache.clear()

def __delitem__(self, key, value):
super(_ColorMapping, self).__delitem__(key, value)
self._cache.clear()


_colors_full_map = {
'b': (0, 0, 1),
'g': (0, 0.5, 0),
Expand All @@ -79,9 +93,13 @@
'w': (1, 1, 1)}
_colors_full_map.update(XKCD_COLORS)
_colors_full_map.update(CSS4_COLORS)
_colors_full_map = _ColorMapping(_colors_full_map, {})


_colors_cache = {}
def get_named_colors_mapping():
"""Return the global mapping of names to named colors.
"""
return _colors_full_map


def _is_nth_color(c):
Expand Down Expand Up @@ -121,11 +139,11 @@ def to_rgba(c, alpha=None):
colors = prop_cycler._transpose()['color']
Copy link
Member

Choose a reason for hiding this comment

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

This needs a cut-out incase the default prop_cycle does not contain a color key. The default in this case should probably be 'k'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was copypasted from the previous implementation. I don't know anything about cycler, but I guess prop_cycler._transpose().get('color', 'k') would work?

c = colors[int(c[1]) % len(colors)]
try:
rgba = _colors_cache[c, alpha]
rgba = _colors_full_map._cache[c, alpha]
Copy link
Member

Choose a reason for hiding this comment

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

This is looking up based on a tuple with a float in it, that might lead to a ballooning of the cache size?

I want to suggest just caching the rgb values, but keep convincing my self every suggestion I type out will not work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is obviously possible to make an alpha-less cache work, but I'm not sure the memory savings are worth it. On the other hand, that would certainly be a precondition for making the cache public (otherwise it is truly too implementation-specific and should be kept private).

except (KeyError, TypeError): # Not in cache, or unhashable.
rgba = _to_rgba_no_colorcycle(c, alpha)
try:
_colors_cache[c, alpha] = rgba
_colors_full_map._cache[c, alpha] = rgba
except TypeError:
pass
return rgba
Expand Down Expand Up @@ -247,7 +265,7 @@ class ColorConverter(object):
"""

colors = _colors_full_map
cache = _colors_cache
cache = _colors_full_map._cache

@staticmethod
def to_rgb(arg):
Expand Down