function EditColor(arg, command, option)

if nargin==0,arg='rgb';,end

global LASTOBJECT LASTTYPE

selectedObj = LASTOBJECT;
if isempty(selectedObj),return,end	% exit if no selected object
type = LASTTYPE;

if arg==0
  
  if(command == 3)
   if(strcmp(type,'line') | strcmp(type,'text'))
      set(selectedObj,'Color',option);
   elseif(strcmp(type,'patch'))
      set(selectedObj,'EdgeColor',option);
   elseif(strcmp(type,'axes'))
      set(selectedObj,'Xcolor',option);
      set(selectedObj,'Ycolor',option);
      set(selectedObj,'Zcolor',option);
   end
 end

elseif strcmp(arg,'rgb')

   % Allow only one RGB_EDIT at a time
   %
   RGB_Edit=findobj(0,'Type','figure','Tag','RGB_Edit');
   if ~isempty(RGB_Edit),return,end
   
   rgbfig = figure('Position',[150 200 220 300],...
                   'NumberTitle','off',...
                   'Name','Edit RGB',...
                   'NextPlot','new',...
                   'Tag','RGB_Edit',...
                   'Units','normalized');
   wh_ratio=320/400;

   htR1=uicontrol('Parent',rgbfig,...
                 'Units','normalized',...
                 'Position',[.05 .5 .11 .08],...
                 'Style','edit',...
                 'String','126',...
                 'BackGroundColor',[1 0 0],...
                 'ForeGroundColor',[0 0 0],...
                 'Tag','RedVal1');
   Rslider=uicontrol('Parent',rgbfig,...
                     'Style','slider',...
                     'Units','normalized',...
                     'Position',[.2 .5 .6 .08],...
                     'Tag','Red_Slider',...
                     'Min',0,'Max',255,'Value',126,...
                     'BackGroundColor',[1 0 0],...
                     'ForeGroundColor',[0 0 0],...
                     'CallBack','EditColor(2)');
   htR2=uicontrol('Parent',rgbfig,...
                 'Units','normalized',...
                 'Position',[.84 .5 .11 .08],...
                 'Style','text',...
                 'String','.5',...
                 'BackGroundColor',[1 0 0],...
                 'ForeGroundColor',[0 0 0],...
                 'Tag','RedVal2');
                 
   htG1=uicontrol('Parent',rgbfig,...
                 'Units','normalized',...
                 'Position',[.05 .395 .11 .08],...
                 'Style','text',...
                 'String','126',...
                 'BackGroundColor',[0 1 0],...
                 'ForeGroundColor',[0 0 0],...
                 'Tag','GreenVal1');
   GRslider=uicontrol('Parent',rgbfig,...
                     'Style','slider',...
                     'Units','normalized',...
                     'Position',[.2 .395 .6 .08],...
                     'Min',0,'Max',255,'Value',126,...
                     'Tag','Green_Slider',...
                     'BackGroundColor',[0 1 0],...
                     'ForeGroundColor',[0 0 0],...
                     'CallBack','EditColor(3)');
   htG2=uicontrol('Parent',rgbfig,...
                 'Units','normalized',...
                 'Position',[.84 .395 .11 .08],...
                 'Style','text',...
                 'String','.5',...
                 'BackGroundColor',[0 1 0],...
                 'ForeGroundColor',[0 0 0],...
                 'Tag','GreenVal2');
                 
   htB1=uicontrol('Parent',rgbfig,...
                 'Units','normalized',...
                 'Position',[.05 .28 .11 .08],...
                 'Style','text',...
                 'String','126',...
                 'BackGroundColor',[0 0 1],...
                 'ForeGroundColor',[1 1 1],...
                 'Tag','BlueVal1');
   Bslider=uicontrol('Parent',rgbfig,...
                     'Style','slider',...
                     'Units','normalized',...
                     'Position',[.2 .28 .6 .08],...
                     'Min',0,'Max',255,'Value',126,...
                     'Tag','Blue_Slider',...
                     'BackGroundColor',[0 0 1],...
                     'ForeGroundColor',[1 1 1],...
                     'CallBack','EditColor(4)');
   htB2=uicontrol('Parent',rgbfig,...
                 'Units','normalized',...
                 'Position',[.84 .28 .11 .08],...
                 'Style','text',...
                 'String','.5',...
                 'BackGroundColor',[0 0 1],...
                 'ForeGroundColor',[1 1 1],...
                 'Tag','BlueVal2');
                 
   FPatchR=uicontrol('Parent',rgbfig,...
                    'Units','normalized',...
                    'Position',[.10 .75 .1/wh_ratio .1],...
                    'Style','text',...
                    'BackGroundColor',[.5 0 0],...
                    'Tag','FPatchR');
   uicontrol('Parent',rgbfig,...
                 'Units','normalized',...
                 'Position',[.21 .75 .1/wh_ratio .1],...
                 'Style','text',...
                 'String','+',...
                 'BackGroundColor',[0 0 0],...
                 'ForeGroundColor',[1 1 1]);
   FPatchG=uicontrol('Parent',rgbfig,...
                    'Units','normalized',...
                    'Position',[.32 .75 .1/wh_ratio .1],...
                    'Style','text',...
                    'BackGroundColor',[0 .5 0],...
                    'Tag','FPatchG');
   uicontrol('Parent',rgbfig,...
                 'Units','normalized',...
                 'Position',[.43 .75 .1/wh_ratio .1],...
                 'Style','text',...
                 'String','+',...
                 'BackGroundColor',[0 0 0],...
                 'ForeGroundColor',[1 1 1]);
   FPatchB=uicontrol('Parent',rgbfig,...
                    'Units','normalized',...
                    'Position',[.54 .75 .1/wh_ratio .1],...
                    'Style','text',...
                    'BackGroundColor',[0 0 .5],...
                    'Tag','FPatchB');
                    
   uicontrol('Parent',rgbfig,...
                 'Units','normalized',...
                 'Position',[.65 .75 .1/wh_ratio .1],...
                 'Style','text',...
                 'String','=',...
                 'BackGroundColor',[0 0 0],...
                 'ForeGroundColor',[1 1 1]);
   FPatchEq=uicontrol('Parent',rgbfig,...
                    'Units','normalized',...
                    'Position',[.77 .75 .1/wh_ratio .1],...
                    'Style','text',...
                    'BackGroundColor',[.5 .5 .5],...
                    'Tag','FPatchEq');
