Skip to content
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

Color-Map Overhaul #52

Merged
merged 4 commits into from
Oct 14, 2021
Merged

Color-Map Overhaul #52

merged 4 commits into from
Oct 14, 2021

Conversation

chr5tphr
Copy link
Owner

@chr5tphr chr5tphr commented Oct 14, 2021

  • added ColorMap, which are objects created from a color-map specification language (cmsl), which specifies linearly interpolated colormaps
  • added LazyColorMapCache, which stores, compiles and caches color maps on demand
  • implement existing built-in color maps with cmsl
  • implement CMAPS (dictionary of built-in color maps) with a LazyColorMapCache object
  • implement get_cmap, which is a convenience function used everywhere where color maps are required and returns a ColorMap object either by looking up its name in the built-in color maps, creates a new one if it is valid cmsl code, or returns the passed ColorMap if it already receives an instance
  • added 4 new built-in color maps based on Added more colormaps to image.py #19

cmsl are comma-separated hexa-decimal values, with an arbitrary index before each value. If no index is specified, the indices of the colors are evenly spaced from 0 to 255, or between their neighbors with indices.

A color-map from blue to white to red may be specified equivalently as

  • 00f,000,f00
  • 00f,80:000,f00
  • 0000ff,000,ff:f00
  • 00:f00,80:000000,ff:ff0000.

If the first and last indices are not zero, they are assumed to not change until the respective boundaries, ie. a color map which only shows the sign as either blue or red may be specified as

  • 79:00f,80:f00

If indices are supplied, they must be in ascending order from left to right.

Built-in color-maps are implemented as

sources = {
    # black to white
    'gray': '000,fff',
    # white to red
    'wred': 'fff,f00',
    # white to blue
    'wblue': 'fff,00f',
    # black to red to yellow to white
    'hot': '000,f00,ff0,fff',
    # black to blue to cyan
    'cold': '000,00f,0ff',
    # combination of cold (reversed) and hot, centered around black
    'coldnhot': '0ff,00f,80:000,f00,ff0,fff',
    # combination of wblue (reversed) and wred, centered around white
    'bwr': '00f,80:fff,f00',
    # red to white to blue as in the french flag
    'france': '0055a4,80:fff,ef4135',
    # red to white to blue with brightness 0xd0
    'seismic': 'd00000,80:d0d0d0,0000d0',
    # cyan to white to magenta with brightness 0xd0
    'coolio': '00d0d0,80:d0d0d0,d000d0',
    # green to white to magenta with brightness 0xd0
    'coleus': '00d000,80:d0d0d0,d000d0',
}
Here is an example of all built-in color-maps visualized:

In order:

  • gray
  • wred
  • wblue
  • hot
  • cold
  • coldnhot
  • bwr
  • france
  • seismic
  • coolio
  • coleus

image

Features include
- ColorMap, which implements the color-map
  specification language (cmsl)
- LazyColorMapCache, a dict-like which stores cmsl source code strings,
  which are compiled lazily and cached when accessed
- all built-in color maps are now implemented using the color-map
  specification language (cmsl) from zennit.cmap.ColorMap
- CMAPS is now a LazyColorMapCache instead of a simple dict, to compile
  color-maps not on load, but on demand
- get_cmap, based on its input, either recalls a built-in color map,
  compiles a new color map, or returns the input if it already is a
  ColorMap instance, to conveniently specify color maps in different
  ways
- visualize multiple color maps at once, and all built-in ones by
  default
- france, which goes from blue to white to red, in the exact colors of the french flag
- seismic, which is blue to white to red with brightness d0
- coolio, which is cyan to white to magenta with brightness d0
- coleus, which is green to white to magenta with brightness d0
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.

None yet

1 participant