-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
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
Changes from 1 commit
f6f908e
75b0d13
72f34be
09a0f4c
2fe2c26
5a4432c
bc6ad2a
d9db062
cb3d7c9
d492ba4
7c2ec4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
For use e.g. by `seaborn.set(color_codes=True)`.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
|
@@ -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): | ||
|
@@ -121,11 +139,11 @@ def to_rgba(c, alpha=None): | |
colors = prop_cycler._transpose()['color'] | ||
c = colors[int(c[1]) % len(colors)] | ||
try: | ||
rgba = _colors_cache[c, alpha] | ||
rgba = _colors_full_map._cache[c, alpha] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -247,7 +265,7 @@ class ColorConverter(object): | |
""" | ||
|
||
colors = _colors_full_map | ||
cache = _colors_cache | ||
cache = _colors_full_map._cache | ||
|
||
@staticmethod | ||
def to_rgb(arg): | ||
|
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 needs a cut-out incase the default prop_cycle does not contain a
color
key. The default in this case should probably be'k'
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 was copypasted from the previous implementation. I don't know anything about
cycler
, but I guessprop_cycler._transpose().get('color', 'k')
would work?