Search code examples
pythonmatlabmatplotlibmatlab-figurecolormap

How to recover key colors from a colormap?


Generally we interpolate key colors to make a colormap.

enter image description here

Can we do the reverse, getting the key colors (approximately) from a colormap?

I convert the colormap to a gray scale, divide the gray vector into piecewise line segments, retain the key points and return the colors at the key index. But it doesn't work well for vivid colormaps.


Solution

  • If you want to recover the key colors from the interpolated color table you should look at the first derivatives of the color table in each color channel separately. If the color table is linearly interpolated between the key colors and if you are lucky you may be able to detect a change in inclination at each key color. If there is no change the key color was not necessary to create this color table.

    In the second derivative you will see at least one extremum in at least one chanel.

    Example

    I use the "rainbow" color table in octave which is similar to matlabs rainbow color table to demonstrate the method. This plots the second derivatives of all RGB channels.

    plot(diff(diff(colormap('rainbow'),1),1))
    

    The resulting plot is displayed here: Plot Example: second derivative of colormap "rainbow"

    Of course you have to add key colors at start and end of colormap.