Hello,
The final thing that prevents me from finally finishing up on refactoring and improving my filter is the need to create N colors that are farthest away from all the existing colors in a palette. So, I thought of this just now, and voronoi solution that is general, is super hard to attempt in G’MIC. General here means is that it works with s>3 too.
So, I went with a general solution involving subdivision:
rep_farthest_distance_int8_color:
foreach {
+colormap. 0,0,0 w:=w r. {$w+$1},1,1,100%,0
if !${-rep_check_isint_0_255[-1]} error inv_val_det fi
+rep_cartesian_product {vector(#s#-1,16)} +*. 17
$_cpus,1,1,2
repeat $1 {
eval.. :"
begin(
M=-inf;
idx=-inf;
);
m=inf;
repeat($w,p,
distance_away=norm(I[#1,p]-I);
if(distance_away<m,m=distance_away);
if(!distance_away,break(););
);
if(m>M,
M=m;
idx=x;
);
_0;
end_t(
I[#-1,t]=[M,idx];
);
"
V={p=argmax(crop(#-1,0,0,0,0,w#-1,1,1,1));I[#-2,i(#-1,p,0,0,1)]}
eval... :"
begin(
M=-inf;
idx=-inf;
V=["$V"];
);
m=inf;
repeat($w,p,
distance_away=norm(I[#1,p]-cut((I+V),0,255));
if(distance_away<m,m=distance_away);
if(!distance_away,break(););
);
if(m>M,
M=m;
idx=x;
);
_0;
end_t(
I[#-1,t]=[M,idx];
);
"
eval "
p=argmax(crop(#-1,0,0,0,0,w#-1,1,1,1));
I[#1,$w]=cut(I[#-3,i(#-1,p,0,0,1)]+["$V"],0,255);
"
w+=1
}
k[0,1]
crop. {w#0},100%
}
C:\Windows\System32>gmic +pal cmyk tic rep_farthest_distance_int8_color 200 toc
[gmic]./ Start G'MIC interpreter (v.3.7.3).
[gmic]./ Created palette(s) 'cmyk'.
[gmic]./ Initialize timer.
[gmic]./ Elapsed time: 1.812 s.
[gmic]./ Display images [0,1] = 'Cyan-Magenta-Yellow-Black, [colormap of 'Cyan-Magenta-Ye...'.
[0] = 'Cyan-Magenta-Yellow-Black':
size = (4,1,1,3) [48 b of float32].
data = (0,255,255,0^255,0,255,0^255,255,0,0).
min = 0, max = 255, mean = 127.5, std = 127.5, coords_min = (0,0,0,0), coords_max = (1,0,0,0).
[1] = '[colormap of 'Cyan-Magenta-Yellow-Black']_c1':
size = (200,1,1,3) [2400 b of float32].
data = (0,0,255,255,127,0,68,68,68,132,255,127,208,204,205,136,136,0,0,68,72,137,255,0,221,255,170,87,0,86,188,3,85,85,205,0,238,255,136,0,68,0,51,207,170,255,0,176,68,137,136,68,204,160,187,204,2,255,144,80,255,136,51,68, ... ,224,105,255,0,0,170,102,136,208,238,102,141,37,37,58,102,208,157,0,153,63,230,0,255,34,111,0,167,136,213,35,255,249,29,255,68,136,85,227,221,199,51,170,17,171,17,221,222,187,69,0,153,177,170,140,123,122,174,60,222,226,90,255,255).
min = 0, max = 255, mean = 128.138, std = 82.6873, coords_min = (0,0,0,0), coords_max = (2,0,0,0).
[gmic]./ End G'MIC interpreter.
This solution technically works, but it’s pretty slow.
Note: rep_cartesian_product can generate nD box as a 1D image. 16x16x16x16x16 is valid in it.