Home > phiwave > @slover > private > pr_getcmap.m

pr_getcmap

PURPOSE ^

get colormap of name acmapname

SYNOPSIS ^

function [cmap, warnstr] = pr_getcmap(acmapname)

DESCRIPTION ^

 get colormap of name acmapname
 FORMAT [cmap, warnstr] = pr_getcmap(acmapname)
 
 Inputs
 acmapname   - string.  Can be (in order of precedence)
               - matrix name in base workspace
               - colour name; one of 'red','green','blue','cyan',
                 'magenta', 'yellow', 'black', 'white'
               - filename of .mat or .lut file.  If filename has no
                 extension, assumes '.mat' extension

 Outputs
 cmap        - Nx3 colormap matrix
               or empty if fails
 warnstr     - warning message if fails
 
 $Id: pr_getcmap.m,v 1.1 2005/04/20 15:05:00 matthewbrett Exp $

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [cmap, warnstr] = pr_getcmap(acmapname)
0002 % get colormap of name acmapname
0003 % FORMAT [cmap, warnstr] = pr_getcmap(acmapname)
0004 %
0005 % Inputs
0006 % acmapname   - string.  Can be (in order of precedence)
0007 %               - matrix name in base workspace
0008 %               - colour name; one of 'red','green','blue','cyan',
0009 %                 'magenta', 'yellow', 'black', 'white'
0010 %               - filename of .mat or .lut file.  If filename has no
0011 %                 extension, assumes '.mat' extension
0012 %
0013 % Outputs
0014 % cmap        - Nx3 colormap matrix
0015 %               or empty if fails
0016 % warnstr     - warning message if fails
0017 %
0018 % $Id: pr_getcmap.m,v 1.1 2005/04/20 15:05:00 matthewbrett Exp $
0019 
0020 cmap = []; warnstr = [];  
0021 if nargin < 1
0022   acmapname = '';
0023 end
0024 if isempty(acmapname)
0025   warnstr = 'No colormap name passed';
0026   return
0027 end
0028 % try a matrix first
0029 cmap = evalin('base',acmapname,'[]');
0030 if ~isempty(cmap)
0031   if size(cmap, 2)~=3
0032     warnstr = ['Colormap matrix ' acmapname ' was not N by 3'];
0033     cmap = [];
0034   end
0035   return
0036 end
0037 % not a matrix, is it...
0038 % a colour name?
0039 tmp = strcmpi(acmapname, {'red','green','blue','cyan', 'magenta', ...
0040             'yellow', 'black', 'white'});
0041 coldefs = [1 0 0;
0042        0 1 0;
0043        0 0 1;
0044        0 1 1;
0045        1 0 1;
0046        1 1 0;
0047        0 0 0;
0048        1 1 1];
0049 
0050 if any(tmp)
0051   coldef = coldefs(tmp, :);
0052   if ~any(diff(coldef))
0053     cmap = coldef;
0054   else
0055     cmap = (0:63)' * coldef / 63;
0056   end
0057   return
0058 end
0059 % is it a file?
0060 oname = acmapname;
0061 [p f e] = fileparts(acmapname);
0062 % if no extension, add .mat
0063 if isempty(e) 
0064   e = '.mat';
0065   acmapname = fullfile(p, [f e]);
0066 end
0067 ef = exist(acmapname, 'file');
0068 % file doesn't exist? Try home directory of this mfile
0069 if ~ef
0070   mfp = fileparts(which(mfilename));
0071   acmapname = fullfile(mfp, [f e]);
0072   ef = exist(acmapname, 'file');
0073 end  
0074 if ~ef
0075   warnstr = ['No matrix or file ''' oname ''''];
0076   return
0077 end
0078 % found file, get cmap
0079 switch lower(e)
0080  case '.mat'
0081   % try for matrix of same name
0082   s = load(acmapname);
0083   if isfield(s, f)
0084     cmap = getfield(s, f);
0085   else % get first matrix in mat file
0086     s = struct2cell(s);
0087     cmap = s{1};
0088   end
0089   if size(cmap, 2)~=3
0090     warnstr = ['Colormap from ' acmapname ' was not an N by 3 matrix'];
0091     cmap = [];
0092   end
0093  case '.lut'
0094   fid = fopen(acmapname, 'rb');
0095   if fid~=-1
0096     cmap = fread(fid, Inf);
0097     l = length(cmap);
0098     if ~rem(l,3) 
0099       cmap = reshape(cmap, l/3, 3) / 255;
0100     else
0101       warnstr = ['LUT map ' acmapname ' was wrong size'];
0102     end
0103   else
0104     warnstr = ['Cannot open LUT colormap file ' acmapname];
0105   end
0106  otherwise
0107   warnstr = ['Unrecognized file extension ' e ' for ' acmapname];
0108 end
0109 return

Generated on Wed 06-Jul-2005 18:07:21 by m2html © 2003