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:
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
- % creates a custom colormap
- % biafra ahanonu
- % started: 2014.01.03
- % inputs
- % colorList - a cell array containing vectors with RGB values, e.g. {[1 1 1], [0 0 1],[1 0 0]}
- % variable inputs
- % nPoints - numeric value specifying the number of points between each color value in the gradient
- % outputs
- %
- % changelog
- % 2014.05.09 - commented to make more readable
- % TODO
- %
- %========================
- options.nPoints = 50;
- % get options
- % display(options)
- % unpack options into current workspace
- % fn=fieldnames(options);
- % for i=1:length(fn)
- % eval([fn{i} '=options.' fn{i} ';']);
- % end
- %========================
- try
- % check that colorList was input, else return default map
- if isempty(colorList)
- colorList = {[1 1 1], [0 0 1],[1 0 0]};
- end
- redMap = [];
- greenMap = [];
- blueMap = [];
- % loop over each color in the list and append its values to the RGB map values
- % linspace is used to create an even gradient between the values specified
- end
- % concatenate the RGB map values to create a custom colormap matrix
- outputColormap = [redMap', greenMap', blueMap'];
- catch err
- end
- % gets default options for a function, replaces with inputArgs inputs if they are present
- % biafra ahanonu
- % 2014.02.17 [22:21:49]
- %
- % inputs
- % options - structure with options as fieldnames
- % inputArgs - varargin containing name-value pairs passed from parent function.
- % list of valid options to accept, simple way to deal with illegal user input
- % loop over each input name-value pair, check whether name is valid and overwrite fieldname in options structure.
- if ischar(val)
- % allow input of an options structure that overwrites existing fieldnames with its own, for increased flexibility
- [options] = mirrorRightStruct(inputOptions,options);
- end
- else
- continue;
- end
- end
- function [pullStruct] = mirrorRightStruct(pushStruct,pullStruct)
- % overwrites fields in pullStruct with those in pushStruct, other pullStruct fields rename intact
- % more generally, copies fields in pushStruct into pullStruct, if there is an overlap in field names, pushStruct overwrites.
- iName = pushNames{name};
- pullStruct.(iName) = pushStruct.(iName);
- end