custom colormaps in matlab

summary

A function to easily create custom colormaps in Matlab.

There are times when the custom colormaps provided by Matlab don't cut it. An easy way to create nice, smooth gradients for custom colormaps is to use linspace's ability to create linearly spaced vectors between two arbitrary numerical values. Building on this, I made a simple function that takes a cell array of different RGB values and outputs a smooth gradient. For example, inputting a cell array with values {[1 0 0], [0 1 0],[0 0 1]} makes a gradient that gradually changes from red to green to blue. You can modify the number of points in the gradient by specifying the variable argument 'nPoints', an example usage:

download codeExample.m

Matlab M
  1. myMap = customColormap({[1 0 0], [0 1 0],[0 0 1]},'nPoints',25);
  2. colormap(myMap);

This code also takes advantage of getOptions, which I discussed previously (see dealing with variable options (varargin) in matlab) as a way to handle variable input arguments in a consistent manner.

M-files and example code below; I'll add it to a GitHub repository at some point in the future.

customColormap.m
getOptions.m
download customColormap.m

Matlab M
  1. function [outputColormap] = customColormap(colorList,varargin)
  2.         % creates a custom colormap
  3.         % biafra ahanonu
  4.         % started: 2014.01.03
  5.         % inputs
  6.                 % colorList - a cell array containing vectors with RGB values, e.g. {[1 1 1], [0 0 1],[1 0 0]}
  7.         % variable inputs
  8.                 % nPoints - numeric value specifying the number of points between each color value in the gradient
  9.         % outputs
  10.                 %
  11.  
  12.         % changelog
  13.                 % 2014.05.09 - commented to make more readable
  14.         % TODO
  15.                 %
  16.  
  17.         %========================
  18.         options.nPoints = 50;
  19.         % get options
  20.         options = getOptions(options,varargin);
  21.         % display(options)
  22.         % unpack options into current workspace
  23.         % fn=fieldnames(options);
  24.         % for i=1:length(fn)
  25.         %       eval([fn{i} '=options.' fn{i} ';']);
  26.         % end
  27.         %========================
  28.  
  29.         try
  30.                 % check that colorList was input, else return default map
  31.                 if isempty(colorList)
  32.                         colorList = {[1 1 1], [0 0 1],[1 0 0]};
  33.                 end
  34.                 nColors = length(colorList);
  35.                 redMap = [];
  36.                 greenMap = [];
  37.                 blueMap = [];
  38.                 % loop over each color in the list and append its values to the RGB map values
  39.                 for i=1:(nColors-1)
  40.                         % linspace is used to create an even gradient between the values specified
  41.                         redMap = [redMap linspace(colorList{i}(1),colorList{i+1}(1),options.nPoints)];
  42.                         greenMap = [greenMap linspace(colorList{i}(2),colorList{i+1}(2),options.nPoints)];
  43.                         blueMap = [blueMap linspace(colorList{i}(3),colorList{i+1}(3),options.nPoints)];
  44.                 end
  45.                 % concatenate the RGB map values to create a custom colormap matrix
  46.                 outputColormap = [redMap', greenMap', blueMap'];
  47.         catch err
  48.                 display(repmat('@',1,7))
  49.                 disp(getReport(err,'extended','hyperlinks','on'));
  50.                 display(repmat('@',1,7))
  51.         end
download getOptions.m

Matlab M
  1. function [options] = getOptions(options,inputArgs,varargin)
  2.     % gets default options for a function, replaces with inputArgs inputs if they are present
  3.     % biafra ahanonu
  4.     % 2014.02.17 [22:21:49]
  5.     %
  6.     % inputs
  7.     %   options - structure with options as fieldnames
  8.     %   inputArgs - varargin containing name-value pairs passed from parent function.
  9.  
  10.     % list of valid options to accept, simple way to deal with illegal user input
  11.     validOptions = fieldnames(options);
  12.  
  13.     % loop over each input name-value pair, check whether name is valid and overwrite fieldname in options structure.
  14.     for i = 1:2:length(inputArgs)
  15.         val = inputArgs{i};
  16.         if ischar(val)
  17.             % allow input of an options structure that overwrites existing fieldnames with its own, for increased flexibility
  18.             if strcmp('options',val)
  19.                 inputOptions = inputArgs{i+1};
  20.                 [options] = mirrorRightStruct(inputOptions,options);
  21.             elseif ~isempty(strmatch(val,validOptions))
  22.                 options.(val) = inputArgs{i+1};
  23.             end
  24.         else
  25.             continue;
  26.         end
  27.     end
  28.  
  29. function [pullStruct] = mirrorRightStruct(pushStruct,pullStruct)
  30.     % overwrites fields in pullStruct with those in pushStruct, other pullStruct fields rename intact
  31.     % more generally, copies fields in pushStruct into pullStruct, if there is an overlap in field names, pushStruct overwrites.
  32.     pushNames = fieldnames(pushStruct);
  33.     for name = 1:length(pushNames)
  34.         iName = pushNames{name};
  35.         pullStruct.(iName) = pushStruct.(iName);
  36.     end

-biafra

comments

comments powered by Disqus

©2006-2017 | biafra ahanonu | updated 19 june 2017
biafra ahanonu