AdjustColorMap - Adjust colormap for current figure, i.e. change gamma. USAGE m = AdjustColorMap(<options>) <options> optional list of property-value pairs (see table below) ========================================================================= Properties Values ------------------------------------------------------------------------- 'shift' proportional hue shift (0 = no change, default) 'gamma' standard gamma for luminance (1 = no change, default) 'hgamma' gamma for hue (1 = no change, default) 'hrotate' proportional hue rotation (0 = no change, default) =========================================================================
0001 function m = AdjustColorMap(varargin) 0002 0003 %AdjustColorMap - Adjust colormap for current figure, i.e. change gamma. 0004 % 0005 % USAGE 0006 % 0007 % m = AdjustColorMap(<options>) 0008 % 0009 % <options> optional list of property-value pairs (see table below) 0010 % 0011 % ========================================================================= 0012 % Properties Values 0013 % ------------------------------------------------------------------------- 0014 % 'shift' proportional hue shift (0 = no change, default) 0015 % 'gamma' standard gamma for luminance (1 = no change, default) 0016 % 'hgamma' gamma for hue (1 = no change, default) 0017 % 'hrotate' proportional hue rotation (0 = no change, default) 0018 % ========================================================================= 0019 0020 % Copyright (C) 2013 by Michaƫl Zugaro 0021 % 0022 % This program is free software; you can redistribute it and/or modify 0023 % it under the terms of the GNU General Public License as published by 0024 % the Free Software Foundation; either version 3 of the License, or 0025 % (at your option) any later version. 0026 0027 % Default values 0028 gamma = 1; 0029 hgamma = 1; 0030 hrotate = 1; 0031 shift = 0; 0032 0033 % Parse parameter list 0034 for i = 1:2:length(varargin), 0035 if ~ischar(varargin{i}), 0036 error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help AdjustColorMap">AdjustColorMap</a>'' for details).']); 0037 end 0038 switch(lower(varargin{i})), 0039 case 'gamma', 0040 gamma = varargin{i+1}; 0041 if ~isdscalar(gamma,'>=0'), 0042 error('Incorrect value for property ''gamma'' (type ''help <a href="matlab:help AdjustColorMap">AdjustColorMap</a>'' for details).'); 0043 end 0044 case 'hgamma', 0045 hgamma = varargin{i+1}; 0046 if ~isdscalar(hgamma,'>=0'), 0047 error('Incorrect value for property ''hgamma'' (type ''help <a href="matlab:help AdjustColorMap">AdjustColorMap</a>'' for details).'); 0048 end 0049 case 'shift', 0050 shift = varargin{i+1}; 0051 if ~isdscalar(shift), 0052 error('Incorrect value for property ''shift'' (type ''help <a href="matlab:help AdjustColorMap">AdjustColorMap</a>'' for details).'); 0053 end 0054 case 'hrotate', 0055 hrotate = varargin{i+1}; 0056 if ~isdscalar(hrotate), 0057 error('Incorrect value for property ''hrotate'' (type ''help <a href="matlab:help AdjustColorMap">AdjustColorMap</a>'' for details).'); 0058 end 0059 otherwise, 0060 error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help AdjustColorMap">AdjustColorMap</a>'' for details).']); 0061 end 0062 end 0063 0064 % Convert from RGB to HSL 0065 hsl = hsv2hsl(rgb2hsv(colormap)); 0066 h = hsl(:,1); 0067 s = hsl(:,2); 0068 l = hsl(:,3); 0069 0070 % Correct luminance and hue gammas 0071 h = h .^ (1/hgamma); 0072 l = l .^ (1/gamma); 0073 0074 % Convert from HSL to RGB 0075 m = hsv2rgb(hsl2hsv([h s l])); 0076 % bad = any(isnan(m),2); 0077 % m(bad,:) = []; 0078 % m = Clip(m,0,1); 0079 0080 % Rotate colors 0081 n = size(m,1); 0082 r = mod(round(abs(hrotate)*n),n); 0083 m = [m(r+1:end,:) ; m(1:r,:)]; 0084 0085 % Correct hue 0086 l = size(m,1); 0087 n = round(abs(shift)*l); 0088 if shift > 0, 0089 m = [m ; repmat(m(end,:),n,1)]; 0090 elseif shift < 0, 0091 m = [repmat(m(1,:),n,1) ; m]; 0092 end 0093 0094 % Apply 0095 colormap(m); 0096 0097 0098 0099 0100 0101 0102 0103 0104 % % Convert RGB to HSV 0105 % hsv = rgb2hsv(colormap); 0106 % 0107 % % Convert HSV to HSL 0108 % h = hsv(:,1); 0109 % s = hsv(:,2) .* hsv(:,3); 0110 % l = (2-hsv(:,2)).*hsv(:,3); 0111 % if l <= 1, 0112 % s = s ./ l; 0113 % else 0114 % s = s ./ (2-l); 0115 % end 0116 % l = l/2; 0117 % 0118 % % Correct gamma 0119 % l = l .^ (1/gamma); 0120 % 0121 % % Convert HSL to HSV 0122 % hsv(:,1) = h .^ (1/hgamma); 0123 % l = l * 2; 0124 % if l <= 1, 0125 % s = s .* l; 0126 % else 0127 % s = s .* (2-l); 0128 % end 0129 % hsv(:,2) = (2 * s) ./ (l + s); 0130 % hsv(:,3) = (l + s) / 2; 0131 % 0132 % % Convert to RGB 0133 % m = hsv2rgb(hsv); 0134 % bad = any(isnan(m),2); 0135 % m(bad,:) = []; 0136 % m = Clip(m,0,1); 0137 % 0138 % % Rotate colors 0139 % n = size(m,1); 0140 % r = mod(round(abs(hrotate)*n),n); 0141 % m = [m(r+1:end,:) ; m(1:r,:)]; 0142 % 0143 % % Correct hue 0144 % l = size(m,1); 0145 % n = round(abs(shift)*l); 0146 % if shift > 0, 0147 % m = [m ; repmat(m(end,:),n,1)]; 0148 % elseif shift < 0, 0149 % m = [repmat(m(1,:),n,1) ; m]; 0150 % end 0151 % 0152 % % Apply 0153 % colormap(m);