Covich
Covich

Reputation: 2814

Interpolating a matlab colormap

In Matlab, using a predefined colormap, I want to associate a color triplet to any value in a range [v_min , v_max].

How can I interpolate this colormap ?

Upvotes: 4

Views: 2695

Answers (1)

Ander Biguri
Ander Biguri

Reputation: 35525

The following code will do. Note that you need to scale and offset the variable you are plotting from [vmin vmax] to [0 1]

val % Variable scaled correctly to the colormap scale (0-1).

hsv=rgb2hsv(cmap);
cm_data=interp1(linspace(0,1,size(cmap,1)),hsv,val);
cm_data=hsv2rgb(cm_data);

cm_data should have the triplet you are looking for. Code taken from here

Note: This will do what you ask, but I am not sure if you want to do this. If this is for plotting purposes, it is a bad idea, as the colormap needs to increase linearly with data, and MATLABs color plotting will assume it is.

If your objective is to make sure you have enough a big colormap then change val to linspace(0,1,300).

Upvotes: 5

Related Questions