Skip to content

Commit 05c54d9

Browse files
committed
colourscale improvements
1 parent b3710f7 commit 05c54d9

File tree

2 files changed

+51
-21
lines changed

2 files changed

+51
-21
lines changed

colourscale.m

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
% to each "line". These colours are a spectrum of saturations and intensities for
66
% a given colour hue.
77
%
8-
% COLOURSCALE('hue',H)
8+
% COLOURSCALE(...,'hue',H)
99
% Use hue H for colour scheme. Since H is a standard "HSV" hue, it varies
1010
% from zero to one, where approximately:
1111
% H=0.0 - red
@@ -18,6 +18,20 @@
1818
% H=0.85 - magenta
1919
% H=1.0 - red again
2020
%
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 to 100 appear to be
24+
% best, although higher than this produces brighter colours they also
25+
% start clipping what is possible represent in RGB.
26+
%
27+
% COLOURSCALE(...,'linewidth',[LW1 LW2])
28+
% If not specified, the plots take on their "natural" linewidth as default
29+
% or as specified by the user. If set to a two-element vector, the
30+
% linewidths of the lines will be set to vary linearly from LW1 to LW2 as
31+
% the plots change colour from dark to light. (This is useful as lighter
32+
% lines often need to be slightly thicker to remain visible compared to
33+
% darker lines.)
34+
%
2135
% COLOURSCALE(...,'repeat',N)
2236
% An optional argument specifies the number of times to use the
2337
% colour space: e.g., colourplot(2) will turn, in a graph with 6 data
@@ -53,7 +67,7 @@
5367
p.addOptional('permute',[]);
5468
p.addOptional('lumin_min',[65 50 40 30]);
5569
p.addOptional('lumin_max',[65 80 80 90]);
56-
p.addOptional('linewidth',[1 2]);
70+
p.addOptional('linewidth',[]);
5771

5872
p.parse(varargin{:});
5973

@@ -64,8 +78,11 @@
6478
lumin_min = p.Results.lumin_min;
6579
lumin_max = p.Results.lumin_max;
6680
lw_range = p.Results.linewidth;
67-
if numel(lw_range) == 1
68-
lw_range = lw_range([1 1]);
81+
82+
if ~isempty(lw_range)
83+
if numel(lw_range) == 1
84+
lw_range = lw_range([1 1]);
85+
end
6986
end
7087

7188
ch = findobj(gca,'Type','line','-not','UserData','colourplot:ignore');
@@ -96,7 +113,9 @@
96113
lmin = lumin_min(min([Ncol,Nlum]));
97114
lmax = lumin_max(min([Ncol,Nlum]));
98115

99-
lw = linspace(lw_range(1),lw_range(2),Ncol);
116+
if ~isempty(lw_range)
117+
lw = linspace(lw_range(1),lw_range(2),Ncol);
118+
end
100119

101120
hcl(:,1) = hue*360;
102121
hcl(:,2) = chroma;
@@ -120,10 +139,16 @@
120139
for ii = 1:Nch
121140
ind = mod(ii-1,Ncol)+1;
122141
if isequal(get(ch(ii),'type'),'line')
123-
set(ch(permute(ii)),...
124-
'Color',rgb(ind,:),...
125-
'LineWidth',lw(ind),...
126-
'UserData','colourplot:ignore')
142+
if isempty(lw_range)
143+
set(ch(permute(ii)),...
144+
'Color',rgb(ind,:),...
145+
'UserData','colourplot:ignore')
146+
else
147+
set(ch(permute(ii)),...
148+
'Color',rgb(ind,:),...
149+
'LineWidth',lw(ind),...
150+
'UserData','colourplot:ignore')
151+
end
127152
end
128153
if isequal(get(ch(ii),'type'),'surface')
129154
set(ch(permute(ii)),...
@@ -176,6 +201,8 @@
176201
% Code written by Nicholas J. Hughes, 2014, released under the following
177202
% licence.
178203
%
204+
% Some minor alternations by Will Robertson, 2018.
205+
%
179206
% The MIT License (MIT)
180207
%
181208
% Copyright (c) 2014 Nicholas J. Hughes
@@ -236,16 +263,18 @@
236263
b = gamma_correct((0.055648*X - 0.204043*Y + 1.057311*Z)/WHITE_Y);
237264

238265
% Round to integers and correct
239-
r = round(255 * r);
240-
g = round(255 * g);
241-
b = round(255 * b);
242-
r(r > 255) = 255;
243-
r(r < 0) = 0;
244-
g(g > 255) = 255;
245-
g(g < 0) = 0;
246-
b(b > 255) = 255;
247-
b(b < 0) = 0;
248266
rgb = [r, g, b];
267+
rgb = round(255 * rgb);
268+
269+
% if any(rgb(:) > 255)
270+
% warning('Colour outside RGB range; clipping.')
271+
% end
272+
% if any(rgb(:) < 0)
273+
% warning('Colour less than zero in RGB; clipping.')
274+
% end
275+
276+
rgb(rgb(:) > 255) = 255;
277+
rgb(rgb(:) < 0) = 0;
249278

250279

251280
function u = gamma_correct(u)

colourscale_test.m

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
p = 20;
1111

1212
% colour: (0-1)
13-
H = 0.6;
13+
HUE = 0.1;
14+
CHROMA = 60;
1415

1516
% linewidths:
1617
LW = [2 4]; % more exaggerated than default
@@ -22,8 +23,8 @@
2223
subplot(M,N,c); cla;
2324
xx = linspace(0,1,p);
2425
yy = repmat(1:c,[p 1])+rand(p,c);
25-
plot(xx,yy)
26-
colourscale('hue',H,'linewidth',LW);
26+
plot(xx,yy,'linewidth',5)
27+
colourscale('hue',HUE,'chroma',CHROMA,'linewidth',LW);
2728
title(['Number of lines: ',num2str(c)])
2829
axis tight
2930
end

0 commit comments

Comments
 (0)