function click_rgbk(arg1, varargin)
% click_rgbk(string_cellarray, varargin)
% like 'gtext', but with more control.
% (1) no arguments: default is to label 4 beams with 4 colors (r,g,b,k)
% usage: 
%  click_rgbk               %for defaults
% (2) arg1 is a handle to a text object: varargin sets properties
%  click_rgbk(handle, varargin)
% (3) arg1 is a string or a cellarray of strings, click for each text item,
%             varargin sets properties
%  click_rgbk({'text1', 'text2'}, varargin)
%  click_rgbk('string1', varargin)
%
%% varargin is                       what                       default
%
%  's'       fontsize                integer                     10
%  'c'       color array           string of colors            ['rgbk']
%                                  or Nx3 color palette        [1 0 0
%                                                               0 1 0
%                                                               0 0 1
%                                                               0 0 0]
%                                                          repeats if necessary
%                                                              
%  'ha',     horizont alalignment   'l', 'c', 'r'              'l'
%                                 (left, center, right)
%  'va',     vertical alignment   't', 'm', 'b'                 'm'
%                                 (top middle bottom)
%  'r'       rotation                 degrees                    0
%  'w'      fontweight            'l', 'n', 'b'                 'b'
%                               (light, normal, bold)
%  'addline'     add a line for each string  [0, 1]
%                                requires 3 clicks:              0 
%                                 start, end, text


%JH 2003/02/20  should override jf.m
   
 %set default maketext
maketext = 1;
if nargin == 0
   str_array = {'beam1', 'beam2', 'beam3', 'beam4'};
elseif isstr(arg1)
   str_array = {arg1};
elseif ishandle(arg1)
   hh = arg1;
   str_array = 1;
   maketext = 0;  %override if necessary
elseif iscell(arg1)
   str_array = arg1;
else
   fprintf('arg1 must be a string or cellarray of strings, or a handle to text object\n')
   return
end


cfg.s  = 10;
cfg.c = ['rgbk'];
cfg.ha = 'l';
cfg.va = 'm';
cfg.r  = 0;
cfg.w  = 'b';
cfg.addline = 0;


cfg = fillstruct(cfg, varargin, 0);

%make numerical equivalent of colors so we can pass those in
if isstr(cfg.c)
   colors = zeros(length(cfg.c),3);
   allcl = 'bgrcmyk';
   allcc = [   0     0     1
               0     1     0
               1     0     0
               0     1     1
               1     0     1
               1     1     0
               0     0     0];
   for ii=1:length(cfg.c)
      colors(ii,:) = allcc(find(cfg.c(ii)==allcl),:);
   end
else
   colors = cfg.c;
   if length(cfg.c(1,:)) ~=3;
      fprintf('colors must be a string of letters or colormap (Nx3)\n')
      return
   end
end



% horizontal alignment
if cfg.ha(1) == 'c'
   cfg.ha = 'center' ;
elseif cfg.ha(1) == 'r'    
   cfg.ha = 'right';
else
   cfg.ha = 'left';  %default
end

% vertical alignment
if cfg.va(1) == 't'
   cfg.va = 'top';
elseif cfg.ha(1) == 'b'
   cfg.va = 'bottom'; 
else
   cfg.va = 'middle'; %default
end

% font weight
if cfg.w(1) == 'l'
   cfg.w = 'light';
elseif cfg.w(1) == 'n'
   cfg.w = 'normal';
else
   cfg.w = 'bold'; %default
end

%%%%%%%%%%%%%%%%%

% DO IT

for ii=1:length(str_array)
   hold on
   ci = rem(ii-1,length(colors(:,1))) + 1;

   if (maketext)
      str = str_array{ii};

      if (cfg.addline)
         fprintf('click for start and end of line to label:%s\n', str);
         [x,y]=ginput(2);
         hh=plot(x, y); set(hh, 'color', colors(ci,:))
      end
      
      fprintf('click for the location of the string %s\n', str);
      hh=gtext(str);
   end
   
   % index into color
   set(hh,'rotation',cfg.r)
   set(hh,'fontsize',cfg.s);
   set(hh,'color',colors(ci,:));
   set(hh,'horizontalalignment',cfg.ha);
   set(hh,'verticalalignment',cfg.va);
   set(hh,'fontweight',cfg.w);
end
