spm_colourmap.m 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. function varargout = spm_colourmap(varargin)
  2. % Colourmap multi-function
  3. % FORMAT map = spm_colourmap
  4. % Return the colourmap of the current figure as a three-column matrix of
  5. % RGB triplets (between 0.0 and 1.0).
  6. %
  7. % FORMAT [map =] spm_colourmap(map)
  8. % Define a colourmap or set it to the current figure.
  9. % map - gray, hot, pink, jet, ...: built-in colourmaps {64 x 3}
  10. % - gray-hot, ...: creates a 'split' colourmap {128 x 3 matrix}
  11. % The lower half is a gray scale and the upper half is
  12. % selected colourmap. This colourmap is used for viewing
  13. % 'rendered' SPMs on a PET, MRI or other background images.
  14. %
  15. % FORMAT [map = ] spm_colourmap(effect[,map])
  16. % Apply an effect to a colourmap then return it or apply it to the current
  17. % figure.
  18. % effect - 'Invert' - invert (flip) the colourmap
  19. % 'Brighten' - call MATLAB's brighten with a beta of +0.2
  20. % 'Darken' - call MATLAB's brighten with a beta of -0.2
  21. %
  22. % FORMAT maps = spm_colourmap('list')
  23. % Return the list of all colourmaps' name (see graph3d).
  24. %
  25. % FORMAT [map =] spm_colourmap('load',fname)
  26. % Load a colourmap from file (*.lut, *.cmap, *.mat) then return it or apply
  27. % it to the current figure.
  28. %
  29. % FORMAT spm_colourmap('save',fname[,map])
  30. % Save a colourmap to file (format according to file extension).
  31. %__________________________________________________________________________
  32. %
  33. % A repository of colourmaps with linearised luminance is available at:
  34. % https://github.com/CPernet/brain_colours
  35. %__________________________________________________________________________
  36. % Copyright (C) 2018 Wellcome Trust Centre for Neuroimaging
  37. % Guillaume Flandin
  38. % $Id: spm_colourmap.m 7428 2018-09-25 13:25:13Z guillaume $
  39. %-FORMAT map = spm_colourmap([map])
  40. %--------------------------------------------------------------------------
  41. if ~nargin
  42. varargout = { colormap };
  43. return;
  44. elseif isnumeric(varargin{1}) && size(varargin{1},2) == 3
  45. map = varargin{1};
  46. action = 'NOP';
  47. else
  48. action = varargin{1};
  49. end
  50. %-Colourmap operation
  51. %--------------------------------------------------------------------------
  52. N = 64; % or size(get(gcf,'colormap'),1)
  53. switch lower(action)
  54. case 'load'
  55. if nargin > 1
  56. fname = varargin{2};
  57. else
  58. fname = spm_select(1,'any','Select colourmap file...');
  59. end
  60. map = load_colourmap(fname);
  61. case 'save'
  62. if nargin > 1
  63. fname = varargin{2};
  64. else
  65. [fname, pth] = uiputfile;
  66. if isequal(fname,0) || isequal(pth,0)
  67. return;
  68. end
  69. fname = fullfile(pth,fname);
  70. end
  71. map = spm_colourmap(varargin{3:end});
  72. save_colourmap(fname,map);
  73. return;
  74. case 'default'
  75. map = 'default';
  76. case 'list'
  77. varargout = { list_colourmaps };
  78. return;
  79. case list_colourmaps
  80. map = feval(lower(action),N);
  81. case 'gray-hot'
  82. tmp = hot(N + N/4); tmp = tmp((1:N) + N/4,:);
  83. map = [gray(N); tmp];
  84. case 'gray-cool'
  85. if N == 64
  86. tmp = [zeros(10,1) zeros(10,1) linspace(0.5,1,10)';
  87. zeros(31,1) linspace(0,1,31)' ones(31,1);
  88. linspace(0,1,23)' ones(23,1) ones(23,1) ];
  89. else
  90. tmp = cool(N);
  91. end
  92. map = [gray(64); tmp];
  93. case 'gray-pink'
  94. tmp = pink(N + N/4); tmp = tmp((1:N) + N/4,:);
  95. map = [gray(N); tmp];
  96. case {'invert','brighten','darken','equalise'}
  97. map = spm_colourmap(varargin{2:end});
  98. switch lower(action)
  99. case 'invert'
  100. map = flipud(map);
  101. case 'brighten'
  102. map = brighten(map, 0.2);
  103. case 'darken'
  104. map = brighten(map, -0.2);
  105. case 'equalise'
  106. map = equalise_colourmap(map);
  107. end
  108. case 'nop'
  109. otherwise
  110. s = regexp(action,'-','split');
  111. if numel(s) > 1
  112. map = [];
  113. for i=1:numel(s)
  114. map = [map; spm_colourmap(s{i})];
  115. end
  116. else
  117. error('Illegal action specification');
  118. end
  119. end
  120. %-Update current figure's colourmap or return it
  121. %--------------------------------------------------------------------------
  122. if nargout
  123. varargout = { map };
  124. else
  125. colormap(map);
  126. end
  127. %==========================================================================
  128. function maps = list_colourmaps
  129. %==========================================================================
  130. maps = {...
  131. 'parula', 'hsv', 'hot', 'gray', 'bone', 'copper', 'pink', 'white', ...
  132. 'flag', 'lines', 'colorcube', 'jet', 'prism', 'cool', 'autumn', ...
  133. 'spring', 'winter', 'summer'};
  134. %==========================================================================
  135. function map = load_colourmap(fname)
  136. %==========================================================================
  137. ext = spm_file(fname,'ext');
  138. if isempty(ext), ext = 'mat'; fname = spm_file(fname,'ext',ext); end
  139. switch ext
  140. case 'lut'
  141. fid = fopen(fname,'rb');
  142. if fid == -1, error('Cannot open colourmap file.'); end
  143. map = fread(fid,Inf,'uchar=>double');
  144. fclose(fid);
  145. map = reshape(map,[],3);
  146. case 'cmap'
  147. map = load(fname,'-ascii');
  148. case 'mat'
  149. map = struct2cell(load(fname));
  150. map = map{1};
  151. otherwise
  152. error('Unknown colour map format.');
  153. end
  154. if any(map(:)>1)
  155. map = map / 255;
  156. end
  157. if size(map, 2) ~= 3
  158. warning('Colourmap was not an N by 3 matrix.');
  159. map = [];
  160. end
  161. %==========================================================================
  162. function save_colourmap(fname,map)
  163. %==========================================================================
  164. ext = spm_file(fname,'ext');
  165. if isempty(ext), ext = 'mat'; fname = spm_file(fname,'ext',ext); end
  166. switch ext
  167. case 'lut'
  168. fid = fopen(fname,'wb');
  169. if fid == -1, error('Cannot open colourmap file.'); end
  170. fwrite(fid,255*map,'uchar');
  171. fclose(fid);
  172. case 'cmap'
  173. save(fname,'map', '-ascii');
  174. case 'mat'
  175. save(fname,'map', spm_get_defaults('mat.format'));
  176. otherwise
  177. error('Unknown colourmap format.');
  178. end
  179. %==========================================================================
  180. function map = equalise_colourmap(map)
  181. %==========================================================================
  182. % Require equalisecolourmap.m from Peter Kovesi:
  183. % https://www.peterkovesi.com/matlabfns/Colourmaps/equalisecolourmap.m
  184. % and the Image Processing Toolbox (whitepoint, makecform, applycform)
  185. % map = equalisecolourmap('RGB',map,'CIE76',[1 1 1],1/25*length(map),0,0);
  186. error('Colour contrast equalisation over a colourmap not yet supported.');