📄 mmsetc.m
字号:
Hc_hlabel = uicontrol(Hf_fig,'style','text',... 'pos',[.55 .49 .35 .05],... 'string','Hue'); Hc_ssli = uicontrol(Hf_fig,'style','slider',... 'pos',[.55 .43 .35 .05],... 'min',0,'max',1,... 'val',inithsv(2),... 'callback','mmsetc(0,''hsv2new'')'); Hc_scur = uicontrol(Hf_fig,'style','text',... 'pos',[.91 .43 .08 .05],... 'string',sprintf('%.2f',get(Hc_ssli,'val'))); Hc_slabel = uicontrol(Hf_fig,'style','text',... 'pos',[.55 .37 .35 .05],... 'string','Saturation'); Hc_vsli = uicontrol(Hf_fig,'style','slider',... 'pos',[.55 .30 .35 .05],... 'min',0,'max',1,... 'val',inithsv(3),... 'callback','mmsetc(0,''hsv2new'')'); Hc_vcur = uicontrol(Hf_fig,'style','text',... 'pos',[.91 .30 .08 .05],... 'string',sprintf('%.2f',get(Hc_vsli,'val'))); Hc_vlabel = uicontrol(Hf_fig,'style','text',... 'pos',[.55 .24 .35 .05],... 'string','Value'); %-------------------------------------------------------------------- % Define the 'initial' color selector popup, and label it. %-------------------------------------------------------------------- Hc_pcolor = uicontrol(Hf_fig,'style','popupmenu',... 'pos',[.05 .05 .20 .10],... 'backgroundcolor',[.7 .7 .8],... 'string','Black|Red|Yellow|Green|Cyan|Blue|Magenta|White|Initial',... 'val',9,... 'userdata',[[0 0 0];... [1 0 0];... [1 1 0];... [0 1 0];... [0 1 1];... [0 0 1];... [1 0 1];... [1 1 1];... [0 0 0]],... 'callback','mmsetc(0,''setinit'')'); Hc_pclabel = uicontrol(Hf_fig,'style','text',... 'pos',[.05 .15 .20 .05],... 'string','Initial Color'); %-------------------------------------------------------------------- % Define the buttons for 'Copy', 'Cancel', and 'Done'. %-------------------------------------------------------------------- Hc_copypb = uicontrol(Hf_fig,'style','push',... 'pos',[.55 .05 .12 .10],... 'backgroundcolor',[.7 .7 .8],... 'string','Copy',... 'callback','mmsetc(0,''copy'')'); Hc_cancelpb = uicontrol(Hf_fig,'style','push',... 'pos',[.70 .05 .12 .10],... 'backgroundcolor',[.7 .7 .8],... 'string','Cancel',... 'callback','mmsetc(0,''cancel'')'); Hc_donepb = uicontrol(Hf_fig,'style','push',... 'pos',[.85 .05 .12 .10],... 'backgroundcolor',[.7 .7 .8],... 'string','Done',... 'callback','mmsetc(0,''done'')'); %-------------------------------------------------------------------- % If an initial color was supplied, insert it into the popup data. %-------------------------------------------------------------------- if m > 0 tempdata = get(Hc_pcolor,'userdata'); tempdata(get(Hc_pcolor,'val'),:) = initrgb; set(Hc_pcolor,'userdata',tempdata) end %-------------------------------------------------------------------- % In order to pass the uicontrol handles to the callback section, % create a vector of object handles, and store it in the figure's % 'userdata' property. %-------------------------------------------------------------------- Z = [Hx_obj, Hc_ifr, Hc_nfr, Hc_icur, Hc_ncur, Hc_rsli, Hc_rcur,... Hc_gsli, Hc_gcur, Hc_bsli, Hc_bcur, Hc_hsli, Hc_hcur, Hc_ssli,... Hc_scur, Hc_vsli, Hc_vcur, Hc_pcolor, Hf_fig]; set(gcf,'userdata',Z); %-------------------------------------------------------------------- % Now we wait for the user to select a color and return the new value. % The 'drawnow' command flushes the event queue on most platforms. % The PC is an exception and requires 'waitforbuttonpress' instead. % Status values for MMSETC_DONE: 2=cancel; 1=done; 0=wait. %-------------------------------------------------------------------- arch = computer; PC = strcmp(arch(1:2),'PC'); while MMSETC_DONE == 0 if PC, waitforbuttonpress; else, drawnow; end end if MMSETC_DONE == 1 if nargout == 1 C = MMSETC_OUT; else disp(sprintf('Selected color: [%.4f %.4f %.4f]',MMSETC_OUT)); end end close returnend%======================================================================% This section handles the callbacks. Do some error checking,% extract the object handles, and execute the callbacks.%======================================================================if nargin == 2 if ~isstr(S), error(ermsg1); end %-------------------------------------------------------------------- % Since the uicontrol handles were stored in the figure 'userdata' % property, extract them for use in the callback routines. %-------------------------------------------------------------------- Z = get(gcf,'userdata'); Hx_obj = Z(1); Hc_ifr = Z(2); Hc_nfr = Z(3); Hc_icur = Z(4); Hc_ncur = Z(5); Hc_rsli = Z(6); Hc_rcur = Z(7); Hc_gsli = Z(8); Hc_gcur = Z(9); Hc_bsli = Z(10); Hc_bcur = Z(11); Hc_hsli = Z(12); Hc_hcur = Z(13); Hc_ssli = Z(14); Hc_scur = Z(15); Hc_vsli = Z(16); Hc_vcur = Z(17); Hc_pcolor = Z(18); Hf_fig = Z(19); %-------------------------------------------------------------------- % If 'cancel' was pressed, set the MMSETC_DONE flag to bail out. %-------------------------------------------------------------------- if strcmp(S,'cancel') MMSETC_DONE = 2; %-------------------------------------------------------------------- % If 'done' was pressed, save the new RGB triple and set the % MMSETC_DONE flag. If the input arg was a valid object handle, % change the color of the object. %-------------------------------------------------------------------- elseif strcmp(S,'done') MMSETC_OUT = get(Hc_nfr,'backgroundcolor'); MMSETC_DONE = 1; if Hx_obj > 0 set(eval('Hx_obj'),'color',get(Hc_nfr,'backgroundcolor')) end %-------------------------------------------------------------------- % If 'copy' was pressed, copy the color from the 'initial' frame % to the 'new' frame, and update the sliders and color values. %-------------------------------------------------------------------- elseif strcmp(S,'copy') set(Hc_nfr,'backgroundcolor',get(Hc_ifr,'backgroundcolor')) mmsetc(0,'setsli'); %-------------------------------------------------------------------- % This routine sets the sliders and color values from the 'new' frame. %-------------------------------------------------------------------- elseif strcmp(S,'setsli') rb=(get(Hc_nfr,'backgroundcolor')); set(Hc_ncur,'string',sprintf('[%.2f %.2f %.2f]',rb)) set(Hc_rsli,'val',rb(1)) set(Hc_rcur,'string',sprintf('%.2f',rb(1))) set(Hc_gsli,'val',rb(2)) set(Hc_gcur,'string',sprintf('%.2f',rb(2))) set(Hc_bsli,'val',rb(3)) set(Hc_bcur,'string',sprintf('%.2f',rb(3))) hv=rgb2hsv(rb); set(Hc_hsli,'val',hv(1)) set(Hc_hcur,'string',sprintf('%.2f',hv(1))) set(Hc_ssli,'val',hv(2)) set(Hc_scur,'string',sprintf('%.2f',hv(2))) set(Hc_vsli,'val',hv(3)) set(Hc_vcur,'string',sprintf('%.2f',hv(3))) %-------------------------------------------------------------------- % After an RGB slider has changed, update the 'new' frame from % the RGB slider values, and update sliders and text displays. %-------------------------------------------------------------------- elseif strcmp(S,'rgb2new') % set 'new' color from rgb sliders set(Hc_nfr,'backgroundcolor',... [get(Hc_rsli,'val'),get(Hc_gsli,'val'),get(Hc_bsli,'val')]) mmsetc(0,'setsli'); %-------------------------------------------------------------------- % After an HSV slider has changed, update the 'new' frame from % the HSV slider values, and update sliders and text displays. %-------------------------------------------------------------------- elseif strcmp(S,'hsv2new') rb=hsv2rgb([get(Hc_hsli,'val'),get(Hc_ssli,'val'),get(Hc_vsli,'val')]); set(Hc_nfr,'backgroundcolor',rb) mmsetc(0,'setsli'); %-------------------------------------------------------------------- % Set the 'initial' frame color from the popup selection. %-------------------------------------------------------------------- elseif strcmp(S,'setinit') ud=get(Hc_pcolor,'userdata'); set(Hc_ifr,'backgroundcolor',ud(get(Hc_pcolor,'val'),:)) set(Hc_icur,'string',... sprintf('[%.2f %.2f %.2f]',get(Hc_ifr,'backgroundcolor'))) %-------------------------------------------------------------------- % If the string S is not one of the above, it is in error. %-------------------------------------------------------------------- else error(ermsg1); end %-------------------------------------------------------------------- % Since this is a callback, return to the calling function. %-------------------------------------------------------------------- returnend
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -