-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLUTconverterToHEXA_2.0.ijm
82 lines (64 loc) · 2.54 KB
/
LUTconverterToHEXA_2.0.ijm
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
// -------------------------------------------------------------------
// Written by: Patrice Mascalchi, DRVision
// Date: 2020-06
// Contact: patricem [at] drvtechnologies.com
// -------------------------------------------------------------------
// Generate a file as Aivia software format for custom LUT coloring.
// Important: input LUT image is expected to have an horizontal gradient.
// Final color coding is done on 256 levels as a maximum.
// v2.0: - includes a randomizer (for labelled mask, such as Aivia or StarDist output)
// -------------------------------------------------------------------
if (bitDepth() != 24) exit("LUT image should be RGB");
// Resize LUT image if more than 256 levels
if (getWidth() > 256) {
run("Scale...", "x=- y=- width=256 interpolation=Bilinear average create");
}
im = getTitle();
na = getString("Name of your custom LUT mapping", "Custom");
endCol = getBoolean("Do you want to replace last color by white?");
doRand = getBoolean("Do you want to randomize colors?");
nLev = getWidth();
run("Text Window...", "name="+na+" width=20 height=50");
print("["+na+"]", "\"" + na +"\"\n");
selectWindow(im);
gradCol = newArray(nLev);
for (n=0; n<nLev; n++) {
lev = round(n / (nLev-1) * 255);
//waitForUser(n +": "+ lev);
pix = getPixel(n, 1); //waitForUser("pix: "+ pix);
hexCol = toHex(pix); //waitForUser("hexCol: "+ hexCol);
hexColF = padMe(hexCol, 6); //waitForUser("hexColF: "+ hexColF);
if (lengthOf(hexColF) > 6) hexColF = substring(hexColF, 2);
// For last color
if (lev==255 && endCol) hexColF = "ffffff";
gradCol[n] = toUpperCase(hexColF);
}
// randomize
if (doRand) {
i = gradCol.length; // The number of items left to shuffle (loop invariant).
while (i > 1) {
k = randomInt(i); // 0 <= k < i.
i--; // i is now the last pertinent index;
temp = gradCol[i]; // swap gradCol[i] with gradCol[k] (does nothing if k==i).
gradCol[i] = gradCol[k];
gradCol[k] = temp;
}
}
// option to replace last color with white if not done before
if (lev<255 && endCol) print("["+na+"]", "100 FFFFFF");
// print final values
for (n=0; n<nLev; n++) {
lev = round(n / (nLev-1) * 255);
prefix = ""+ lev + " ";
print("["+na+"]", prefix + gradCol[n] +"\n");
}
selectWindow(na);
// --- functions -----------------------------
function padMe(txt, nb) {
while (lengthOf(txt) < nb) txt = "0" + txt;
return txt;
}
// returns a random number, 0 <= k < n
function randomInt(n) {
return n * random();
}