%                    'Position',[.15 .7 .7 .2],...
                 
   
   GetColorbutton = uicontrol('Parent',rgbfig,...
                           'Style','pushbutton',...
                           'String','Get Color',...
                           'Units','normalized',...
                           'Position',[.05 .05 .25 .1],...
                           'Callback','EditColor(6)',...
                           'BackGroundColor','g',...
                           'ForeGroundColor','k',...
                            'Tag','GetColorbutton');
   Applybutton = uicontrol('Parent',rgbfig,...
                           'Style','Checkbox',...
                           'String','Apply',...
                           'Units','normalized',...
                           'Position',[.375 .05 .25 .1],...
                           'Callback','EditColor(7)',...
                           'BackGroundColor','g',...
                           'ForeGroundColor','k',...
                            'Tag','Applybutton');

   Cancelbutton = uicontrol('Parent',rgbfig,...
                            'Style','pushbutton',...
                            'String','Cancel',...
                            'Units','normalized',...
                            'Position',[.7 .05 .25 .1],...
                            'Callback','EditColor(9)',...
                            'BackGroundColor','r',...
                            'ForeGroundColor','k',...
                            'Tag','Cancelbutton');

elseif arg==2   %  Affect Red Color

   fig=findobj(0,'Type','figure','Tag','RGB_Edit');
   Red_Slider=findobj(fig,'Type','uicontrol','Tag','Red_Slider');
   RVal1=findobj(fig,'Type','uicontrol','Tag','RedVal1');
   RVal2=findobj(fig,'Type','uicontrol','Tag','RedVal2');
   FPatchR=findobj(fig,'Type','uicontrol','Tag','FPatchR');
   FPatchEq=findobj(fig,'Type','uicontrol','Tag','FPatchEq');
   newval=get(Red_Slider,'Value');
   set(RVal1,'String',int2str(newval));
   
   if newval==255
      s='01.';
   elseif newval==0
      s='00.';
   else
      s=num2str(newval/255,3);
   end
   set(RVal2,'String',s(2:length(s)));
   oldcolor=get(FPatchEq,'BackGroundColor');
   set(FPatchR,'BackGroundColor',[newval/255 0 0]);
   set(FPatchEq,'BackGroundColor',[newval/255 oldcolor(2) oldcolor(3)]);

   ApButton=findobj(fig,'Type','uicontrol','Tag','Applybutton');   
   if get(ApButton,'Value')==1,EditColor(8),end

