Be the first to rate this file! 8 Downloads (last 30 days) File Size: 2.85 KB File ID: #39453
image thumbnail

Adaptive Colormap Function Creates a Colormap Specific to Data

by

 

13 Dec 2012 (Updated )

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

| Watch this File

File Information
Description

  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

Acknowledgements

This file inspired Zerocolor.

Required Products MATLAB
MATLAB release MATLAB 7.14 (R2012a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
26 Oct 2013 Tristan Ursell

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

19 Aug 2013 Blake

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.

14 Dec 2012 Tristan Ursell

I'll fix the typos in the description soon.

Updates
25 Jan 2013

updates to the description and instructions.

11 Feb 2013

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

12 Feb 2013

shortened code, fixed a bug

12 Feb 2013

matlab central uploaded the wrong file

29 Oct 2013

corrected a small bug

Contact us