v_colormap

PURPOSE ^

V_COLORMAP set and plot color map

SYNOPSIS ^

function [rgb,y,l]=v_colormap(map,m,n,p)

DESCRIPTION ^

V_COLORMAP set and plot color map

   Usage: (1) v_colormap([],'g');          % plot the current color map

          (2) v_colormap([],'',256);       % intepolate the current color map to 256 entries

          (3) v_colormap('copper','y');    % make copper color map linear in luminance

          (4) v_colormap('copper','L');    % make copper color map linear in lightness^2

          (5) imagesc(x,max(abs(x(:)))*[-1 31/32]);        % plot an image containing signed data and set the color range
              v_colormap('v_bipveey');                     % select a bipolar colormap
              colorbar;                                    % show the color bar

          (6) rev=360;                                                     % 360 for degrees or 2*pi for radians
              phase=repmat(linspace(0,2*rev,200),200,1);                   % create 2 cycles of linear phase
              imagesc(mod(phase+rev/128,rev)-rev/128,rev*[-1 127]/128);    % ensure data range is rev*[-1 127]/128
              v_colormap('v_circrby');                                     % select a circular colormap
              colorbar;

  Inputs:
           map  Either an (r,3) array specifying the RGB colourmap entries
                or else a string specifying a bult-in colour map. Use []
                to leave the colour map unchanged.
                Standard maps are:
                   'Jet','HSV','Hot','Cool','Spring','Summer','Autumn',
                   'Winter','Gray','Bone','Copper','Pink','Lines','Parula'
                Additional maps, all with 64 entries, are:
                   'v_thermliny'  thermal scale that is linear in luminance
                                  Varies from black through blue, red, yellow to white.
                                  Luminance varies linearly from black to white.
                   'v_bipliny'    bipolar scale that is linear in luminance
                                  Negative values are blue/turquoise and postive value are orange/yellow.
                                  Luminance varies linearly from black to white with zero at 50.8% gray.
                   'v_bipveey'    bipolar scale that is V-shaped in luminance
                                  Negative values are blue/turqoise and positive values are red/yellow.
                                  Luminance is proportional to absolute value with zero=black.
                   'v_circrby'    Circular scale that is V-shaped in luminance and is red for
                                  entries 2-32 and blue for entries 34-64. Intended for plotting phase.
                For 'v_bipliny' and 'v_circrby', zero corresponds to entry 33 and so the range of values
                is -32:31 or, equivalently, either [-1, +0.96875=31/32] or [-1.0323=-32/31,+1]. See usage
                example (5) above.

             m  Mode string:
                   'g' to plot information about the color map
                   'y' to force luminance^p to be linear or V-shaped (two linear segments)
                   'l' to force lightness^p to be linear or V-shaped (two linear segments)
                   'Y' like 'y' but with default p=0.667
                   'L' like 'l' but with default p=2
                   'f' flips the map to reverse its order
                   'b' force minimum luminance >=0.05 (or 0.1 for 'B')
                   'w' force maximum luminance <=0.95 (or 0.9 for 'W')
                   'k' to keep the current color map (i.e. don't update it to a new one]

             n  the number of entries in the colourmap or the number in
                each linearly-interpolated segment excluding the entry shared
                with the previous segment. The total number of entries is n=sum(n).
                For modes 'y','Y','l','L' the number of segments must be 1
                or 2; otherwise the number of segments must be 1 or r-1.

             p  power law to use for linearized luminance or lightness [default p=1]
                see the description of 'y' and 'l' for its effect

 Outputs:       Note that the colormap will be updated regardless of whether outputs are
                specified. Use the 'k' option to supress updating.

           rgb  RGB color map entries; one per row.
                All values will be in the range 0 to 1

             y  column vector of luminance values (from CIEXYZ colour space)

             l  column vector of lightness values (lightness is the perceived brightness from CIELUV colour space)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [rgb,y,l]=v_colormap(map,m,n,p)
0002 %V_COLORMAP set and plot color map
0003 %
0004 %   Usage: (1) v_colormap([],'g');          % plot the current color map
0005 %
0006 %          (2) v_colormap([],'',256);       % intepolate the current color map to 256 entries
0007 %
0008 %          (3) v_colormap('copper','y');    % make copper color map linear in luminance
0009 %
0010 %          (4) v_colormap('copper','L');    % make copper color map linear in lightness^2
0011 %
0012 %          (5) imagesc(x,max(abs(x(:)))*[-1 31/32]);        % plot an image containing signed data and set the color range
0013 %              v_colormap('v_bipveey');                     % select a bipolar colormap
0014 %              colorbar;                                    % show the color bar
0015 %
0016 %          (6) rev=360;                                                     % 360 for degrees or 2*pi for radians
0017 %              phase=repmat(linspace(0,2*rev,200),200,1);                   % create 2 cycles of linear phase
0018 %              imagesc(mod(phase+rev/128,rev)-rev/128,rev*[-1 127]/128);    % ensure data range is rev*[-1 127]/128
0019 %              v_colormap('v_circrby');                                     % select a circular colormap
0020 %              colorbar;
0021 %
0022 %  Inputs:
0023 %           map  Either an (r,3) array specifying the RGB colourmap entries
0024 %                or else a string specifying a bult-in colour map. Use []
0025 %                to leave the colour map unchanged.
0026 %                Standard maps are:
0027 %                   'Jet','HSV','Hot','Cool','Spring','Summer','Autumn',
0028 %                   'Winter','Gray','Bone','Copper','Pink','Lines','Parula'
0029 %                Additional maps, all with 64 entries, are:
0030 %                   'v_thermliny'  thermal scale that is linear in luminance
0031 %                                  Varies from black through blue, red, yellow to white.
0032 %                                  Luminance varies linearly from black to white.
0033 %                   'v_bipliny'    bipolar scale that is linear in luminance
0034 %                                  Negative values are blue/turquoise and postive value are orange/yellow.
0035 %                                  Luminance varies linearly from black to white with zero at 50.8% gray.
0036 %                   'v_bipveey'    bipolar scale that is V-shaped in luminance
0037 %                                  Negative values are blue/turqoise and positive values are red/yellow.
0038 %                                  Luminance is proportional to absolute value with zero=black.
0039 %                   'v_circrby'    Circular scale that is V-shaped in luminance and is red for
0040 %                                  entries 2-32 and blue for entries 34-64. Intended for plotting phase.
0041 %                For 'v_bipliny' and 'v_circrby', zero corresponds to entry 33 and so the range of values
0042 %                is -32:31 or, equivalently, either [-1, +0.96875=31/32] or [-1.0323=-32/31,+1]. See usage
0043 %                example (5) above.
0044 %
0045 %             m  Mode string:
0046 %                   'g' to plot information about the color map
0047 %                   'y' to force luminance^p to be linear or V-shaped (two linear segments)
0048 %                   'l' to force lightness^p to be linear or V-shaped (two linear segments)
0049 %                   'Y' like 'y' but with default p=0.667
0050 %                   'L' like 'l' but with default p=2
0051 %                   'f' flips the map to reverse its order
0052 %                   'b' force minimum luminance >=0.05 (or 0.1 for 'B')
0053 %                   'w' force maximum luminance <=0.95 (or 0.9 for 'W')
0054 %                   'k' to keep the current color map (i.e. don't update it to a new one]
0055 %
0056 %             n  the number of entries in the colourmap or the number in
0057 %                each linearly-interpolated segment excluding the entry shared
0058 %                with the previous segment. The total number of entries is n=sum(n).
0059 %                For modes 'y','Y','l','L' the number of segments must be 1
0060 %                or 2; otherwise the number of segments must be 1 or r-1.
0061 %
0062 %             p  power law to use for linearized luminance or lightness [default p=1]
0063 %                see the description of 'y' and 'l' for its effect
0064 %
0065 % Outputs:       Note that the colormap will be updated regardless of whether outputs are
0066 %                specified. Use the 'k' option to supress updating.
0067 %
0068 %           rgb  RGB color map entries; one per row.
0069 %                All values will be in the range 0 to 1
0070 %
0071 %             y  column vector of luminance values (from CIEXYZ colour space)
0072 %
0073 %             l  column vector of lightness values (lightness is the perceived brightness from CIELUV colour space)
0074 
0075 % Bugs/Suggestions:
0076 %  (1) Should allow an optional "target" input (like colormap(0) does).
0077 
0078 %      Copyright (C) Mike Brookes 2012-2018
0079 %      Version: $Id: v_colormap.m 10866 2018-09-21 17:32:44Z dmb $
0080 %
0081 %   VOICEBOX is a MATLAB toolbox for speech processing.
0082 %   Home page: http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
0083 %
0084 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0085 %   This program is free software; you can redistribute it and/or modify
0086 %   it under the terms of the GNU General Public License as published by
0087 %   the Free Software Foundation; either version 2 of the License, or
0088 %   (at your option) any later version.
0089 %
0090 %   This program is distributed in the hope that it will be useful,
0091 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0092 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0093 %   GNU General Public License for more details.
0094 %
0095 %   You can obtain a copy of the GNU General Public License from
0096 %   http://www.gnu.org/copyleft/gpl.html or by writing to
0097 %   Free Software Foundation, Inc.,675 Mass Ave, Cambridge, MA 02139, USA.
0098 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0099 persistent maps nams mcal modes nmap nszs pows la lb lc lci laci lk lq yv
0100 if isempty(maps)
0101     lk=(6/29)^3;
0102     la=841/108;
0103     lb=4/29;
0104     lc=1.16;
0105     lq=la*lc*lk;
0106     lci=1/lc;
0107     laci=lci/la;
0108     %     yv=inv([1.0 0.956 0.621; 1.0 -0.272 -0.647; 1.0 -1.106 1.703]);
0109     %     yv=yv(:,1);
0110     yv=[0.2126; 0.7152; 0.0722];                    % Mapping from RGB to luminance
0111     % specifications for local color maps
0112     nams={'v_thermliny','v_bipliny','v_bipveey','v_circrby'};
0113     mcal=[1 1 1 1];                                 % +1 if need to calculate maps entry
0114     modes={'y' 'y' 'y' 'y'};                        % modes for map calculation
0115     nszs={64 64 [33 31]  [33 32]};                  % linear interpolation regions
0116     nmap=[64 64 64 64];                             % final map size
0117     pows=[1 1 1 1];                                 % powers for maps
0118     % 'v_thermliny'
0119     maps{1}=[0 0 0; 72 0 167; 252 83 16; 255 249 0; 255 255 255]/255;
0120     % 'v_bipliny': bipolar map with grayscale linearity
0121     maps{2}=[0 0 0; 0 2 203; 1 101 226; 128 128 128; 252 153 12; 252 245 0; 252 249 18; 252 252 252]/252;
0122     % 'v_bipveey'
0123     maps{3}=[0 0.95 1; 0 0 0.9; 0 0 0; 0.5 0 0; 0.80 0.78 0];
0124     % 'v_circrby'
0125     maps{4}=[0 0 0; 1 0.183 0; 1 0.9 0; 1 1 1; 0 1 0.8; 0 0.379 1; 0 0 0];
0126 end
0127 if nargin<2
0128     m='';
0129 end
0130 if nargin<4
0131     if any(m=='Y')
0132         p=2/3;
0133     elseif any(m=='L')
0134         p=2;
0135     else
0136         p=1;                            % power to raise lightness/luminance to
0137     end
0138 end
0139 pr=1/p;
0140 um=m;
0141 m=lower(m);                                     % convert mode letters to lower case
0142 oldmap=colormap;                                % get existing map
0143 rest=0;                                         % do not restore old map by default
0144 if nargin==0 || numel(map)==0                   % use existing map
0145     rgb=oldmap;
0146 elseif ischar(map)                              % if map given as a string
0147     ix=find(strcmpi(map,nams),1);               % check if it is one of ours
0148     if numel(ix)                                % if it is one of ours
0149         if mcal(ix)                             % need to calculate the map the first time around
0150             maps{ix}=v_colormap(maps{ix},modes{ix},nszs{ix},pows(ix));
0151             mcal(ix)=0;                         % don't calculate it again
0152             maps{ix}=maps{ix}(1:nmap(ix),:);    % only keep the first nmap(ix) entries
0153         end
0154         rgb=maps{ix};
0155     else
0156         rgb=colormap(map);              % not one of ours - just pass it on to standard colormap function
0157         rest=any(m=='k');               % need to restore the old map if 'k' option is set
0158     end
0159 else
0160     rgb=map; % numeric map specified
0161 end
0162 if any(m=='y') ||  any(m=='l') || (nargin>2 && numel(n)>0) % need to do linear interpolation
0163     nm=size(rgb,1);
0164     if any(m=='y') ||  any(m=='l')
0165         y=rgb*yv;  % convert map to luminance
0166         up=y(2:nm)>y(1:nm-1); % find increasing
0167         ex=up(1:nm-2)~=up(2:nm-1); % +1 for a peak or valley
0168         yd=2*up(1)-1; % +1 if y initially increasing
0169         switch sum(ex)
0170             case 0 % monotonic
0171                 if nargin<3
0172                     r=nm;
0173                 else
0174                     r=n(1);
0175                 end
0176                 if any(m=='y')
0177                     l=y([1 nm]).^p;
0178                     tt=(l(1)+(0:r-1)'*(l(2)-l(1))/(r-1)).^pr; % target luminances
0179                 else
0180                     tt=y([1 nm]');
0181                     l=(lc*(la*tt+(tt>lk).*(tt.^(1/3)-la*tt-lb))).^p;
0182                     tt=(l(1)+(0:r-1)'*(l(2)-l(1))/(r-1)).^pr; % target lightnesses
0183                     tt=laci*tt+(tt>lq).*((lci*tt+lb).^3-laci*tt); % target luminances
0184                 end
0185                 [ss,ix]=sort([tt;y]*yd);
0186             case 1 % V-shaped
0187                 ipk=find(ex,1)+1;  % position of peak/valley in y
0188                 if nargin<3
0189                     n=[ipk nm-ipk];  % size of linear segments
0190                 end
0191                 r=n(1)+n(2);  % total size of map
0192                 if any(m=='y')
0193                     l=y([1 ipk nm]).^p;
0194                     tt=(l(2)+[(1-n(1):0)*(l(2)-l(1))/(n(1)-1) (1:n(2))*(l(3)-l(2))/(n(2))]').^pr; % target luminances
0195                 else
0196                     tt=y([1 ipk nm]');
0197                     l=(lc*(la*tt+(tt>lk).*(tt.^(1/3)-la*tt-lb))).^p;
0198                     tt=(l(2)+[(1-n(1):0)*(l(2)-l(1))/(n(1)-1) (1:n(2))*(l(3)-l(2))/(n(2))]').^pr; % target lightnesses
0199                     tt=laci*tt+(tt>lq).*((lci*tt+lb).^3-laci*tt); % target luminances
0200                 end
0201                 [ss,ix]=sort([tt(1:n(1))-y(ipk); y(ipk)-tt(n(1)+1:r);y(1:ipk)-y(ipk); y(ipk)-y(ipk+1:nm)]*yd);
0202             otherwise
0203                 error('luminance has more than two monotonic segments');
0204         end
0205     else                % just linearly interpolate the given values
0206         if numel(n)==nm-1
0207             r=sum(n);
0208             y=[1;cumsum(n(:))];
0209         else
0210             r=n(1);
0211             y=1+(0:nm-1)'*(r-1)/(nm-1);
0212         end
0213         tt=(1:r)';
0214         [ss,ix]=sort([tt;y]);
0215     end
0216     jx=zeros(size(ix));
0217     jx(ix)=1:numel(jx);
0218     jx=min(max(jx(1:r)-(1:r)',1),nm-1);
0219     al=(tt-y(jx))./(y(jx+1)-y(jx)); % fraction of upper sample to include
0220     rgb=rgb(jx,:)+(rgb(jx+1,:)-rgb(jx,:)).*al(:,ones(1,3)); % update the map
0221 end
0222 if any(m=='f')
0223     rgb=rgb(end:-1:1,:);
0224 end
0225 y=rgb*yv;  % convert RGB to luminance
0226 if any(m=='b') || any(m=='w') % need to constrain luminance
0227     minyt=0.05*(any(m=='b')+any(um=='B')); % target minimum luminance
0228     maxyt=1-0.05*(any(m=='w')+any(um=='W')); % target maximum luminance
0229     maxy=max(y);
0230     miny=min(y);
0231     if maxy>maxyt || miny<minyt
0232         maxy=max(maxy,maxyt);
0233         miny=min(miny,minyt);
0234         rgb=(rgb-miny)*(maxyt-minyt)/(maxy-miny)+minyt;
0235         y=rgb*yv;  % convert RGB to luminance
0236     end
0237 end
0238 l=lc*(la*y+(y>lk).*(y.^(1/3)-la*y-lb)); % convert luminance to lightness
0239 if rest
0240     colormap(oldmap);  % restore the old map
0241 elseif ~isequal(rgb,oldmap)
0242     colormap(rgb); % update the system map
0243 end
0244 if any(m=='g')
0245     sp=[1 2 2];
0246     ssp=sum(sp);
0247     axw=0.05;
0248     nc=size(rgb,1);  % size of color map
0249     hsv=rgb2hsv(rgb);
0250     subplot(ssp,1,sp(1)+(1:sp(2)));
0251     plot(1:nc,y,'--k');
0252     hold on
0253     plot(1:nc,rgb(:,1),'-r');
0254     plot(1:nc,rgb(:,2),'-g');
0255     plot(1:nc,rgb(:,3),'-b');
0256     hold off
0257     axis([0.5 nc+0.5 -axw 1+axw]);
0258     legend('Y','R','G','B','location','best');
0259     ylabel('RGB + Y');
0260     subplot(ssp,1,sp(1)+sp(2)+1:ssp);
0261     plot(1:nc,l,'--k');
0262     hold on
0263     plot(1:nc,hsv(:,1),'-r');
0264     plot(1:nc,hsv(:,2),'-g');
0265     plot(1:nc,hsv(:,3),'-b');
0266     hold off
0267     axis([0.5 nc+0.5 -axw 1+axw]);
0268     legend('L*','H','S','V','location','best');
0269     ylabel('HSV + L*');
0270     subplot(ssp,1,1:sp(1));
0271     image(permute(reshape([rgb y(:,[1 1 1])],[nc,3,2]),[3 1 2]));
0272     set(gca,'YTick',[]);
0273 end

Generated by m2html © 2003