⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mmsetc.m

📁 对于精通掌握matlab非常有用的资料
💻 M
📖 第 1 页 / 共 2 页
字号:
function C=mmsetc(H,S)%MMSETC Obtain an RGB Triple Interactively from a Color Sample.% MMSETC displays a dialog box for the user to select% a color interactively and displays the result.%% X = MMSETC returns the selected color in X.%% MMSETC([r g b]) uses the RGB triple as the initial% RGB value for modification.%% MMSETC C  -or-% MMSETC('C') where C is a color spec (y,m,c,r,g,b,w,k), uses % the specified color as the initial value.%% MMSETC(H) where the input argument H is the handle of% a valid graphics object that supports color, uses the color% property of the object as the initial RGB value.%% MMSETC select   -or- % MMSETC('select') waits for the user to click on a valid % graphics object that supports color, and uses the color% property of the object as the initial RGB value.%       % If the initial RGB value was obtained from an object or% object handle, the 'Done' pushbutton will apply the % resulting color property to the selected object.%% If no initial color is specified, black will be used.%% Examples:%        mmsetc%        mycolor=mmsetc%        mmsetc([.25 .62 .54])%        mmsetc(H)%        mmsetc g%        mmsetc red%        mmsetc select%        mycolor=mmsetc('select')%%   The second argument S is used internally to execute the callbacks.% B.R. Littlefield, University of Maine, Orono, ME 04469% 3/2/95% Copyright (c) 1996 by Prentice-Hall, Inc.%======================================================================% define some strings, and start error checking.%======================================================================global MMSETC_OUT MMSETC_DONEermsg1 = 'Too many input arguments.';ermsg2 = 'Input argument if used must be an RGB triple or valid handle.';if nargin > 2, error(ermsg1); end%======================================================================%  This section handles the 'no argument' case and sets the defaults.%======================================================================if nargin == 0   m = 0;   initrgb = [0 0 0];  Hx_obj = 0;end%======================================================================% Here the function was called with a single argument.  Check for % a valid RGB triple or an object handle and bail out if invalid.% Set the initial color value if H is valid.%======================================================================if nargin == 1   %--------------------------------------------------------------------   %  If H is 'select', get the color property of the selected object  %  and use it for the initial color.  %--------------------------------------------------------------------   if isstr(H)    m = 1;    Hx_obj = 0;    if strcmp(H,'select')      waitforbuttonpress;      Hx_obj = get(gcf,'CurrentObject');      initrgb = get(Hx_obj,'Color');      if isstr(initrgb)        initrgb = [0 0 0];      end    elseif strcmp(H(1:1),'r'), initrgb = [1 0 0];    elseif strcmp(H(1:1),'y'), initrgb = [1 1 0];    elseif strcmp(H(1:1),'g'), initrgb = [0 1 0];    elseif strcmp(H(1:1),'m'), initrgb = [0 1 1];    elseif strcmp(H(1:1),'b'), initrgb = [0 0 1];    elseif strcmp(H(1:1),'w'), initrgb = [1 1 1];    elseif strcmp(H(1:1),'k'), initrgb = [0 0 0];    else      error(ermsg2);     end    %--------------------------------------------------------------------   %  Otherwise H should be an RGB triple or an object handle.  %--------------------------------------------------------------------   else    [n m]=size(H);    if n ~= 1, error(ermsg2); end       %--------------------------------------------------------------------     %  If H is a valid RGB row vector, use it for the initial color.    %--------------------------------------------------------------------     if m == 3 & max(H) <= 1 & min(H) >= 0        Hx_obj = 0;      initrgb = H;     %--------------------------------------------------------------------     %  If H is a single number, it should be an object handle.    %  If the object has no 'color' property, get() will exit     %  with an error message.    %--------------------------------------------------------------------     elseif m == 1         Hx_obj = H;      initrgb = get(Hx_obj,'color');      if isstr(initrgb)        initrgb = [0 0 0];      end       %--------------------------------------------------------------------     %  Otherwise, H is invalid, so bail out.    %--------------------------------------------------------------------       else      error(ermsg2);    end  endend  %======================================================================% Do the initialization if this is a user call (zero or one arguments).%======================================================================if nargin < 2   MMSETC_OUT = initrgb;  MMSETC_DONE = 0;  %--------------------------------------------------------------------  % First, get a figure window, and set some properties.  %--------------------------------------------------------------------  scr = get(0,'screensize');  Hf_fig = figure('pos',[(scr(3)/2)-220 (scr(4)/2)-165 440 330],...      'color',[.75 .75 .75],...    'numbertitle','off',...    'name','Color Selector');  %--------------------------------------------------------------------  % Set some default properties for uicontrols in this figure.  %--------------------------------------------------------------------  set(Hf_fig,'DefaultUicontrolUnits','normalized',...    'DefaultUicontrolBackgroundColor',get(Hf_fig,'color'));  %--------------------------------------------------------------------  % Now that we have an initial RGB value, get the initial HSV value  % so we can initialize the sliders.  %--------------------------------------------------------------------  inithsv = rgb2hsv(initrgb);  %--------------------------------------------------------------------  % Define the 'initial' and 'new' color frames, and label them.  %--------------------------------------------------------------------  Hc_ifr = uicontrol(Hf_fig,'style','frame',...    'pos',[.25 .70 .25 .20],...    'backgroundcolor',initrgb);   Hc_nfr = uicontrol(Hf_fig,'style','frame',...    'pos',[.50 .70 .25 .20],...    'backgroundcolor',initrgb);   Hc_ilabel = uicontrol(Hf_fig,'style','text',...    'pos',[.25 .91 .25 .05],...    'string','Initial Color');  Hc_nlabel = uicontrol(Hf_fig,'style','text',...    'pos',[.50 .91 .25 .05],...    'string','New Color');  %--------------------------------------------------------------------  % Get the color values, and display them under the frames.  %--------------------------------------------------------------------  Hc_icur = uicontrol(Hf_fig,'style','text',...    'pos',[.25 .64 .25 .05],...    'string',sprintf('[%.2f %.2f %.2f]',get(Hc_ifr,'backgroundcolor')));  Hc_ncur = uicontrol(Hf_fig,'style','text',...    'pos',[.50 .64 .25 .05],...    'string',sprintf('[%.2f %.2f %.2f]',get(Hc_nfr,'backgroundcolor')));  %--------------------------------------------------------------------  % Define the sliders with labels and current value displays.  %--------------------------------------------------------------------  Hc_rsli = uicontrol(Hf_fig,'style','slider',...    'pos',[.10 .55 .35 .05],...    'min',0,'max',1,'val',initrgb(1),...    'callback','mmsetc(0,''rgb2new'')');      Hc_rcur = uicontrol(Hf_fig,'style','text',...    'pos',[.01 .55 .08 .05],...    'string',sprintf('%.2f',get(Hc_rsli,'val')));  Hc_rlabel = uicontrol(Hf_fig,'style','text',...    'pos',[.10 .49 .35 .05],...    'string','Red');  Hc_gsli = uicontrol(Hf_fig,'style','slider',...    'pos',[.10 .43 .35 .05],...    'min',0,'max',1,'val',initrgb(2),...    'callback','mmsetc(0,''rgb2new'')');      Hc_gcur = uicontrol(Hf_fig,'style','text',...    'pos',[.01 .43 .08 .05],...    'string',sprintf('%.2f',get(Hc_gsli,'val')));  Hc_glabel = uicontrol(Hf_fig,'style','text',...    'pos',[.10 .37 .35 .05],...    'string','Green');  Hc_bsli = uicontrol(Hf_fig,'style','slider',...    'pos',[.10 .30 .35 .05],...    'min',0,'max',1,'val',initrgb(3),...    'callback','mmsetc(0,''rgb2new'')');      Hc_bcur = uicontrol(Hf_fig,'style','text',...    'pos',[.01 .30 .08 .05],...    'string',sprintf('%.2f',get(Hc_bsli,'val')));  Hc_blabel = uicontrol(Hf_fig,'style','text',...    'pos',[.10 .24 .35 .05],...    'string','Blue');  Hc_hsli = uicontrol(Hf_fig,'style','slider',...    'pos',[.55 .55 .35 .05],...    'min',0,'max',1,...    'val',inithsv(1),...    'callback','mmsetc(0,''hsv2new'')');      Hc_hcur = uicontrol(Hf_fig,'style','text',...    'pos',[.91 .55 .08 .05],...    'string',sprintf('%.2f',get(Hc_hsli,'val')));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -