|
1 | 1 |
|
2 | 2 | function [ RGBOUT ] = colourscale( varargin ) |
3 | | -%COLOURSCALEPLOT Make colourful, nice-looking plots |
| 3 | +%COLOURSCALE Make colourful, nice-looking plots |
4 | 4 | % This function takes the current figure and applies a series of colours |
5 | | -% to each "line". These colours are a spectrum of saturations and intensities for |
6 | | -% a given colour hue. |
| 5 | +% to each "line". These colours are a spectrum of saturations and |
| 6 | +% intensities for a given colour hue. |
7 | 7 | % |
8 | 8 | % COLOURSCALE(...,'hue',H) |
9 | | -% Use hue H for colour scheme. Since H is a standard "HSV" hue, it varies |
10 | | -% from zero to one, where approximately: |
| 9 | +% Use hue H for colour scheme (default 0.2). |
| 10 | +% H is a standard "HSV" hue, from zero to one, where approx.: |
11 | 11 | % H=0.0 - red |
12 | 12 | % H=0.1 - orange |
13 | 13 | % H=0.15 - yellow |
|
19 | 19 | % H=1.0 - red again |
20 | 20 | % |
21 | 21 | % COLOURSCALE(...,'chroma',C) |
22 | | -% Use chroma C for colour scheme. Chroma appears to be a nonlinear |
23 | | -% parameter with sensible maximum; values around 40 (dull) to 100 (bright) |
24 | | -% appear to be best, although higher than this produces brighter colours |
25 | | -% they also start clipping what is possible represent in RGB. |
| 22 | +% Use chroma C for colour scheme (default 70). |
| 23 | +% Chroma appears to be a nonlinear parameter with sensible maximum; values |
| 24 | +% around 40 (dull) to 100 (bright) appear to be best, although higher than |
| 25 | +% this produces brighter colours they also start clipping what is possible |
| 26 | +% represent in RGB. |
26 | 27 | % |
27 | 28 | % COLOURSCALE(...,'lumin',[l_N L_N]) |
28 | 29 | % Use [l_N L_N] as the range for lumin values to vary over. Lumin values |
|
52 | 53 | % integer. |
53 | 54 | % |
54 | 55 | % COLOURSCALE(...,'permute',P) |
55 | | -% By default the lines are coloured in the order in which they |
56 | | -% were plot. This order can be changed by specifying a permutation |
57 | | -% of the order using indexing, such as in a four-plot graph: |
| 56 | +% By default the lines are coloured in the order in which they were plot. |
| 57 | +% This order can be changed by specifying a permutation of the order using |
| 58 | +% indexing, such as in a four-plot graph: |
58 | 59 | % colourscale(...,'permute',[1 3 2 4]) |
59 | 60 | % |
60 | | -% If the 'UserData' for a data line is 'colourscale:ignore', then |
61 | | -% it will not be included in the COLOURSCALE colouring. |
| 61 | +% If the 'UserData' for a data line is 'colourscale:ignore', then |
| 62 | +% it will not be included in the COLOURSCALE colouring. |
62 | 63 | % |
63 | | -% RGBOUT = colourscale( ... ) will simply return the colours that |
64 | | -% would be used, but it will NOT attempt to colour the plot. |
| 64 | +% RGB = COLOURSCALE(...) |
| 65 | +% As above, and also returns the colours in an array. |
| 66 | + |
| 67 | + |
| 68 | +%% COLOURSCALE v0.1 |
65 | 69 | % |
| 70 | +% Copyright (c) 2017-2018 Will Robertson |
| 71 | +% All rights reserved. |
| 72 | +% Licence (BSD) appended. |
66 | 73 | % |
67 | 74 | % Please report bugs and feature requests for |
68 | 75 | % this package at the development repository: |
69 | | -% <http://github.com/wspr/matlabpkg/> |
70 | | -% |
71 | | -% COLOURSCALE v0.1 Will Robertson |
72 | | -% Licence appended. |
| 76 | +% <http://github.com/wspr/matlab-plot-tools/> |
| 77 | + |
| 78 | + |
| 79 | +%% Option parsing |
73 | 80 |
|
74 | 81 | p = inputParser; |
75 | 82 | p.addOptional('hue',0.2); |
|
78 | 85 | p.addOptional('permute',[]); |
79 | 86 | p.addOptional('lumin',{[65 65] [50 80] [40 80] [30 90]}); |
80 | 87 | p.addOptional('linewidth',[]); |
81 | | - |
82 | 88 | p.parse(varargin{:}); |
83 | 89 |
|
84 | 90 | hue = p.Results.hue; |
85 | 91 | chroma = p.Results.chroma; |
86 | | -series = p.Results.repeat; |
| 92 | +Nseries = p.Results.repeat; |
87 | 93 | permute = p.Results.permute; |
88 | 94 | lumin = p.Results.lumin; |
89 | 95 | lw_range = p.Results.linewidth; |
90 | 96 |
|
| 97 | + |
| 98 | +%% Option massaging |
| 99 | + |
91 | 100 | if ~isempty(lw_range) |
92 | 101 | if numel(lw_range) == 1 |
93 | 102 | lw_range = lw_range([1 1]); |
|
100 | 109 |
|
101 | 110 | ch = findobj(gca,'Type','line','-not','UserData','colourscale:ignore'); |
102 | 111 |
|
103 | | -Nch = length(ch); |
104 | | -Ncol = Nch/series; |
| 112 | +Nch = numel(ch); |
| 113 | +Ncol = Nch/Nseries; |
| 114 | +Nlum = numel(lumin); |
| 115 | + |
105 | 116 | if round(Ncol) ~= Ncol |
106 | 117 | % Each set of data series must be the same length to avoid rounding problems!! |
107 | 118 | disp(['There are ',num2str(Nch),' data series']) |
108 | 119 | error('There must be an integer multiple of specified data series in the figure.') |
109 | 120 | end |
110 | 121 |
|
111 | | -Nlum = numel(lumin); |
| 122 | +if isempty(permute) |
| 123 | + permute = 1:Nch; |
| 124 | +else |
| 125 | + if ~isequal(sort(permute),1:Nch) |
| 126 | + error('2nd argument must be a permutation of 1:N where N is the number of colours.'); |
| 127 | + end |
| 128 | +end |
| 129 | + |
| 130 | + |
| 131 | +%% Calculate colours |
112 | 132 |
|
113 | 133 | % indexing into lumin values needs a trick. |
114 | 134 | % let's say we have lumin values of [65 50 40 30]; |
|
135 | 155 | end |
136 | 156 | rgb = rgb/255; |
137 | 157 |
|
138 | | -if isempty(permute) |
139 | | - permute = 1:Nch; |
140 | | -else |
141 | | - if ~isequal(sort(permute),1:Nch) |
142 | | - error('2nd argument must be a permutation of 1:N where N is the number of colours.'); |
143 | | - end |
144 | | -end |
145 | 158 |
|
146 | | -if nargout == 0 |
147 | | - for ii = 1:Nch |
148 | | - ind = mod(ii-1,Ncol)+1; |
149 | | - if isequal(get(ch(ii),'type'),'line') |
150 | | - if isempty(lw_range) |
151 | | - set(ch(permute(ii)),... |
152 | | - 'Color',rgb(ind,:),... |
153 | | - 'UserData','colourscale:ignore') |
154 | | - else |
155 | | - set(ch(permute(ii)),... |
156 | | - 'Color',rgb(ind,:),... |
157 | | - 'LineWidth',lw(ind),... |
158 | | - 'UserData','colourscale:ignore') |
159 | | - end |
160 | | - end |
161 | | - if isequal(get(ch(ii),'type'),'surface') |
| 159 | +%% Assign colours |
| 160 | + |
| 161 | +for ii = 1:Nch |
| 162 | + ind = mod(ii-1,Ncol)+1; |
| 163 | + if isequal(get(ch(ii),'type'),'line') |
| 164 | + if isempty(lw_range) |
| 165 | + set(ch(permute(ii)),... |
| 166 | + 'Color',rgb(ind,:),... |
| 167 | + 'UserData','colourscale:ignore') |
| 168 | + else |
162 | 169 | set(ch(permute(ii)),... |
163 | | - 'FaceColor',rgb(ind,:),... |
164 | | - 'EdgeColor',rgb(ind,:),... |
| 170 | + 'Color',rgb(ind,:),... |
| 171 | + 'LineWidth',lw(ind),... |
165 | 172 | 'UserData','colourscale:ignore') |
166 | 173 | end |
167 | 174 | end |
168 | | -else |
| 175 | + if isequal(get(ch(ii),'type'),'surface') |
| 176 | + set(ch(permute(ii)),... |
| 177 | + 'FaceColor',rgb(ind,:),... |
| 178 | + 'EdgeColor',rgb(ind,:),... |
| 179 | + 'UserData','colourscale:ignore') |
| 180 | + end |
| 181 | +end |
| 182 | + |
| 183 | + |
| 184 | +%% Fin |
| 185 | + |
| 186 | +if nargout > 0 |
169 | 187 | RGBOUT = rgb; |
170 | 188 | end |
171 | 189 |
|
172 | 190 | return |
173 | 191 |
|
174 | | -% Copyright (c) 2015-2016, Will Robertson, will at wspr dot io |
175 | | -% All rights reserved. |
| 192 | + |
| 193 | +%% Licence |
176 | 194 | % |
177 | 195 | % Distributed under the BSD licence in accordance with the wishes of the |
178 | 196 | % Matlab File Exchange. |
|
0 commit comments