How to create a custom dynamic colormap?

Hi,
I have been wondering about how to create a custom colormap, with a dynamic range similar to the default colormaps in MATLAB. The call to the colorbar will be something like,
customColormap = getCustomColormap(nColors);
It is possible that there is no clear solution to this problems, but it would be great if anyone have an idea to how to proceed.
Best Regards Patrik

 Risposta accettata

Hi!
You can use the command "colormap" to create color maps. The built-in colormaps can be scaled dynamically, like
colormap(jet(nColors))
You can as well build your own color maps. A color map is a m-by-3 matrix of real numbers between 0.0 and 1.0, specifying RGB values.

4 Commenti

Thank you for the answer, but what I really meant was that I wanted to specify an own colormap that could be scaled dynamically. Eg I want three basic colors black green and red and then want to be able to create an algorithm to scale these to a number of variants depending on the size of the colormap. Is there any basic way to do this or do I need to do this by trial and error?
BR/Patrik
Do you know the interactive colormap editor? With this you can build your own color map.
What you can do is: specify your edge colors of the color map. Then you can use matlab's interpolation routines to calculate a custom colomap. The following example might help you.
% taken from "doc surf"
[X,Y,Z] = peaks(30);
surfc(X,Y,Z)
colormap hsv
axis([-3 3 -3 3 -10 5])
% number of colors
n = 20;
% color map with red and blue on the edges
C = [1 0 0; 0 0 1];
% convert to HSV for interpolation
C_HSV = rgb2hsv(C);
% interpolate hue value
C_HSV_interp = interp1([0 n], C_HSV(:, 1), 1:n);
% compose full HSV colormap
C_HSV = [C_HSV_interp(:), repmat(C_HSV(2:3), n, 1)];
% convert back to RGB
C = hsv2rgb(C_HSV);
% set colormap
colormap(C)
colorbar
Thank you this colormap editor is amazing. For the automatic rescaling I guess i gets more complicated the more advanced colormap, but thank you for giving me a start.
A small correction:
C_HSV = [C_HSV_interp(:), repmat(C_HSV( 1, 2:3), n, 1)];

Accedi per commentare.

Più risposte (0)

Categorie

Richiesto:

il 17 Ott 2013

Commentato:

il 10 Mar 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by