elseif arg==3    %  Affect Green Color

   fig=findobj(0,'Type','figure','Tag','RGB_Edit');
   Green_Slider=findobj(fig,'Type','uicontrol','Tag','Green_Slider');
   GVal1=findobj(fig,'Type','uicontrol','Tag','GreenVal1');
   GVal2=findobj(fig,'Type','uicontrol','Tag','GreenVal2');
   FPatchEq=findobj(fig,'Type','uicontrol','Tag','FPatchEq');
   FPatchG=findobj(fig,'Type','uicontrol','Tag','FPatchG');
   newval=get(Green_Slider,'Value');
   set(GVal1,'String',int2str(newval));

   if newval==255
      s='01.';
   elseif newval==0
      s='00.';
   else
      s=num2str(newval/255,3);
   end
   set(GVal2,'String',s(2:length(s)));
   oldcolor=get(FPatchEq,'BackGroundColor');
   set(FPatchG,'BackGroundColor',[0 newval/255 0]);
   set(FPatchEq,'BackGroundColor',[oldcolor(1) newval/255 oldcolor(3)]);

   ApButton=findobj(fig,'Type','uicontrol','Tag','Applybutton');   
   if get(ApButton,'Value')==1,EditColor(8),end
   
elseif arg==4    %  Affect Blue Color

   fig=findobj(0,'Type','figure','Tag','RGB_Edit');
   Blue_Slider=findobj(fig,'Type','uicontrol','Tag','Blue_Slider');
   BVal1=findobj(fig,'Type','uicontrol','Tag','BlueVal1');
   BVal2=findobj(fig,'Type','uicontrol','Tag','BlueVal2');
   FPatchEq=findobj(fig,'Type','uicontrol','Tag','FPatchEq');
   FPatchB=findobj(fig,'Type','uicontrol','Tag','FPatchB');
   newval=get(Blue_Slider,'Value');
   set(BVal1,'String',int2str(newval));
   
   if newval==255
      s='01.';
   elseif newval==0
      s='00.';
   else
      s=num2str(newval/255,3);
   end
   set(BVal2,'String',s(2:length(s)));
   oldcolor=get(FPatchEq,'BackGroundColor');
   set(FPatchB,'BackGroundColor',[0 0 newval/255]);
   set(FPatchEq,'BackGroundColor',[oldcolor(1) oldcolor(2) newval/255]);
   
   ApButton=findobj(fig,'Type','uicontrol','Tag','Applybutton');   
   if get(ApButton,'Value')==1,EditColor(8),end
   
