Home > atmlab > graphs > zerowhite2.m

zerowhite2

PURPOSE ^

ZEROWHITE2 Creates a colormap with white for 0

SYNOPSIS ^

function zerowhite2(dx,varargin)

DESCRIPTION ^

 ZEROWHITE2   Creates a colormap with white for 0

    This function creates a color map that gives white for values around
    zero. An alternative version is found in *zerowhite*. This version is
    easier to use with Matlab's *colorbar*. Example on usage:
       pcolor(rand(10,20));
       caxis([-1 1]),zerowhite2(0.1,'pnz',0.4,0.5),colorbar

    Each color is set to correspond to a data range of *dx*. The white
    color covers always the range [-dx/2,dx/2[, even though data are either
    strictly positive or negative. As many ranges as possible are created
    without going outside range given by *caxis*.

    The function sets both *caxis* and *colormap*. As the number of color
    ranges is based on settings in *caxis*, the plot can change if the
    function is called twice with exactly same settings, if not *caxis* is
    re-set in between.

 FORMAT   zerowhite2(dx[,rgb,pbl,nbl])
        
 IN    dx       Step in data corresponding to each color.
 OPT   rgb      Coding for red, green and blue color by a string of length 3.
                The character 'p' means that positive values will be weighted
                with the corresponding color. The character 'n' refer to
                negative values. The character 'z' means max weight at zero.
                Default is 'nzp'.
       pbl      Level of blackness on positive side. A value between 0 and
                1, where 0 means that no black is blended in for high
                values, and 1 means that max positive value will be 
                completely black. Default is 0.5.
       nbl      Level of blackness on negative side. As *pbl* but for
                negative values.

 See also: zerowhite, zerobright

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

zerowhite2.m

SOURCE CODE ^

0001 % ZEROWHITE2   Creates a colormap with white for 0
0002 %
0003 %    This function creates a color map that gives white for values around
0004 %    zero. An alternative version is found in *zerowhite*. This version is
0005 %    easier to use with Matlab's *colorbar*. Example on usage:
0006 %       pcolor(rand(10,20));
0007 %       caxis([-1 1]),zerowhite2(0.1,'pnz',0.4,0.5),colorbar
0008 %
0009 %    Each color is set to correspond to a data range of *dx*. The white
0010 %    color covers always the range [-dx/2,dx/2[, even though data are either
0011 %    strictly positive or negative. As many ranges as possible are created
0012 %    without going outside range given by *caxis*.
0013 %
0014 %    The function sets both *caxis* and *colormap*. As the number of color
0015 %    ranges is based on settings in *caxis*, the plot can change if the
0016 %    function is called twice with exactly same settings, if not *caxis* is
0017 %    re-set in between.
0018 %
0019 % FORMAT   zerowhite2(dx[,rgb,pbl,nbl])
0020 %
0021 % IN    dx       Step in data corresponding to each color.
0022 % OPT   rgb      Coding for red, green and blue color by a string of length 3.
0023 %                The character 'p' means that positive values will be weighted
0024 %                with the corresponding color. The character 'n' refer to
0025 %                negative values. The character 'z' means max weight at zero.
0026 %                Default is 'nzp'.
0027 %       pbl      Level of blackness on positive side. A value between 0 and
0028 %                1, where 0 means that no black is blended in for high
0029 %                values, and 1 means that max positive value will be
0030 %                completely black. Default is 0.5.
0031 %       nbl      Level of blackness on negative side. As *pbl* but for
0032 %                negative values.
0033 %
0034 % See also: zerowhite, zerobright
0035 
0036 % 2007-03-12   Created by Patrick Eriksson.
0037 
0038 
0039 function zerowhite2(dx,varargin)
0040 %
0041 [rgb,pbl,nbl] = optargs( varargin, { 'nzp', 0.5, 0.5 } );
0042 
0043   
0044 if dx <= 0
0045   error( 'Argument *dx* must be positive.' );
0046 end
0047 
0048 
0049 
0050 ca = caxis;
0051 %
0052 if ca(1) > 0  |  ca(2) < 0
0053   error( 'Zero is not inside present color range.' );
0054 end
0055 
0056 
0057 %- Centre point for color interval and corresponding color axis
0058 %
0059 x = unique( [ 0:-dx:ca(1)+dx/2 0:dx:ca(2)-dx/2 ] );
0060 %
0061 ca = [ x(1)-dx/2 x(end)+dx/2];
0062 
0063 
0064 
0065 %- Color distance
0066 %
0067 d = x';
0068 %
0069 ind = find( x > 0 );
0070 d(ind) = (1+pbl) * x(ind)/ca(2);
0071 %
0072 ind = find( x < 0 );
0073 d(ind) = (1+nbl) * x(ind)/abs(ca(1));
0074 
0075 
0076 
0077 %--- Create color map
0078 
0079 %- Set colors first strictly from color distance
0080 %
0081 cmap = ones( length(x), 3 );
0082 %
0083 for i = 1:3
0084 
0085   if rgb(i) == 'p'
0086     cmap(:,i) = 1 + d;
0087   elseif rgb(i) == 'n'
0088     cmap(:,i) = 1 - d;
0089   elseif rgb(i) == 'z'
0090     cmap(:,i) = 1 - abs(d);
0091   else
0092     error( 'Unknown selection in *rgb*.' );
0093   end
0094   
0095 end
0096 
0097 %- Cut all values above 1
0098 %
0099 cmap( find(cmap>1) ) = 1;
0100 
0101 
0102 %- Use negative values to determine blending towards black
0103 %
0104 for i = 1 : size(cmap,1)
0105   
0106   minv = min( cmap(i,:) );
0107   
0108   if minv < 0
0109     cmap(i,:) = cmap(i,:) + minv;
0110   end
0111 end
0112 %
0113 cmap( find(cmap<0) ) = 0;
0114 
0115 
0116 
0117 %--- Set  colors
0118 %
0119 caxis( ca );
0120 colormap( cmap );
0121 
0122   
0123

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005