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
bahanonu [at] alum.mit.edu

additional articles to journey through:

dealing with variable options (varargin) in matlab
17 february 2014 | programming

There are various ways to handle variable input arguments (varargin) in Matlab. I present the standard getOptions method I develop[...]ed that is simple, flexible, and allows for more readable code.

dslr chronicles #1: getting ready for mars
09 september 2020 | photography

San Francisco looks good in red. Select pictures from a Martian-themed bike ride around SF.[...]

¿qué es la calle?
24 may 2013 | short story | spanish

Había terminado y salé para mi cocina. Tenía hambre pero este día no había comida dentro de mi despensa. Me fui y caminé hacienda[...] la Tport—una máquina que puede transportar una persona a otro lugar sin energía y tiempo. Cuando entré la máquina, toqué algunos botónes y esperé. Pero nada ocurrió y lo hice las mismas acciones otra vez—y nada ocurrió.

How would the disappearance of streets affect the social fabric? This short story briefly (in castellano!) explores a world in which instantaneous, free transport is possible. Meant mainly to practice my spanish, i plan to follow-up with a more detail story in the future.

why you should watch black mirror
29 november 2015 | television

Black Mirror is a unique and ever timely experience. The vignettes are spot on and while each explores a different aspect of our interactio[...]n with technology, the internet, and social media, there is an underlying dystopian view of the world that unifies the whole series. Watch it.

©2006-2025 | Site created & coded by Biafra Ahanonu | Updated 21 October 2024
biafra ahanonu