function rgb = vals2colormap(vals, colormap, crange)
% Take in a vector of N values and return and return a Nx3 matrix of RGB
% values associated with a given colormap
%
% rgb = AFQ_vals2colormap(vals, [colormap = 'jet'], [crange])
%
% Inputs:
% vals = A vector of values to map to a colormap or a cell array of
% vectors of values
% colormap = A matlab colormap. Examples: colormap = 'autumn';
% colormap = 'jet'; colormap = 'hot';
% crange = The values to map to the minimum and maximum of the colormap.
% Defualts to the full range of values in vals.
%
% Outputs:
% rgb = Nx3 matrix of rgb values mapping each value in vals to the
% corresponding rgb colors. If vals is a cell array then rgb
% will be a cell array of the same length
%
% Example:
% vals = rand(1,100);
% rgb = AFQ_vals2colormap(vals, 'hot');
%
% Copyright Jason D. Yeatman, June 2012
if ~exist('colormap','var') || isempty(colormap)
colormap = 'jet';
end
%
if ~iscell(vals)
if ~exist('crange','var') || isempty(crange)
crange = [min(vals) max(vals)];
end
% Generate the colormap
cmap = eval([colormap '(256)']);
% Normalize the values to be between 1 and 256
vals(vals < crange(1)) = crange(1);
vals(vals > crange(2)) = crange(2);
valsN = round(((vals - crange(1)) ./ diff(crange)) .* 255)+1;
% Convert any nans to ones
valsN(isnan(valsN)) = 1;
% Convert the normalized values to the RGB values of the colormap
rgb = cmap(valsN, :);
elseif iscell(vals)
if ~exist('crange','var') || isempty(crange)
crange = [min(vertcat(vals{:})) max(vertcat(vals{:}))];
end
% Generate the colormap
cmap = eval([colormap '(256)']);
for ii = 1:length(vals)
% Normalize the values to be between 1 and 256 for cell ii
valsN = vals{ii};
valsN(valsN < crange(1)) = crange(1);
valsN(valsN > crange(2)) = crange(2);
valsN = round(((valsN - crange(1)) ./ diff(crange)) .* 255)+1;
% Convert any nans to ones
valsN(isnan(valsN)) = 1;
% Convert the normalized values to the RGB values of the colormap
rgb{ii} = cmap(valsN, :);
end
end
end %for colour coding firing rate maps