function h = ind_cbar(trange, irange, hor_vert, titlestr, shrink, varargin)
%  function h = ind_cbar(trange, irange, hor_vert, titlestr, shrink, ...)
%
%  Makes an INDexed ColorBAR for used with indexed colors;
%  see c_index.m
%
%     h is the handle of the new colorbar object
%     trange, irange are the data and index ranges from c_index.
%     hor_vert is orientation: default (i.e. missing) is vertical
%        options are 'horiz' and 'vert'
%     titlestr: a label for the X or Y axis, as appropriate
%     shrink: [sw sl], where sw is the thickness of the colorbar
%        as a fraction of what the colorbar command produces,
%        and sl is the length, also as a fraction of the original.
%        Default is [0.25, 0.7].  Default is set if shrink = [].
%        Alternative: [left bottom width height]; if shrink
%        is a 4-element vector, it will be interpreted as
%        an axes object 'Position' specification.  Units are
%        still the Matlab default, normalized.
%     any arguments following shrink will be treated as
%        axes property-value pairs, and will be passed to
%        the colorbar axes object without checking.  Note that
%        to use this option, you must specify something for each
%        of the previous arguments.  titlestr can be '' and
%        shrink can be [] if you don't want them to do anything.
%
%

%  Eric Firing

% 98/07/03 EF: changed to not use colorbar.m.  Deleting the colorbar
%     was changing the main axes back, so that the colorbar was
%     sometimes being overlaid by the main axes.
%

% defaults ------------------------------
if nargin < 5,
   shrink = [];
end
if isempty(shrink), shrink = [0.25 0.7]; end
if nargin < 4,
   titlestr = '';
end
if nargin < 3,
   hor_vert = 'vert';
end
if nargin < 2,
   irange = [1 64];
end

% Argument checking ------------------
if (nargin<1)
        help ind_cbar
        error('At least one argument is needed.')
end


if (~strcmp(hor_vert,'vert')&~strcmp(hor_vert,'horiz'))
   help ind_cbar
   error(['orientation string given as ' hor_vert ...
         ' must be either ''horiz'' or ''vert'' or at least start with h or v '])
end
% No checking of fourth argument: title string.
if length(shrink) ~= 2 & length(shrink) ~= 4,
   help ind_cbar
   error('shrink (5th argument) must have 0, 2, or 4 elements.')
end



ha_main = gca;

%--------
if hor_vert(1) == 'v', vert = 1; else vert = 0; end
pos = get(ha_main, 'Position');
units = get(ha_main, 'units');
set(ha_main, 'Units', 'normalized');

if vert == 1,
   stripe = 0.075; edge = 0.02; space = 0.05;
   new_pos = [pos(1) pos(2) pos(3)*(1-stripe-edge-space) pos(4)];
   pos_cbar = [pos(1)+(1-stripe-edge)*pos(3) pos(2) stripe*pos(3) pos(4)];
   % Above is Matlab positioning by colorbar.m; now adjust.
   pos_cbar = [pos_cbar(1), pos_cbar(2)+ 0.5*(1-shrink(2))*pos_cbar(4), ...
               shrink(1)*pos_cbar(3), shrink(2)*pos_cbar(4)];
else
   stripe = 0.075; edge = 0.0; space = 0.1;
   new_pos = [pos(1) pos(2)+(stripe+space)*pos(4) pos(3) (1-stripe-space)*pos(4)];
   pos_cbar = [pos(1) pos(2) pos(3) stripe*pos(4)];
   % Again, adjust.
   pos_cbar = [pos_cbar(1)+  0.5*(1-shrink(2))*pos_cbar(3), ...
               pos_cbar(2)+ (1-shrink(1))*pos_cbar(4), ...
               shrink(2)*pos_cbar(3), shrink(1)*pos_cbar(4)];
end

%---------

set(ha_main, 'Position', new_pos);
set(ha_main, 'Units', units);

ha_cbar = axes('Position', pos_cbar);
set(ha_cbar, 'Tag', 'ind_cbar');

dT = diff(trange);
dI = diff(irange);

i_cm1 = irange(1):(irange(2)+1);
nC = length(i_cm1);
C = [i_cm1; i_cm1];
Y = [0;1];
X = trange(1) + (i_cm1 - irange(1)) * dT/dI;

if vert == 1,
   C = C.';
   xx = X;
   X = Y.';
   Y = xx.';
   axis([0 1 trange]);
else
   axis([trange 0 1]);
end

hs = surface(X,Y,C);
set(hs,'CDataMapping','direct','EdgeColor','none');

set(ha_cbar,'TickDir', 'out')

if vert == 1,
   set(ha_cbar, 'XTick',[], 'YAxisLocation','right');
   ylabel(titlestr);
else
   set(ha_cbar, 'YTick',[]);
   xlabel(titlestr);
end

% Axes properties set via remaining input args:
if ~isempty(varargin)
   set(ha_cbar, varargin{:});
end

axes(ha_main);

h = ha_cbar;     % output argument

