Doxygen Source Code Documentation
MakeColorMap.m
Go to the documentation of this file.00001 function [err,M] = MakeColorMap (Fiducials,Ncols,Opt)
00002 %
00003 % [err,M] = MakeColorMap (Fiducials,Ncols,Opt)
00004 %
00005 %Purpose:
00006 % returns the RGB colormap containing Ncols that vary linearily
00007 % from the first color in Fiducials to the last.
00008 %
00009 %Input Parameters:
00010 % Fiducials : Nx3 matix specifying the RGB values (0-255) or
00011 % (0-1) depending on the value of Opt.Range. Those
00012 % fiducial colours will be equally spaced on the map
00013 % Ncols : Total number of colours in the map
00014 % You are somewhat restricted in the total number of
00015 % colours you choose. You must choose a number that
00016 % allows you to have the same number of colours between
00017 % each successive fiducials. Do not worry, the function
00018 % will suggest a good number closest to the one you chose.
00019 % Such a mighty function !
00020 % Opt is a structure containing the following fields
00021 % .Range (225 / 1) This specifies if RGB values are specified
00022 % in both Fiducials and M form 0-255 (integers)
00023 % or 0-1 floats.
00024 % .SkipLast (0/1) if set to 0, then the last color specified in
00025 % Fiducials is the last color in M. If set to 1, the last
00026 % color in M represents the color that would come right
00027 % before the last one in Fifucials. This last option is
00028 % usefull when you're crating cyclical color maps where
00029 % the last color in Fiduciasl is like the first.
00030 % .Showme (0/1) optional parameter to show a display of the map
00031 % .Write optional string. If supplied, M is written to the file
00032 % specified by .Write
00033 %
00034 %Output Parameters:
00035 % err : 0 No Problem
00036 % : 1 Mucho Problems
00037 % .verbose (0/1) optional verbose parameter, default is 0
00038 %More Info :
00039 % example
00040 % Fiducials = [255 0 0; 0 255 0; 0 0 255];
00041 % Opt.Range = 255; Opt.SkipLast = 1; Opt.Write = '';
00042 % [err,M] = MakeColorMap (Fiducials,6,Opt)
00043 % gives M =
00044 % 255 0 0
00045 % 128 128 0
00046 % 0 255 0
00047 % 0 128 128
00048 % 0 0 255
00049 % 128 0 128
00050 %
00051 %If you set Opt.SkipLast = 0, you'll get
00052 % [err,M] = MakeColorMap (Fiducials,7,Opt);
00053 %M =
00054 %
00055 % 255 0 0
00056 % 128 128 0
00057 % 0 255 0
00058 % 0 128 128
00059 % 0 0 255
00060 % 128 0 128
00061 % 255 0 0
00062 %
00063 % see also rgbdectohex, and ScaleToMap
00064 %
00065 %
00066 % Author : Ziad Saad
00067 % Date : Wed Apr 08 12:51:29 CDT 1998
00068
00069
00070 %Define the function name for easy referencing
00071 FuncName = 'MakeColorMap';
00072
00073 %initailize return variables
00074 err = 1;
00075 M = [];
00076
00077 if (~isfield(Opt,'Range') | ~isfield(Opt,'SkipLast')),
00078 fprintf(2, 'Error %s: Range or SkipLast fields missing.\n', FuncName);
00079 err = 1; return;
00080 end
00081
00082 if (~isfield(Opt,'verbose')),
00083 Opt.verbose = 0;
00084 end
00085
00086 if (~isfield(Opt,'Write')),
00087 Opt.Write = '';
00088 else
00089 if (filexist(Opt.Write)),
00090 err = ErrEval(FuncName,'Err_FileExist'); return
00091 end
00092 end
00093
00094 if (~isfield(Opt,'Showme')),
00095 Opt.Showme = 1;
00096 end
00097
00098 if (size(Fiducials,2) ~= 3),
00099 err = ErrEval(FuncName,'Err_BadOptSize'); return
00100 end
00101
00102 if (size(Fiducials,1) > Ncols),
00103 err = ErrEval(FuncName,'Err_More fiducials than colours needed.'); return
00104 end
00105
00106 %Check for some weird input
00107 tmp = find (Fiducials < 0);
00108 if (~isempty(tmp)),
00109 err= ErrEval(FuncName,'Err_No negative values allowed in Fiducials.'); return
00110 end
00111
00112 tmp = find (Fiducials > Opt.Range);
00113 if (~isempty(tmp)),
00114 serr = sprintf('Err_Values in Fiducials should not exceed Opt.Range (%g).',Opt.Range);
00115 err= ErrEval(FuncName,serr); return
00116 end
00117
00118
00119 %initialize
00120 M = -ones(Ncols,3);
00121 Nfid = size (Fiducials,1); %number of fiducial colours
00122
00123 if (~Opt.SkipLast),
00124 Ninter = Ncols - Nfid; %total number of intermediate colours
00125 else
00126 Ninter = Ncols - (Nfid -1);
00127 end
00128
00129 Ngap = Nfid - 1; %total number of gaps to fill
00130
00131 %You must have an equal number of colours in each gap
00132 Npergap = Ninter ./ Ngap;
00133
00134 if (Npergap ~= round(Npergap)),
00135 if (Opt.SkipLast) Ncolsgood = round(Npergap) .* Ngap + Nfid -1;
00136 else Ncolsgood = round(Npergap) .* Ngap + Nfid; end
00137 serr = sprintf('Err_The choice of Ncols does not work with the number\nof fiducial colours.\nTry Ncols = %g ',Ncolsgood);
00138 M = [];
00139 err = ErrEval(FuncName,serr); return;
00140 end
00141
00142 %Start forming M
00143 cnt = 0;
00144 im = 1;
00145 for (i=1:1:Ngap),
00146 if (Fiducials(i,1)~=Fiducials(i+1,1)),
00147 M1 = linspace(Fiducials(i,1),Fiducials(i+1,1),Npergap+2)';
00148 else
00149 M1 = Fiducials(i,1) .* ones(Npergap+2,1);
00150 end
00151
00152 if (Fiducials(i,2)~=Fiducials(i+1,2)),
00153 M2 = linspace(Fiducials(i,2),Fiducials(i+1,2),Npergap+2)';
00154 else
00155 M2 = Fiducials(i,2) .* ones(Npergap+2,1);
00156 end
00157
00158 if (Fiducials(i,3)~=Fiducials(i+1,3)),
00159 M3 = linspace(Fiducials(i,3),Fiducials(i+1,3),Npergap+2)';
00160 else
00161 M3 = Fiducials(i,3) .* ones(Npergap+2,1);
00162 end
00163
00164
00165 im2 = im+Npergap+1;
00166 if (i<Ngap | ~Opt.SkipLast),
00167 M(im:im2,:) = [M1 M2 M3];
00168 else
00169 M(im:im2-1,:) = [M1(1:Npergap+1) M2(1:Npergap+1) M3(1:Npergap+1)];
00170 end
00171 im = im2;
00172
00173 end
00174
00175
00176 %make sure format for output is good
00177 if (Opt.Range == 255), %needs to be integer output
00178 M = round(M);
00179 end
00180
00181 if (Opt.Showme),
00182 figure;
00183 if (Opt.Range == 255),
00184 Mrgb = M ./ 255;
00185 else
00186 Mrgb = M;
00187 end
00188 colormap (Mrgb);
00189 subplot 211;
00190 image ([1:1:length(Mrgb(:,1))]);
00191 subplot 212;
00192 pie (ones(1,length(Mrgb(:,1))));
00193 end
00194
00195 if (~isempty(Opt.Write)),
00196 wryte2(M,3,Opt.Write,'on');
00197 end
00198
00199 err = 0;
00200 return;
00201