elseif arg==6    %  Get Color from object
                 %  Must not be a GUI, or a child of the RGB_Edit figure

   waitforbuttonpress;
   gtype=get(gco,'Type');
   if strcmp(gtype,'uicontrol')|strcmp(gtype,'uimenu'),return,end
   fig=findobj(0,'Type','figure','Tag','RGB_Edit');
   par=get(gco,'parent');
   if par==fig|gco==fig,return,end
   
   if strcmp(gtype,'patch')
      color=get(gco,'FaceColor');
   else
      color=get(gco,'Color');
   end
   
   if strcmp(color,'none'),return,end
   
   Rt1=findobj(fig,'Type','uicontrol','Tag','RedVal1');
   Gt1=findobj(fig,'Type','uicontrol','Tag','GreenVal1');
   Bt1=findobj(fig,'Type','uicontrol','Tag','BlueVal1');
   Rt2=findobj(fig,'Type','uicontrol','Tag','RedVal2');
   Gt2=findobj(fig,'Type','uicontrol','Tag','GreenVal2');
   Bt2=findobj(fig,'Type','uicontrol','Tag','BlueVal2');
   Red_Slider=findobj(fig,'Type','uicontrol','Tag','Red_Slider');
   Green_Slider=findobj(fig,'Type','uicontrol','Tag','Green_Slider');
   Blue_Slider=findobj(fig,'Type','uicontrol','Tag','Blue_Slider');
   FPatchR=findobj(fig,'Type','uicontrol','Tag','FPatchR');
   FPatchG=findobj(fig,'Type','uicontrol','Tag','FPatchG');
   FPatchB=findobj(fig,'Type','uicontrol','Tag','FPatchB');
   FPatchEq=findobj(fig,'Type','uicontrol','Tag','FPatchEq');
   
   Rval=floor(color(1)*255.);
   Gval=floor(color(2)*255.);
   Bval=floor(color(3)*255.);
   set(Red_Slider,'Value',Rval);
   set(Green_Slider,'Value',Gval);
   set(Blue_Slider,'Value',Bval);
   set(Rt1,'String',int2str(Rval));
   set(Gt1,'String',int2str(Gval));
   set(Bt1,'String',int2str(Bval));
   
   if color(1)==1
      s1='01.';
   elseif color(1)==0
      s1='00.';
   else
      s1=num2str(color(1),3);
   end
   set(Rt2,'String',s1(2:length(s1)));
   
   if color(2)==1
      s2='01.';
   elseif color(2)==0
      s2='00.';
   else
      s2=num2str(color(2),3);
   end
   set(Gt2,'String',s2(2:length(s2)));
   
   if color(3)==1
      s3='01.';
   elseif color(3)==0
      s3='00.';
   else
      s3=num2str(color(3),3);
   end
   set(Bt2,'String',s3(2:length(s3)));
   
   set(FPatchR,'BackGroundColor',[color(1) 0 0]);
   set(FPatchG,'BackGroundColor',[0 color(2) 0]);
   set(FPatchB,'BackGroundColor',[0 0 color(3)]);
   set(FPatchEq,'BackGroundColor',color);
   

elseif arg==7    %  Apply Color to chosen object 
                 %  Must not be a GUI, or a child of the RGB_Edit figure

   fig=findobj(0,'Type','figure','Tag','RGB_Edit');
   ApButton=findobj(fig,'Type','uicontrol','Tag','Applybutton');
   if get(ApButton,'Value')==0,return,end
   
   waitforbuttonpress;
   
   gtype=get(gco,'Type');
   if strcmp(gtype,'uicontrol')|strcmp(gtype,'uimenu'),return,end
   par=get(gco,'parent');
   if par==fig|gco==fig,return,end
   
   Rt1=findobj(fig,'Type','uicontrol','Tag','RedVal1');
   Gt1=findobj(fig,'Type','uicontrol','Tag','GreenVal1');
   Bt1=findobj(fig,'Type','uicontrol','Tag','BlueVal1');
   R=str2num(get(Rt1,'String'));
   G=str2num(get(Gt1,'String'));
   B=str2num(get(Bt1,'String'));
   newcolor=[R G B]/255;
      
   if strcmp(gtype,'patch')
      set(gco,'FaceColor',newcolor);
   else
      set(gco,'Color',newcolor);
   end
   set(ApButton,'UserData',gco)

elseif arg==8
   fig=findobj(0,'Type','figure','Tag','RGB_Edit');
   ApButton=findobj(fig,'Type','uicontrol','Tag','Applybutton');
   cobj=get(ApButton,'UserData');
   if isempty(cobj),return,end
   gtype=get(cobj,'Type');
   
   Rt1=findobj(fig,'Type','uicontrol','Tag','RedVal1');
   Gt1=findobj(fig,'Type','uicontrol','Tag','GreenVal1');
   Bt1=findobj(fig,'Type','uicontrol','Tag','BlueVal1');
   R=str2num(get(Rt1,'String'));
   G=str2num(get(Gt1,'String'));
   B=str2num(get(Bt1,'String'));
   newcolor=[R G B]/255;
      
   if strcmp(gtype,'patch')
      set(cobj,'FaceColor',newcolor);
   else
      set(cobj,'Color',newcolor);
   end
   
   
elseif arg==9
   fig=findobj(0,'Type','figure','Tag','RGB_Edit');
   delete(fig);
end
