 # Color maps

plotphoto

### Tom's rainbow color map #

While exploring and analyzing existing color maps in plotting libraries, I observed that a color map can be generated from three mathematical functions that specify an intensity value given the value of a position on a linear subspace, with one function for each of the red, green and blue channels in the RGB color model.

I also observed that a rainbow-like color map can be generated from three curved distributions with a peak near the start, the middle and the end of a linear subspace for red, green and blue respectively, in order or in reversed order.

Therefore, I have created my own rainbow-like color map with three functions that output unnormalized normal distributions with different values of μ and σ for red, green and blue channels.

Note that an unnormalized normal distribution here means that it has a global maximum of 1, while a typical normalized normal distribution has the sum of all values of x being 1.  The proposed color map is hereafter referred to as Tom's rainbow color map. The opportunity to name the color map Tombow is forgone to avoid naming conflict with the existing Japanese stationery brand.

The following code snippet includes my Python function `gen_cmap` that returns an n-quantized ListedColormap in matplotlib given a list of functions that map a position in a linear space to a color intensity value for each channel. It is used to generate Tom's rainbow color map from an easily configurable set of mathematical functions.

``import numpy as npfrom matplotlib.colors import ListedColormapfrom matplotlib.cm import register_cmapdef gen_cmap(funcs, n=256, **kwargs):    X = np.linspace(0, 1, n)    Y = np.asarray([ list(map(func, X)) for func in funcs ]).T    return ListedColormap(Y, **kwargs)register_cmap(cmap=gen_cmap([    lambda x: np.exp(-0.5 * (x-0.75)**2 / 0.25**2),     lambda x: np.exp(-0.5 * (x-0.5)**2 / 0.25**2),     lambda x: np.exp(-0.5 * (x-0.2)**2 / 0.35**2), ], name='TomsRainbow'))``

### Plots for color maps #

Tom's rainbow color map `TomsRainbow` can be compared with existing rainbow-like color maps, including `rainbow` and `turbo`.

• `TomsRainbow` has darker reds, less purplish blues and smoother intensity changes than `rainbow`. Both `TomsRainbow` and `rainbow` have lighter colors overall when compared with `turbo` and `jet`.

• `TomsRainbow` has brighter colors especially on both ends and smoother intensity changes than `turbo`.

The use of Tom's rainbow color map is demonstrated in another post that includes contour surface plots of various mathematical functions using my color map.

In the following figure, plots are provided for various color maps including `TomsRainbow` and other existing color maps. From left to right:

1. Surface plot is provided. The equation of the surface plot used is z = (x + y) / 2, where variable x is on the horizontal axis, variable y is on the vertical axis, and variable z is the surface. Undesirable banding effect, if any, can be more easily observable as diagonal lines that are perpendicular to the dashed line in the diagonal plot than in the vertical or horizontal plot of the color map.

2. A line plot of the brightness levels of the red, green and blue components in the RGB color model is generated. An additional color bar at the bottom of the line plot is provided for reference.

3. A line plot of the values of the J component (luminance) in the CAM02 Uniform Color Space is generated to show the perceived brightness level.

4. A line plot of the values of the a* component (red-green) and the b* component (yellow-blue) in the CAM02 Uniform Color Space is generated to show the perceived brightness level.  Mesh plot with color bar and line plot of RGB components of different color maps

Changelog

• Mar 2021 Added color map generation code snippet with description.
• Feb 2021 Added Tom's Rainbow color map with new plots including line plots of CAM02-UCS components in SVG format.
• Jan 2021 Updated plot to include over 30 matplotlib color maps.
• Oct 2020 Added line plots of RGB components for each color map.
• Oct 2020 Re-organized content in bullet points. 