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