Home > atmlab > graphs > zerowhite.m

zerowhite

PURPOSE ^

ZEROWHITE Creates a colormap with white for 0

SYNOPSIS ^

function [cmap,v] = zerowhite( D, nclrs, rgb, blackl )

DESCRIPTION ^

 ZEROWHITE   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 *zerowhite2*.

    Examples on color codings:

     *rgb* : negative - zero - positive
     ------------------------------
       nzp : red - white - blue
       pzn : blue - white - red
       npz : red - white -green
       znp : green - white - blue
       pzp : black - white - pink

    The return arguments of this function are used as
       contourf(D,v)
       colormap(cmap)

 FORMAT   [cmap,v] = zerowhite( D, nclrs, rgb, blackl )
        
 OUT   cmap     Color map.
       v        Vector with contour level values.
 IN    D        Data matrix to be displayed
       nclrs    --- Scalar:
                Number of colors to use between 0 and max absolute value. 
                --- Vector:
                If this is a vector it is taken to define explictly the 
                contour intervals. This is handy if you want to create nice 
                intervals, such as [-2 -1 .5 .5 1 2]. The intervals do not 
                have to be evenly spaced. Both min and max value of this 
                vector can not be outside the data range of D (appears to
                be a bug in Matlab).
 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'.
       blackl   Level of blackness. A value between 0 and 1, where 0 means
                that no black is blended in for high value, and 1 means
                that max absolute value will be completely black.  
                Default is 0.5.

 See also: zerowhite2, zerobright

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

zerowhite.m

SOURCE CODE ^

0001 % ZEROWHITE   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 *zerowhite2*.
0005 %
0006 %    Examples on color codings:
0007 %
0008 %     *rgb* : negative - zero - positive
0009 %     ------------------------------
0010 %       nzp : red - white - blue
0011 %       pzn : blue - white - red
0012 %       npz : red - white -green
0013 %       znp : green - white - blue
0014 %       pzp : black - white - pink
0015 %
0016 %    The return arguments of this function are used as
0017 %       contourf(D,v)
0018 %       colormap(cmap)
0019 %
0020 % FORMAT   [cmap,v] = zerowhite( D, nclrs, rgb, blackl )
0021 %
0022 % OUT   cmap     Color map.
0023 %       v        Vector with contour level values.
0024 % IN    D        Data matrix to be displayed
0025 %       nclrs    --- Scalar:
0026 %                Number of colors to use between 0 and max absolute value.
0027 %                --- Vector:
0028 %                If this is a vector it is taken to define explictly the
0029 %                contour intervals. This is handy if you want to create nice
0030 %                intervals, such as [-2 -1 .5 .5 1 2]. The intervals do not
0031 %                have to be evenly spaced. Both min and max value of this
0032 %                vector can not be outside the data range of D (appears to
0033 %                be a bug in Matlab).
0034 % OPT   rgb      Coding for red, green and blue color by a string of length 3.
0035 %                The character 'p' means that positive values will be weighted
0036 %                with the corresponding color. The character 'n' refer to
0037 %                negative values. The character 'z' means max weight at zero.
0038 %                Default is 'nzp'.
0039 %       blackl   Level of blackness. A value between 0 and 1, where 0 means
0040 %                that no black is blended in for high value, and 1 means
0041 %                that max absolute value will be completely black.
0042 %                Default is 0.5.
0043 %
0044 % See also: zerowhite2, zerobright
0045 
0046 % 2003-12-06   Patrick Eriksson: Created.
0047 % 2004-04-07   Stefan Buehler and Patrick Eriksson: Added the option to have
0048 %              prescribed contour intervals.
0049 
0050 
0051 function [cmap,v] = zerowhite( D, nclrs, rgb, blackl )
0052 
0053 
0054 %= Default values
0055 %
0056 if nargin < 3
0057   rgb = 'nzp';
0058 end
0059 if nargin < 4
0060   blackl = 0.5;
0061 end
0062 
0063 
0064 %= Check input
0065 %
0066 if ~ischar(rgb)  &  length(rgb)~=3
0067   error( 'Argument *rgb* must be string of length 3' );
0068 end
0069 %
0070 if any( ~( (rgb=='n') + (rgb=='p') + (rgb=='z') ) )
0071   error( 'Argument *rgb* can only include the characters n, z and p.' );
0072 end
0073 %
0074 if ~isvector( nclrs )
0075   error('Argumnent *nclrs must be a scalar or a vector.');
0076 end
0077 %
0078 if any( nclrs == 0 )
0079   error('Argument *nclrs* can not contain the value 0.')
0080 end
0081   
0082 
0083 
0084 %= Get max/min values
0085 %
0086 
0087 if length(nclrs) == 1 
0088   % Make nclrs contour levels.
0089   minv = min( min( D ) );
0090   maxv = max( max( D ) );
0091   amax = max( abs( [ minv, maxv ] ) );
0092   dv   = amax / nclrs;
0093 
0094   %= Create vector with contour levels
0095   %
0096   if abs( minv ) < abs( maxv )
0097     v = fliplr( (maxv-dv/2) : -dv : (minv-dv) );
0098     w = ( 1 + blackl ) * ( v' + dv/2 ) / amax;
0099   else
0100     v = (minv-dv/2) : dv : (maxv+dv);
0101     w = ( 1 + blackl ) * ( v' - dv/2 ) / amax;
0102   end
0103 
0104 else
0105 
0106   v = nclrs;
0107 
0108   if any( diff(v) <= 0 )
0109     error('Explicitely selected color levels must be strictly increasing.');
0110   end
0111 
0112   if v(1) < min(min(D))  &  tail(v,1) > max(max(D))
0113     error( ...
0114          'Either first or last value of *nclrs* must be in data range of *D*');
0115   end  
0116 
0117   %= Create vector with contour levels
0118   %
0119   if abs(v(1)) > abs(tail(v,1))
0120     i = max( find( v < 0 ) );
0121     w = ( 1 + blackl ) * ( v' - v(i) ) / max(abs(v));
0122   else
0123     i = min( find( v > 0 ) );
0124     w = ( 1 + blackl ) * ( v' + v(i) ) / max(abs(v));
0125   end
0126 
0127   % With *blackl* close to 1, we can get somewhat too high weights.
0128   % A dirty fix:
0129   w( find(abs(w)>2) ) = 2;
0130 end
0131 
0132 
0133 
0134 %= Set up vectors for positive, negative and mixing colour
0135 %
0136 %
0137 p      = w + 1;
0138 ind    = find( p < 0 );
0139 p(ind) = 0;
0140 ind    = find( p > 1 & p < 2);
0141 p(ind) = 1;
0142 ind    = find( p >= 2);
0143 p(ind) = 3 - p(ind);
0144 %
0145 z      = 1 - abs( w );
0146 ind    = find( z < 0 );
0147 z(ind) = 0;
0148 %
0149 n      = -w + 1; 
0150 ind    = find( n < 0 );
0151 n(ind) = 0;
0152 ind    = find( n > 1 & n < 2);
0153 n(ind) = 1;
0154 ind    = find( n >= 2);
0155 n(ind) = 3 - n(ind);
0156 
0157 
0158 %= Put color map together
0159 %
0160 cmap = zeros( length(w), 3 );
0161 %
0162 cmap(:,1) = eval( rgb(1) ); 
0163 cmap(:,2) = eval( rgb(2) ); 
0164 cmap(:,3) = eval( rgb(3) ); 
0165 
0166

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