Code covered by the BSD License  

Highlights from
mapcolors

image thumbnail

mapcolors

by

 

Create a custom RGB colormap by interpolating between two pre-defined extremes.

mapcolors(start,finish,nBits)
function map = mapcolors(start,finish,nBits)
% MAPCOLORS Create a custom RGB colormap by interpolating between two pre-defined
% extremes.
%
% MAPCOLORS(START,FINISH) returns a 64 x 3 matrix containing RGB
% intensities linearly interpolated between the values specified in START
% and FINISH, which are 3-element vectors containing RGB intensities 0-255.
% 
% MAPCOLORS(START,FINISH,NBITS) returns an nBITS x 3 matrix instead.
%
% EXAMPLE: To recreate Matlab's built-in 'winter' colormap, execute the
% following:
%
% wintermap = mapcolors([0 0 255],[0 255 127.5]);
% 
%      Observe that:
% size(wintermap) % >>  [64 3]
% wintermap(1,:) % >> [0 0 1]
% wintermap(64,:) % >> [0 1 0.5]
%
% See also COLORMAP

if nargin<2
    error('Not enough inputs. Must specify START and FINISH color vectors.')
elseif nargin<3
    nBits = 64;
else
    if ~isscalar(nBits) || round(nBits)~=nBits
        error('nBits must be a scalar integer.')
    end
end
    
if numel(start)~=3 || numel(finish)~=3
    error('Inputs must be three-element vectors.')
end
         
start = start/255; %converts to 0-1 scale for MATLAB
finish = finish/255;

map = zeros(nBits,3);
for c = 1:3
    map(:,c) = linspace(start(c),finish(c), nBits)';
end

Contact us