www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/uisetcolormap.m

    function [outmap,outints]=uisetcolormap(inmap,inints)
%UISETCOLORMAP Interactive colormap editor
%
%  UISETCOLORMAP allows editing of a colormap and the breaks associated
%  with it.
%
%  map = UISETCOLORMAP(inmap,inints) returns the colormap from a gui
%  initialised with the colormap inmap and the intervals inints.  The
%  number of interval values should be equal to length(inmap)-1 - the first
%  and last colors in the map are assumed to apply from -inf and to inf
%  respectively.
%
%  [map,ints]=UISETCOLORMAP(inmap,inints) also returns the new intervals.
%
%  Specifying NaN for, or omitting, the inints parameter will cause the
%  colormap gui to not have any display of interval numbers.

%  Copyright 2000-2014 The MathWorks, Inc. and Ford Global Technologies, Inc.

if ~usejava('swing')
    h = errordlg('Colormap editor requires Java Swing component support.');
    waitfor(h);
    outmap = 0;
    outints = 0;
    return
end

SHOW_INTS = true;

if nargin<2
    % Intervals not specified => do not show them
    inints = [];
    SHOW_INTS = false;
elseif isempty(inints)
    % Empty but specified - need to use a default value for them
    inints = 0;
elseif all(isnan(inints))
    % Specified as NaN => do not show them
    inints = [];
    SHOW_INTS = false;
end

if nargin<1
    inmap = [0 0 0];
end

% Make intervals the correct size 
if ~isempty(inints)
    if length(inints)<(size(inmap,1)-1)
        inints(end+1:(size(inmap,1)-1)) = inints(end);
    else
        inints = inints(1:(size(inmap,1)-1));
    end
end

if ~SHOW_INTS
   fight = 140;
   cmapht = 36;
else
   fight = 160;
   cmapht = 53;
end

if size(inmap,2)~=3
    error(message('mbc:uisetcolormap:InvalidArgument'));
end
if any(inmap(:)>1) || any(inmap(:)<0)
    error(message('mbc:uisetcolormap:InvalidArgument1'));
end

ud.Map = inmap;
ud.BreakValues = inints;
ud.ShowBreakValues = SHOW_INTS;

SC = xregGui.SystemColorsDbl;

figh = xregdialog('name', 'Colormap Editor');
xregcenterfigure(figh, [400, fight]);

okbtn = uicontrol('Parent',figh,...
    'Style','pushbutton',...
    'String','OK',...
    'Callback',@(src, evt) set(figh, 'Tag', 'ok', 'Visible', 'off'));
cancbtn = uicontrol('Parent',figh,...
    'Style','pushbutton',...
    'String','Cancel',...
    'Callback',@(src, evt) set(figh, 'Tag', 'cancel', 'Visible', 'off'));

ud.hMapSize = mbcgui.widget.Spinner('Parent', figh, ...
    'Min',1,...
    'Max', 2^12, ...  % Need to limit size of colormap allowed to sensible values
    'Rule','int',...
    'ClickIncrement',1,...
    'Value',size(inmap,1),...
    'Callback', {@i_cmapsize, figh});
cmaplist = {'Custom';'autumn';'bone';'colorcube'; ...
    'cool';'copper';'flag';'gray';'hot';'hsv';'jet';...
    'lines';'parula';'pink';'prism';'spring';'summer';'white';'winter'};
ud.hMapName = uicontrol('Style','popupmenu',...
    'Parent',figh,...
    'String',cmaplist,...
    'BackgroundColor', SC.WINDOW_BG, ...
    'Callback', {@i_cmapname, figh});
ud.hGradient = uicontrol('Style','pushbutton',...
    'Parent',figh,...
    'String','Create a Gradient', ...
    'Callback', {@i_gradient, figh});
ud.hEntryNums = uicontrol('Style','checkbox',...
   'Parent',figh,...
   'String','Show entry numbers',...
   'Value',0, ...
   'Callback', {@i_showcmapnumbers, figh});
P = com.mathworks.toolbox.mbc.gui.peer.ColormapEditorTablePeer;
ud.hMapTable = mbcwidgets.Table1D(P, ...
    'Parent', figh, ...
    'Editable', true, ...
    'SelectionMode', 'SingleRegion', ...
    'ValueChangedCallback', {@i_editmap, figh});

lblNumCols = xregGui.labelcontrol('parent', figh, ...
    'String', 'Colormap size:', ...
    'LabelSizeMode', 'absolute', ...
    'LabelSize', 120, ...
    'ControlSize', 60, ...
    'Control', ud.hMapSize);
lblName = xregGui.labelcontrol('parent', figh, ...
    'String', 'Colormap:', ...
    'LabelSizeMode', 'absolute', ...
    'LabelSize', 90, ...
    'ControlSize', 90, ...
    'Control', ud.hMapName, ...
    'Border', [0 2 0 3]);

L1 = xreggridbaglayout(figh, ...
    'packstatus', 'off', ...
    'dimension', [4 4], ...
    'rowsizes', [25 20 0 cmapht], ...
    'gapy', 5, ...
    'colsizes', [200 -1  30 100], ...
    'mergeblock', {[2 2], [3 4]}, ...
    'mergeblock', {[4 4], [1 4]}, ...
    'elements', {lblName, lblNumCols, [], ud.hMapTable, ...
    [], [], [], [], ...
    [], ud.hEntryNums, [], [], ...
    ud.hGradient});
