Skip to content

Commit

Permalink
Refer to ColorBrewer 2.0
Browse files Browse the repository at this point in the history
* Clear ColorBrewer 2.0 references.
* Improve error IDs and messages.
  • Loading branch information
DrosteEffect committed Jan 15, 2024
1 parent e356ed2 commit 1773eb1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 53 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
BREWERMAP Function
==================

BREWERMAP provides all ColorBrewer colorschemes for MATLAB, with simple selection by colormap length and scheme name. Alternatively the scheme name can be preselected, after which only the colormap length is required to define an output colormap.
BREWERMAP provides all ColorBrewer 2.0 colorschemes for MATLAB, with simple selection by colormap length and scheme name. Alternatively the scheme name can be preselected, after which only the colormap length is required to define an output colormap.

BREWERMAP is compatible with all MATLAB functions that require a colormap function. The function consists of just one M-file that provides all of the ColorBrewer colorschemes (no mat file, no third party files, no file-clutter!). Downsampling or interpolation or repetition of the nodes occurs automatically, if required.

Expand Down Expand Up @@ -30,8 +30,8 @@ BREWERMAP is compatible with all MATLAB functions that require a colormap functi
plotm(lat, long, 'k')

% Plot a scheme's RGB values:
rgbplot(brewermap(NaN, 'Blues')) % standard
rgbplot(brewermap(NaN,'-Blues')) % reversed
rgbplot(brewermap(NaN, '+Blues')) % standard
rgbplot(brewermap(NaN, '-Blues')) % reversed

% View information about a colorscheme:
[~,num,typ] = brewermap(NaN,'Paired')
Expand Down Expand Up @@ -67,7 +67,7 @@ R2014b or later: BREWERMAP_VIEW can also update other axes' or figures' colormap
image(S.X)
brewermap_view(gca) % default = colormap

R2019b or later can also update other axes's or figures' line colororders in real time, for example:
R2019b or later: BREWERMAP_VIEW can also update other axes's or figures' line colororders in real time, for example:

