Skip to content

Instantly share code, notes, and snippets.

@briochemc
Created June 12, 2018 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briochemc/a9c5ec9f936c5736c2248784e7be8933 to your computer and use it in GitHub Desktop.
Save briochemc/a9c5ec9f936c5736c2248784e7be8933 to your computer and use it in GitHub Desktop.
make a colormap out of a screenshot
function [cbar] = make_c_map_out_of_screenshot(impath,crop,column)
% ------ Help for make_c_map_out_of_screenshot -------
%
% Creates a colorbar from a screenshot of another colorbar.
%
% How to use:
% 1) Make screenshot of the colorbar (Ideally, have the left most column of
% pixels right on the colormap)
% 2) Save as image file (png works, maybe other formats will not, try it out!)
% 3) Put path to file in some variable, e.g.:
% >> impath = 'my_path_to_my_image' ;
% 4) (optional) If you need to crop the top and bottom, put the number of pixels
% to crop in a variable `crop`, e.g.:
% >> crop = [2 3] ; % will crops 2 pixels on top and 3 pixels on bottom
% 5) (optional) If you need to select a specific column from
% the image other than the first one, specify it in a variable `column`, e.g.:
% >> column = 4 ; % will take the 4th column for the colormap
% 6) Call the function:
% >> cbar = make_c_map_out_of_screenshot(impath) ; % will put the colorbar in `cbar`
% >> cbar = make_c_map_out_of_screenshot(impat,crop) ; % will also crop top and bottom
% >> cbar = make_c_map_out_of_screenshot(impat,crop,column) ; % will also select the column you chose
% If you want to test it (when chosing how to crop), use without output argument:
% >> make_c_map_out_of_screenshot(impat,crop,column) ; % will not save the cbar but will plot a test figure
%
% ------ End Help for make_c_map_out_of_screenshot ----
% Copyright (c) 2018 Benoit Pasquier
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, including without limitation the rights
% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
% copies of the Software, and to permit persons to whom the Software is
% furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all
% copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
% SOFTWARE.
if nargin < 3
column = 1 ;
end
if nargin < 2
crop = [0 0] ;
end
foo = imread(impath) ;
foo_cut = foo(1+crop(1):end-crop(2),column,:) ;
cbar256 = squeeze(foo_cut) ;
cbar = double(cbar256) / 256 ;
if nargout == 0
figure(666);
contourf(peaks)
colorbar
colormap(cbar)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment