-
Notifications
You must be signed in to change notification settings - Fork 0
/
clab_rainbow_make.m
376 lines (293 loc) · 11.9 KB
/
clab_rainbow_make.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
function cmap = clab_rainbow_make(n, attributes, dbg)
% -------------------------------------------------------------------------
% Default inputs
if nargin<3 || isempty(dbg)
dbg = 0; % Whether to output information and figures
end
if nargin<2 || isempty(attributes)
attributes = 'greenmid'; % Colormap type option
end
if nargin<1 || isempty(n)
n = size(get(gcf,'colormap'),1); % Number of colours in the colormap
end
% -------------------------------------------------------------------------
% Main function body
attrbreaks = strfind(attributes,':');
if isempty(attrbreaks)
typ = attributes;
paramset = '1';
else
typ = attributes(1:attrbreaks(1)-1);
paramset = attributes(attrbreaks(1)+1:end);
end
switch lower(typ)
case 'smooth'
cmap = clab_rainbow_make_smooth(n, paramset, dbg);
case 'greenmid'
cmap = clab_rainbow_make_greenmid(n, paramset, dbg);
otherwise
error('Unfamiliar colormap attribute: %s',typ);
end
end
function params = get_rainbow_ellipse_params(paramset)
% Declares parameters for the (partial) ellipse in CIELab which is used to
% generate the colormap
switch paramset
case '1'
params.use_uplab = false;
params.P = [-60 49 88]; % Green point
params.C = [301.0341 -123.2044 -32.2823]; % Centre
params.a = 400; % Major axis coef
params.b = 106; % Minor axis coef
params.theta = -25.5/180*pi; % Rotation of ellipse wrt ab-plane of L*a*b*
params.phi = -17.5/180*pi; % L* Inclination of major axis
params.psi = 1.7/180*pi; % L* Inclination of minor axis
params.U = [ cos(params.theta) sin(params.theta) sin(params.phi)]; % Major axis
params.V = [-sin(params.theta) cos(params.theta) sin(params.psi)]; % Minor axis
params.t_start = 4.2; % Good start point
params.t_end = 2.3; % Good end point
case '2'
params.use_uplab = false;
params.P = [-41 37 91];
params.C = [131.8926 -41.7917 15.2377];
params.a = 190;
params.b = 89;
params.theta = -24.5/180*pi;
params.phi = -23.5/180*pi;
params.psi = 0/180*pi;
params.U = [ cos(params.theta) sin(params.theta) sin(params.phi)]; % Major axis
params.V = [-sin(params.theta) cos(params.theta) sin(params.psi)]; % Minor axis
params.t_start = 4.6; % Good start point
params.t_end = 2.05; % Good end point
otherwise
error('Unfamiliar parameter set');
end
end
function [btsp_t_srt, btsp_t_end] = find_ellipse_ends(paramset)
% Finds the limiting points which are only just inside the ellipse
params = get_rainbow_ellipse_params(paramset);
rgbgamut = fetch_cielchab_gamut('srgb');
% First generate a ton of points to find the end points of theta
n = 100001;
t = linspace(params.t_start, params.t_end, n);
x = params.C(1) + params.a * cos(t) * params.U(1) + params.b * sin(t) * params.V(1);
y = params.C(2) + params.a * cos(t) * params.U(2) + params.b * sin(t) * params.V(2);
z = params.C(3) + params.a * cos(t) * params.U(3) + params.b * sin(t) * params.V(3);
Lab = [z' x' y'];
TF = isingamut(Lab,rgbgamut,'Lab');
% Find edges of gamut
I_srt = find(TF,1,'first');
I_end = find(TF,1,'last');
% Go back by a Euclidian distance of 1 to be sure we're in gamut
de_srt = sqrt(sum(bsxfun(@minus,Lab(I_srt:I_end,:),Lab(I_srt,:)).^2,2));
de_end = sqrt(sum(bsxfun(@minus,Lab(I_srt:I_end,:),Lab(I_end,:)).^2,2));
btsp_t_srt = t(I_srt-1+find(de_srt>1,1,'first'));
btsp_t_end = t(I_srt-1+find(de_end>1,1,'last'));
end
function ciebow_cmap = clab_rainbow_make_smooth(n_target, paramset, dbg)
params = get_rainbow_ellipse_params(paramset);
rgbgamut = fetch_cielchab_gamut('srgb');
% First generate a ton of points to find the end points of theta
[btsp_t_srt, btsp_t_end] = find_ellipse_ends(paramset);
% Now generate a ton of points in this range
n = 100001;
t = linspace(btsp_t_srt, btsp_t_end, n);
x = params.C(1) + params.a * cos(t) * params.U(1) + params.b * sin(t) * params.V(1);
y = params.C(2) + params.a * cos(t) * params.U(2) + params.b * sin(t) * params.V(2);
z = params.C(3) + params.a * cos(t) * params.U(3) + params.b * sin(t) * params.V(3);
Lab = [z' x' y'];
TF = isingamut(Lab,rgbgamut,'Lab');
% Check all points are now inside
% if dbg;
% fprintf('This need to be equal %d = %d\t%d\n',sum(TF),length(TF),(sum(TF)/length(TF)));
% end
if sum(TF)~=length(TF)
warning('Not all points in gamut');
end
% Using simple 1931 color difference metric
% Find Euclidian 2norm distance between each set of consequtive points
Lab_dif = diff(Lab,1,1);
Lab_inv = sqrt(sum(Lab_dif.^2,2));
% % % Using more up to date color difference metric
% % % https://en.wikipedia.org/wiki/Color_difference#CIEDE2000
% %
% % My implementation (doesn't work)
% % LCh = applycform(Lab,makecform('lab2lch'));
% % Lab_inv = ciede(LCh(1:end-1,:),LCh(2:end,:));
% %
% % From Westland and Ripamonti (2004) - FEX:40640
% Lab_inv = cie00de(Lab(1:end-1,:),Lab(2:end,:));
% Hence find length of complete arc
arclen_full = sum(Lab_inv);
% Find 256 points equally spaced out of the 100000 we generated
% n_target = 256;
arclen_inv_target = arclen_full./(n_target-1);
% Match target to the arclength NOT the euclidean length between points
% This is easier to code
Lab_inv_runtot = [0; cumsum(Lab_inv)];
arclen_targets = arclen_inv_target * (0:(n_target-1));
t_new = interp1(Lab_inv_runtot, t, arclen_targets);
t_new(1) = btsp_t_srt;
t_new(end) = btsp_t_end;
x = params.C(1) + params.a * cos(t_new) * params.U(1) + params.b * sin(t_new) * params.V(1);
y = params.C(2) + params.a * cos(t_new) * params.U(2) + params.b * sin(t_new) * params.V(2);
z = params.C(3) + params.a * cos(t_new) * params.U(3) + params.b * sin(t_new) * params.V(3);
Lab = [z' x' y'];
ciebow_cmap = soft_lab2rgb(Lab, params.use_uplab);
% debugging figures
if dbg;
img = repmat(ciebow_cmap,[1 1 20]);
img = permute(img,[1 3 2]);
figure;
imagesc(img);
axis xy;
[TF,P2] = isingamut(Lab,rgbgamut,'Lab');
% disp(sum(TF)/length(TF))
figure;
hold on;
plot3(Lab(:,2), Lab(:,3), Lab(:,1), 'bx')
plot3(P2(:,2) , P2(:,3) , P2(:,1) , 'r-')
xlabel('a');
ylabel('b');
zlabel('L');
view(0,90);
title('Rotate to see L');
figure;
hold on;
plot3(Lab(:,2), Lab(:,3), Lab(:,1), 'kx-');
% Get a mesh version of the gamut
if ~isfield(rgbgamut,'lchmesh')
rgbgamut.lchmesh = make_gamut_mesh(rgbgamut);
end
L = rgbgamut.lchmesh.Lgrid([1:4:(end-1) 1],[1:4:(end-1) end]);
c = rgbgamut.lchmesh.cgrid([1:4:(end-1) 1],[1:4:(end-1) end]);
h = rgbgamut.lchmesh.hgrid([1:4:(end-1) 1],[1:4:(end-1) end])/180*pi;
a = c.*cos(h);
b = c.*sin(h);
CMAP = soft_lab2rgb([L(:) a(:) b(:)], params.use_uplab);
hs = mesh(a,b,L,reshape(CMAP,[size(L) 3]));
set(hs,'FaceColor','none');
set(gca,'Color',[0.4663 0.4663 0.4663]);
set(gca,'XLim',[-150 150],'YLim',[-150 150],'ZLim',[0 100]);
xlabel('a*')
ylabel('b*')
zlabel('L*')
plot_labcurve_rgbgamut(Lab)
end
end
function ciebow_cmap = clab_rainbow_make_greenmid(n_target, paramset, dbg)
% Option to have same amount of red as blue
% This is a very simple method used where the curve is split in two and
% there are different Delta E values between colours in each.
% It would be better to have the rate change sigmoidally instead
% Mid point is at t=pi
params = get_rainbow_ellipse_params(paramset);
rgbgamut = fetch_cielchab_gamut('srgb');
% First generate a ton of points to find the end points of theta
[btsp_t_srt, btsp_t_end] = find_ellipse_ends(paramset);
% Now generate a ton of points in this range
n = 100001;
t1 = linspace(btsp_t_srt, pi, n);
x1 = params.C(1) + params.a * cos(t1) * params.U(1) + params.b * sin(t1) * params.V(1);
y1 = params.C(2) + params.a * cos(t1) * params.U(2) + params.b * sin(t1) * params.V(2);
z1 = params.C(3) + params.a * cos(t1) * params.U(3) + params.b * sin(t1) * params.V(3);
t2 = linspace(pi, btsp_t_end, n);
x2 = params.C(1) + params.a * cos(t2) * params.U(1) + params.b * sin(t2) * params.V(1);
y2 = params.C(2) + params.a * cos(t2) * params.U(2) + params.b * sin(t2) * params.V(2);
z2 = params.C(3) + params.a * cos(t2) * params.U(3) + params.b * sin(t2) * params.V(3);
Lab1 = [z1' x1' y1'];
Lab2 = [z2' x2' y2'];
% Check all points are now inside
TF = isingamut([Lab1;Lab2],rgbgamut,'Lab');
% if dbg;
% fprintf('This need to be equal %d = %d\t%d\n',sum(TF),length(TF),(sum(TF)/length(TF)));
% end
if sum(TF)~=length(TF)
warning('Not all points in gamut');
end
% Using simple 1931 color difference metric
% Find Euclidian 2norm distance between each set of consequtive points
Lab1_dif = diff(Lab1,1,1);
Lab2_dif = diff(Lab2,1,1);
Lab1_inv = sqrt(sum(Lab1_dif.^2,2));
Lab2_inv = sqrt(sum(Lab2_dif.^2,2));
% % % Using more up to date color difference metric
% % % https://en.wikipedia.org/wiki/Color_difference#CIEDE2000
% %
% % My implementation (doesn't work)
% % LCh1 = applycform(Lab1,makecform('lab2lch'));
% % LCh2 = applycform(Lab2,makecform('lab2lch'));
% % Lab1_inv = ciede(LCh1(1:end-1,:),LCh1(2:end,:)); % Coded by me
% % Lab2_inv = ciede(LCh2(1:end-1,:),LCh2(2:end,:)); % Coded by me
% %
% % From Westland and Ripamonti (2004) - FEX:40640
% Lab1_inv = cie00de(Lab1(1:end-1,:),Lab1(2:end,:)); % From Westland and Ripamonti (2004)
% Lab2_inv = cie00de(Lab2(1:end-1,:),Lab2(2:end,:)); % From Westland and Ripamonti (2004)
% Hence find length of complete arc
arclen_blu = sum(Lab1_inv);
arclen_red = sum(Lab2_inv);
% Find 256 points equally spaced out of the 100000 we generated
% n_target = 256;
if mod(n_target,2);
n_each = (n_target-1)/2;
else
n_each = n_target/2;
end
arclen_inv_target_blu = arclen_blu./n_each;
arclen_inv_target_red = arclen_red./n_each;
% Match target to the arclength NOT the euclidean length between points
% This is easier to code
Lab1_inv_runtot = [0; cumsum(Lab1_inv)];
Lab2_inv_runtot = [0; cumsum(Lab2_inv)];
arclen_targets_blu = arclen_inv_target_blu * (0:n_each);
arclen_targets_red = arclen_inv_target_red * (0:n_each);
t1_new = interp1(Lab1_inv_runtot, t1, arclen_targets_blu);
t2_new = interp1(Lab2_inv_runtot, t2, arclen_targets_red);
% If even number requested, there is an extra colour. Sorry.
t_new = [btsp_t_srt t1_new(2:end-1) pi t2_new(2:end-1) btsp_t_end];
x = params.C(1) + params.a * cos(t_new) * params.U(1) + params.b * sin(t_new) * params.V(1);
y = params.C(2) + params.a * cos(t_new) * params.U(2) + params.b * sin(t_new) * params.V(2);
z = params.C(3) + params.a * cos(t_new) * params.U(3) + params.b * sin(t_new) * params.V(3);
Lab = [z' x' y'];
ciebow_cmap = soft_lab2rgb(Lab, params.use_uplab);
% debugging figures
if dbg;
img = repmat(ciebow_cmap,[1 1 20]);
img = permute(img,[1 3 2]);
figure;
imagesc(img);
axis xy;
[TF,P2] = isingamut(Lab,rgbgamut,'Lab');
% disp(sum(TF)/length(TF))
figure;
hold on;
plot3(Lab(:,2), Lab(:,3), Lab(:,1), 'kx')
plot3(P2(:,2) , P2(:,3) , P2(:,1) , 'r-')
xlabel('a');
ylabel('b');
zlabel('L');
view(0,90);
title('Rotate to see L')
figure;
hold on;
plot3(Lab(:,2), Lab(:,3), Lab(:,1), 'kx-');
% Get a mesh version of the gamut
if ~isfield(rgbgamut,'lchmesh')
rgbgamut.lchmesh = make_gamut_mesh(rgbgamut);
end
L = rgbgamut.lchmesh.Lgrid([1:4:(end-1) 1],[1:4:(end-1) end]);
c = rgbgamut.lchmesh.cgrid([1:4:(end-1) 1],[1:4:(end-1) end]);
h = rgbgamut.lchmesh.hgrid([1:4:(end-1) 1],[1:4:(end-1) end])/180*pi;
a = c.*cos(h);
b = c.*sin(h);
CMAP = soft_lab2rgb([L(:) a(:) b(:)], params.use_uplab);
hs = mesh(a,b,L,reshape(CMAP,[size(L) 3]));
set(hs,'FaceColor','none');
set(gca,'Color',[0.4663 0.4663 0.4663]);
set(gca,'XLim',[-150 150],'YLim',[-150 150],'ZLim',[0 100]);
xlabel('a*')
ylabel('b*')
zlabel('L*')
plot_labcurve_rgbgamut(Lab)
end
end