plot(rand(7,7))
brewermap_view(gca,[],true) % colororder
Expand Down
32 changes: 18 additions & 14 deletions brewermap.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function [map,num,typ,scheme] = brewermap(N,scheme) %#ok<*ISMAT>
function [map,num,typ,scheme] = brewermap(N,scheme)
% The complete selection of ColorBrewer colorschemes/palettes (RGB colormaps).
%
% (c) 2014-2024 Stephen Cobeldick
%
% Returns any RGB colormap from the ColorBrewer colorschemes, especially
% Returns an RGB colormap from Cynthia Brewer's ColorBrewer 2.0 palettes,
% intended for mapping and plots with attractive, distinguishable colors.
%
%%% Basic Syntax:
Expand Down Expand Up @@ -58,7 +58,7 @@
% >> axis([-3,3,-3,3,-10,5])
%
%%% Plot a colorscheme's RGB values:
% >> rgbplot(brewermap(NaN, 'Blues')) % standard
% >> rgbplot(brewermap(NaN, '+Blues')) % standard
% >> rgbplot(brewermap(NaN, '-Blues')) % reversed
%
%%% View information about a colorscheme:
Expand All @@ -85,10 +85,10 @@
%
%% Input and Output Arguments %%
%
%%% Inputs:
%%% Inputs (**=default):
% N = NumericScalar, N>=0, an integer to specify the colormap length.
% = [], same length as MATLAB's inbuilt colormap functions.
% = NaN, same length as the defining RGB nodes (useful for line ColorOrder).
% = []**, map has the same length as MATLAB's inbuilt colormap functions.
% = NaN, map is exactly the defining RGB nodes (useful for line ColorOrder).
% scheme = CharRowVector or StringScalar, a ColorBrewer colorscheme name.
%
%%% Outputs:
Expand All @@ -104,7 +104,7 @@
persistent bmc scm txt
%
if isempty(bmc)
bmc = bmColors();
bmc = bmStruct();
end
%
if nargin==0
Expand All @@ -128,6 +128,7 @@
'SC:brewermap:N:NotScalarNumeric',err)
assert(isnan(N) || isreal(N)&&isfinite(N)&&fix(N)==N&&N>=0,...
'SC:brewermap:N:NotWholeRealNotNaN',err)
N = double(N);
end
if nargin<2
assert(~isempty(scm),...
Expand All @@ -138,13 +139,16 @@
scheme = bm1s2c(scheme);
assert(ischar(scheme)&&ndims(scheme)==2&&size(scheme,1)==1,...
'SC:brewermap:scheme:NotText',...
'Input <scheme> must be a character vector or a string scalar.')
'Input <scheme> must be a character vector or a string scalar.') %#ok<ISMAT>
end
else % preset
assert(nargin<2,...
'SC:brewermap:WrongArgumentNumber',...
'If calling with two inputs then the 1st input must be numeric.')
scheme = bm1s2c(N);
assert(ischar(scheme)&&ndims(scheme)==2&&size(scheme,1)==1,...
'SC:brewermap:N:NotText',...
'To preset: the scheme must be a character vector or a string scalar.')
'To preset: the scheme must be a character vector or a string scalar.') %#ok<ISMAT>
if strcmpi(scheme,'list')
map = {bmc.str};
num = [bmc.num];
Expand Down Expand Up @@ -201,7 +205,7 @@
case 'Sequential'
map = interp1(1:num,map,ido,'pchip');
otherwise
error('SC:brewermap:Interp:UnknownType','Cannot interpolate this colorscheme type.')
error('SC:brewermap:Interp:UnknownType','Unknown colorscheme type.')
end
%
end
Expand Down Expand Up @@ -237,7 +241,7 @@
arr = arr{1};
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%bmGammaCor
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%bm1s2c
function [idx,itp] = bmIndex(N,num,typ)
% Ensure exactly the same colors as the online ColorBrewer colorschemes.
%
Expand Down Expand Up @@ -299,7 +303,7 @@
%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%bmIndex
function bmc = bmColors()
function bmc = bmStruct()
% Return a structure of all colorschemes: name, scheme type, RGB values, number of nodes.
% Order: sorted first by <typ>, then case-insensitive sorted by <str>.
bmc(35).str = 'YlOrRd';
Expand Down Expand Up @@ -417,12 +421,12 @@
case 'Sequential'
bmc(k).num = 9;
otherwise
error('SC:brewermap:Colors:UnknownType','Unknown colorscheme type.')
error('SC:brewermap:Struct:UnknownType','Unknown colorscheme type.')
end
end
%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%bmColors
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%bmStruct
%
% Code and Implementation:
% Copyright (c) 2014-2024 Stephen Cobeldick
Expand Down
2 changes: 1 addition & 1 deletion brewermap_plot.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function brewermap_plot()
% Simple plot of all ColorBrewer colorscheme nodes in one figure.
% Simple figure of all ColorBrewer 2.0 colorschemes' defining nodes.
%
% (c) 2014-2024 Stephen Cobeldick
%
Expand Down
69 changes: 35 additions & 34 deletions brewermap_view.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
function [map,num,typ,scheme] = brewermap_view(N,scheme,isco) %#ok<*ISMAT>
% An interactive figure for ColorBrewer colorscheme selection (RGB colormaps)
function [map,num,typ,scheme] = brewermap_view(N,scheme,isco)
% Creates an interactive figure for viewing ColorBrewer colormaps. With demo!
%
% (c) 2014-2024 Stephen Cobeldick
%
% View Cynthia Brewer's ColorBrewer colorschemes in a figure.
%
% View Cynthia Brewer's ColorBrewer 2.0 colorschemes/palettes in a figure:
% * Two colorbars give the colorscheme in color and grayscale.
% * A button toggles between 3D-cube and 2D-lineplot of the RGB values.
% * A button toggles an endless demo cycle through the colorschemes.
% * A button reverses the colormap.
% * 35 buttons select any ColorBrewer colorscheme.
% * Text with the colorscheme's type (Diverging/Qualitative/Sequential)
% * Text with the colorscheme's number of nodes (defining colors).
% * Thirty-five buttons select any ColorBrewer colorscheme.
% * Text with the colorscheme's type (Diverging/Qualitative/Sequential).
% * Text with the colorscheme's number of nodes (i.e. defining colors).
%
%%% Syntax:
% brewermap_view
Expand All @@ -23,21 +22,21 @@
% [map,num,typ] = brewermap_view(...)
%
% Calling the function with an output argument blocks MATLAB execution until
% the figure is deleted: the final colormap and colorscheme are then returned.
% the figure is deleted: the final colormap and parameters are then returned.
%
%% Adjust Colormaps or Colororders of Figures or Axes %%
%
% Only R2014b or later. Provide axes or figure handles as the first input
% and their COLORMAP() will be updated in real-time by BREWERMAP_VIEW.
% For R2019b or later: set the 3rd input to TRUE to set the COLORORDER().
% For R2014b or later: provide axes or figure handles as the first input
% and their COLORMAP will be updated in real-time by BREWERMAP_VIEW.
% For R2019b or later: set the 3rd input to TRUE to update COLORORDER.
%
%%% Examples:
%
% >> S = load('spine');
% >> image(S.X)
% >> brewermap_view(gca) % colormap
%
% >> plot(rand(7,7))
% >> plot(rand(5,7))
% >> brewermap_view(gca,[],true) % colororder
%
%% Input and Output Arguments %%
Expand All @@ -48,7 +47,9 @@
% = NaN, same length as the defining RGB nodes (useful for Line ColorOrder).
% = Array of axes or figure handles. R2014b or later only.
% scheme = CharRowVector or StringScalar, a ColorBrewer colorscheme name.
% isco = true/false** to set the axes or figures colororder/colormap.
% = []** randomly selects a colorscheme.
% isco = LogicalScalar, true/false** updates the colororder/colormap of
% the provided axes or figure handles. R2019b or later only.
%
%%% Outputs (these block code execution until the figure is closed!):
% map = NumericMatrix, the colormap defined when the figure is closed.
Expand Down Expand Up @@ -76,31 +77,31 @@
fnh = @colormap;
end
%
err = 'First input must be a real positive scalar numeric or [] or NaN.';
err = 'First input <N> must be a real positive scalar numeric or [] or NaN.';
if nargin==0 || isnumeric(N)&&isequal(N,[])
N = dfn;
elseif isnumeric(N)
assert(isscalar(N),'SC:brewermap_view:NotScalarNumeric',err)
assert(isnan(N)||isreal(N)&&isfinite(N)&&fix(N)==N&&N>0,...
'SC:brewermap_view:NotRealPositiveNotNaN',err)
assert(isscalar(N),'SC:brewermap_view:N:NotScalarNumeric',err)
assert(isreal(N)&&isfinite(N)&&fix(N)==N&&N>0||isnan(N),...
'SC:brewermap_view:N:NotRealPositiveIntegerNorNaN',err)
N = double(N);
elseif all(ishghandle(N(:))) % R2014b or later
assert(isgraphics(N(:),'axes')|isgraphics(N(:),'figure'),...
'SC:brewermap_view:NotAxesNorFigureHandles',...
'First input may be an array of figure or axes handles.')
assert(all(isgraphics(N(:),'axes')|isgraphics(N(:),'figure')),...
'SC:brewermap_view:N:NotAxesNorFigureHandles',...
'First input <N> may be an array of figure or axes handles.')
hgv = N(:);
nmr = arrayfun(@(h)size(fnh(h),1),hgv);
N = nmr(1);
else
error('SC:brewermap_view:UnsupportedInput',err)
error('SC:brewermap_view:N:UnsupportedInput',err)
end
%
[mcs,nmn,pyt] = brewermap('list');
%
% Check BREWERMAP output:
tmp = find([any(diff(double(char(pyt)),1),2);1]);
assert(isequal(tmp,[9;17;35]),'SC:brewermap_view:SchemeSequence',...
'The BREWERMAP function returned an unexpected scheme sequence.')
assert(isequal(tmp,[9;17;35]),'SC:brewermap_view:BREWERMAP:OutputOrder',...
'The BREWERMAP function returned an unexpected colorscheme sequence.')
%
if nargin<2 || isnumeric(scheme)&&isequal(scheme,[])
% Default pseudo-random colorscheme:
Expand All @@ -110,8 +111,8 @@
% Parse input colorscheme:
scheme = bmv1s2c(scheme);
assert(ischar(scheme) && ndims(scheme)==2 && size(scheme,1)==1,...
'SC:brewermap_view:NotCharacterVector',...
'Second input <scheme> must be a 1xN char vector.')
'SC:brewermap_view:scheme:NotCharacterVector',...
'Second input <scheme> must be a 1xN char vector.') %#ok<ISMAT>
% Check if a reversed colormap was requested:
isr = strncmp(scheme,'-',1) || strncmp(scheme,'*',1);
isd = strncmp(scheme,'+',1) || isr;
Expand All @@ -130,7 +131,7 @@
stp = [1,10]; % [minor,major]
%
% Define the 3D cube axis order:
xyz = 'RGB'; % choose order
xyz = 'RGB'; % specify the order here.
[~,xyz] = ismember(xyz,'RGB');
%
if new % Create a new figure.
Expand Down Expand Up @@ -200,7 +201,7 @@
cbIm(2) = image('Parent',cbAx(2), 'CData',C);
cbIm(1) = image('Parent',cbAx(1), 'CData',C);
%
% Add parameter slider, listener, and corresponding text:
% Add parameter sliders, listeners, and corresponding text:
sv = mean([lbd,rbd],2);
pTxt = uicontrol(figH,'Style','text', 'Units','normalized',...
'Position',[bgw+2*gap,bgh-2*bth-gap,btw,bth], 'String','X');
Expand Down Expand Up @@ -230,10 +231,10 @@
%
function str = bmvName()
% Generate the colorscheme name.
drn = '+-';
pfx = '+-';
ids = strcmpi(scm,mcs);
if any(ids)
str = [drn(1+isr),mcs{ids}];
str = [pfx(1+isr),mcs{ids}];
else % will throw an error in BREWERMAP:
str = scm;
end
Expand All @@ -247,12 +248,12 @@ function bmvUpDt()
mag = map*[0.298936;0.587043;0.114021];
%
% Update colorbar values:
set(cbAx, 'YLim', [0,abs(N)+(N==0)]+0.5);
set(cbAx, 'YLim',[0,abs(N)+(N==0)]+0.5);
set(cbIm(1), 'CData',reshape(map,[],1,3))
set(cbIm(2), 'CData',repmat(mag,[1,1,3]))
set(cbIm(2), 'CData', repmat(mag,[1,1,3]))
%
% Update 2D line / 3D patch values:
if get(is2D, 'Value') % 2D
if get(is2D, 'Value')
set(ln2D, 'XData',linspace(0,1,abs(N)));
set(ln2D,{'YData'},num2cell([map,mag],1).');
else % 3D
Expand All @@ -273,7 +274,7 @@ function bmvUpDt()
% Update parameter value text:
set(pTxt(1), 'String',sprintf('N = %.0f',N));
%
% Update external axes/figure:
% Update external axes/figure colormaps/colororders:
nmr(1) = N;
for k = 1:numel(hgv)
fnh(hgv(k),brewermap(nmr(k),bmvName()));
Expand Down Expand Up @@ -388,7 +389,7 @@ function bmvDemo(h,~)
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%bmv1s2c
%
% Copyright (c) 2014-2022 Stephen Cobeldick
% Copyright (c) 2014-2024 Stephen Cobeldick
%
% Licensed under the Apache License, Version 2.0 (the "License");
% you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 1773eb1

Please sign in to comment.