File Exchange

image thumbnail

Adaptive Colormap Function Creates a Colormap Specific to Data

version 1.5 (2.85 KB) by

Easily design and fit a colormap to the values in your data!

1 Download

Updated

View License

  Tristan Ursell
  Nov 2013
  Adaptive colormap
 
  rgbslide(mat_in,...)
  slide_map = rgbslide(...);
 
  This function creates, on the fly, an active / adaptive colormap for the
  data given by the matrix 'mat_in', and can be used in combination with
  image generation in a figure. This can be very useful for coloring
  data according to its absolute value. For instance, if your data ranges
  from -rand to rand and you want the positive values in that image to be
  zero=black --> positive number=red, and the negative values to be
  zero=black --> negative numbers=blue -- the colormap would have to
  actively adjust itself to the image data -- that is what this function
  does. Try out the examples.
 
  Another nice thing is that if you use zero_color = [0 0 0]; (i.e. black)
  then the colormap is guaranteed to be increasing luminosity in each
  direction (unlike most other colormaps, e.g. 'jet' or 'hsv').
 
  This function has a number of options:
 
  'zero_val' is the value that will map to the color 'zero_color',
  the default is 0.
 
  'low_val' is the value to which 'low_color' corresponds, if no value is
  specified then 'low_val' will be min(mat_in(:)).
 
  'high_val' is the value to which 'high_color' corresponds, if no value is
  specified then 'high_val' will be max(mat_in(:)).
 
  'zero_color' is a 1 x 3 color vector specifying the color of the value
  'zero_val', the default is black (0 0 0).
 
  'low_color' is a 1 x 3 color vector specifying the color of the lowest
  value in the map, the default is blue (0 0.25 1).
 
  'high_color' is a 1 x 3 color vector specifying the color of the highest
  value in the map, the default is red (1 0.25 0).
 
  The term 'equal' may be invoked to keep the colormapping around
  'zero_val' constrained to equal slope on each side, or no argument will
  allow the slopes to vary independently so as to maximize the color
  contrast on either side of 'zero_val'.
 
  'res' sets the resolution of the colormap, such that the output
  'slide_map' will be res x 3 in size.
 
  SEE ALSO: colormap, stretchlim
 
  EXAMPLES:
 
  dx=500;
  Xpos=ones(dx,1)*(1:dx);
  Ypos=Xpos';
 
  Im1=cos(4*pi*Xpos/dx).*cos(6*pi*Ypos/dx)+5*((Xpos-dx/2).^2/dx^2+(Ypos-dx/2).^2/dx^2);
 
  figure;
  imagesc(Im1)
  axis equal tight
  rgbslide(Im1);
  colorbar
 
  figure;
  imagesc(Im1)
  axis equal tight
  rgbslide(Im1,'equal');
  colorbar
 
  figure;
  imagesc(Im1)
  axis equal tight
  rgbslide(Im1,'zero_color',[1 1 1]);
  colorbar
 
  figure;
  imagesc(Im1)
  axis equal tight
  rgbslide(Im1,'zero_val',1);
  colorbar
 
  figure;
  imagesc(Im1)
  axis equal tight
  rgbslide(Im1,'zero_val',-3);
  colorbar
 
  figure;
  imagesc(Im1)
  axis equal tight
  rgbslide(Im1,'zero_val',5);
  colorbar
 
  figure;
  imagesc(Im1)
  axis equal tight
  rgbslide(Im1,'high_color',[0 1 0.25],'low_color',[1 0 0.8]);
  colorbar
 
  figure;
  imagesc(Im1)
  axis equal tight
  rgbslide(Im1,'low_val',-1/2,'high_val',2);
  colorbar
 
  figure;
  imagesc(Im1)
  axis equal tight
  colormap(jet)
  colorbar

Comments and Ratings (4)

thalia

thalia (view profile)

Very nice work
I am creating 3 maps. In 2 of them the 0 value in the colormap column does not show by the color white as I have specified but is shifted a lottle more donw. Can you help me debug it?

Tristan Ursell

Hey Blake -- found the single character bug that caused this! Be careful with the "<" vs. "<=" :)

GradStudent

Works great, thanks for the submission!

If anything: it would be nice if on a plot of 0-100, there was only red and black showing on the plot - there's always blue and red showing on the plots, even if no negative values are present.

Tristan Ursell

I'll fix the typos in the description soon.

Updates

1.5

corrected a small bug

1.4

matlab central uploaded the wrong file

1.3

shortened code, fixed a bug

1.2

Significant upgrades to the code, can now handle specifications of all control points, colors, and any resolution, with any input matrix values.

1.1

updates to the description and instructions.

MATLAB Release
MATLAB 7.14 (R2012a)
Acknowledgements

Inspired: Zerocolor

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video