L2 = xreggridbaglayout(figh, ...
    'dimension', [2 3], ...
    'rowsizes', [-1 25], ...
    'colsizes', [-1 65 65], ...
    'border', [7 7 7 7], ...
    'gap', 7, ...
    'mergeblock', {[1 1], [1 3]}, ...
    'elements', {L1, [], [], okbtn, [], cancbtn});

figh.UserData = ud;
figh.LayoutManager = L2;
set(L2, 'packstatus', 'on');

i_setupUI(ud);

figh.showDialog(okbtn);

tg = get(figh,'Tag');
if strcmp(tg, 'ok')
   ud = get(figh,'UserData');
   outmap = ud.Map;
   if nargout==2
      outints = ud.BreakValues;
   end
else
    outmap = 0;
    if nargout==2
        outints = 0;
    end
end
delete(figh);


function i_setupUI(ud)
%Try and match input colormap against the standard ones
cmapstr = get(ud.hMapName, 'String');
cmapsize = size(ud.Map, 1);
MapIdx = 1;
for n = 2:length(cmapstr)
    standardmap = feval(cmapstr{n}, cmapsize);
    if all(standardmap==ud.Map)
        MapIdx = n;
        break
    end
end
set(ud.hMapName, 'Value', MapIdx);
ud.hMapSize.Value = cmapsize;

if ~ud.ShowBreakValues
    ud.hMapTable.Peer.setColormap(ud.Map);
else
    ud.hMapTable.Peer.setColormap(ud.Map, ud.BreakValues);
end


function i_cmapname(~, ~, figh)
ud = get(figh, 'UserData');
cmapindex = get(ud.hMapName, 'Value');
if cmapindex>1
    cmapstr = get(ud.hMapName, 'String');
    cmapsize = ud.hMapSize.Value;
    ud.Map = feval(cmapstr{cmapindex}, cmapsize);

    if ~ud.ShowBreakValues
        ud.hMapTable.Peer.setColormap(ud.Map);
    else
        ud.hMapTable.Peer.setColormap(ud.Map, ud.BreakValues);
    end

    set(figh, 'UserData', ud);
end

function i_cmapsize(~, ~, figh)
ud = get(figh, 'UserData');
cmapindex = get(ud.hMapName, 'Value');
cmapsize = ud.hMapSize.Value;
cmapsizeold = size(ud.Map,1);

if cmapindex==1
    % Extend or contract the custom colormap
    if (cmapsizeold<cmapsize)
        ud.Map = [ud.Map; repmat(ud.Map(end,:), cmapsize-cmapsizeold, 1)];
    else
        ud.Map = ud.Map(1:cmapsize,:);
    end
else
    % Create a new standard colormap
    cmapstr = get(ud.hMapName, 'String');
    ud.Map = feval(cmapstr{cmapindex}, cmapsize);
end

if ~ud.ShowBreakValues
    ud.hMapTable.Peer.setColormap(ud.Map);
else
    % Extend or contract the breakpoints
    if (cmapsizeold<cmapsize)
        if ~isempty(ud.BreakValues)
            ud.BreakValues((end+1):(cmapsize-1)) = ud.BreakValues(end);
        else
            ud.BreakValues(1:(cmapsize-1)) = 0;
        end
    else
        ud.BreakValues = ud.BreakValues(1:(cmapsize-1));
    end
    ud.hMapTable.Peer.setColormap(ud.Map, ud.BreakValues);
end

set(figh, 'UserData', ud);


function i_showcmapnumbers(~, ~, figh)
ud = get(figh, 'UserData');
ud.hMapTable.Peer.setShowEntryNumbers(get(ud.hEntryNums, 'Value'));


function i_gradient(~, ~, figh)
ud = get(figh, 'UserData');
cmapsize = size(ud.Map, 1);
if cmapsize>2
    stcol = ud.Map(1,:);
    endcol = ud.Map(end,:);
    ud.Map = [linspace(stcol(1), endcol(1), cmapsize)', ...
        linspace(stcol(2), endcol(2), cmapsize)', ...
        linspace(stcol(3), endcol(3), cmapsize)'];

    if isempty(ud.BreakValues)
        ud.hMapTable.Peer.setColormap(ud.Map);
    else
        ud.hMapTable.Peer.setColormap(ud.Map, ud.BreakValues);
    end
    
    % Set colormap to custom
    set(ud.hMapName, 'Value', 1);

    set(figh, 'UserData', ud);
end


function i_editmap(~, evt, figh)
ud = get(figh, 'UserData');
data = evt.data;

for n = 1:length(data.Rows)
    if ~isempty(ud.BreakValues) && data.Rows(n)==1
        % New numbers
        ud.BreakValues(data.Columns) = data.NewValue(n, :);
    else
        % New colors
        for m = 1:length(data.Columns)
            col = data.NewValue(n, m);
            ud.Map(data.Columns(m),1) = col.getRed./255;
            ud.Map(data.Columns(m),2) = col.getGreen./255;
            ud.Map(data.Columns(m),3) = col.getBlue./255;            
        end
    end
end

% Set colormap to custom
set(ud.hMapName, 'Value', 1);

set(figh, 'UserData', ud);