Skip to content

Commit 9dd01f9

Browse files
committed
Improve scripting of colors/Luts
1 parent 2508ca8 commit 9dd01f9

5 files changed

Lines changed: 65 additions & 21 deletions

File tree

‎src/main/java/sc/fiji/snt/Path.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ public PathNode(final SNTPoint point) {
170170
}
171171

172172
// Node color management
173-
public Color getNodeColor() { return nodeColor; }
174-
public boolean hasNodeColor() { return nodeColor != null; }
175-
public void setNodeColor(final Color color) { this.nodeColor = color; }
173+
public Color getColor() { return nodeColor; }
174+
public boolean hasColor() { return nodeColor != null; }
175+
public void setColor(final Color color) { this.nodeColor = color; }
176176

177177
// Radius management
178178
public double getRadius() { return radius; }
@@ -1976,12 +1976,12 @@ else if (getStartJoinsPoint() != null) {
19761976
*/
19771977
public void setNodeColors(final Color[] colors) {
19781978
if (colors == null) {
1979-
nodes.forEach(node -> node.setNodeColor(null));
1979+
nodes.forEach(node -> node.setColor(null));
19801980
} else if (colors.length != size()) {
19811981
throw new IllegalArgumentException("colors array must have as many elements as nodes");
19821982
} else {
19831983
for (int i = 0; i < colors.length; i++)
1984-
getNodeWithoutChecks(i).setNodeColor(colors[i]);
1984+
getNodeWithoutChecks(i).setColor(colors[i]);
19851985
}
19861986
}
19871987

@@ -1995,7 +1995,7 @@ public Color[] getNodeColors() {
19951995
// Direct array creation is faster than using streams!?
19961996
final Color[] colors = new Color[nodes.size()];
19971997
for (int i = 0; i < nodes.size(); i++)
1998-
colors[i] = nodes.get(i).getNodeColor();
1998+
colors[i] = nodes.get(i).getColor();
19991999
return colors;
20002000
}
20012001

@@ -2007,7 +2007,7 @@ public Color[] getNodeColors() {
20072007
* this path
20082008
*/
20092009
public Color getNodeColor(final int pos) {
2010-
return getNodeWithChecks(pos).getNodeColor();
2010+
return getNodeWithChecks(pos).getColor();
20112011
}
20122012

20132013
/**
@@ -2017,7 +2017,7 @@ public Color getNodeColor(final int pos) {
20172017
* @param pos the node position
20182018
*/
20192019
public void setNodeColor(final Color color, final int pos) {
2020-
getNodeWithChecks(pos).setNodeColor(color);
2020+
getNodeWithChecks(pos).setColor(color);
20212021
}
20222022

20232023
/**
@@ -2198,7 +2198,7 @@ public void setColor(final Color color) {
21982198
this.color = color;
21992199
hasCustomColor = color != null;
22002200
if (getFitted() != null) getFitted().setColor(color);
2201-
if (hasNodeColors() && size() == 1) getNodeWithoutChecks(0).setNodeColor(color);
2201+
if (hasNodeColors() && size() == 1) getNodeWithoutChecks(0).setColor(color);
22022202
}
22032203

22042204
/**
@@ -2513,7 +2513,7 @@ public boolean hasRadii() {
25132513
public boolean hasNodeColors() {
25142514
// Direct iteration with early termination is faster than streams!?
25152515
for (final PathNode node : nodes) {
2516-
if (node.hasNodeColor()) return true;
2516+
if (node.hasColor()) return true;
25172517
}
25182518
return false;
25192519
}

‎src/main/java/sc/fiji/snt/analysis/NodeColorMapper.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import sc.fiji.snt.util.ColorMaps;
3838
import sc.fiji.snt.util.PointInImage;
3939
import sc.fiji.snt.util.SNTPoint;
40-
import sc.fiji.snt.util.SWCPoint;
4140
import sc.fiji.snt.viewer.Viewer3D;
4241

4342
/**
@@ -247,7 +246,7 @@ public Map<ColorRGB, List<SNTPoint>> getNodesByColor() {
247246
final HashMap<ColorRGB, List<SNTPoint>> result = new HashMap<>(getColorTable().getLength());
248247
for (final Path path : lasMeasurementTree.list()) {
249248
final Path.PathNode node = path.getNode(0);
250-
final Color colorAWT = node.getNodeColor();
249+
final Color colorAWT = node.getColor();
251250
final ColorRGB color = new ColorRGB(colorAWT.getRed(), colorAWT.getGreen(), colorAWT.getBlue());
252251
result.putIfAbsent(color, new ArrayList<>());
253252
result.get(color).add(node);

‎src/main/java/sc/fiji/snt/analysis/TreeColorMapper.java‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,7 @@
3636
import sc.fiji.snt.analysis.sholl.math.LinearProfileStats;
3737
import sc.fiji.snt.analysis.sholl.math.ShollStats;
3838
import sc.fiji.snt.analysis.sholl.parsers.TreeParser;
39-
import sc.fiji.snt.util.PointInImage;
40-
import sc.fiji.snt.util.SNTColor;
41-
import sc.fiji.snt.util.SWCPoint;
42-
import sc.fiji.snt.util.ShollPoint;
39+
import sc.fiji.snt.util.*;
4340
import sc.fiji.snt.viewer.MultiViewer2D;
4441
import sc.fiji.snt.viewer.Viewer2D;
4542
import sc.fiji.snt.viewer.Viewer3D;
@@ -155,6 +152,8 @@ public static List<String> getMetrics() {
155152
}
156153

157154
public ColorTable getColorTable(final String lut) {
155+
final ColorTable cMap = ColorMaps.get(lut);
156+
if (cMap != null) return cMap;
158157
initLuts();
159158
for (final Map.Entry<String, URL> entry : luts.entrySet()) {
160159
if (entry.getKey().contains(lut)) {

‎src/main/java/sc/fiji/snt/analysis/graph/GraphColorMapper.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.scijava.util.ColorRGB;
3535

3636
import sc.fiji.snt.analysis.ColorMapper;
37+
import sc.fiji.snt.util.ColorMaps;
3738
import sc.fiji.snt.viewer.geditor.GraphEditor;
3839

3940
import java.io.IOException;
@@ -168,6 +169,8 @@ public Set<String> getAvailableLuts() {
168169
}
169170

170171
public ColorTable getColorTable(final String lut) {
172+
final ColorTable cMap = ColorMaps.get(lut);
173+
if (cMap != null) return cMap;
171174
initLuts();
172175
for (final Map.Entry<String, URL> entry : luts.entrySet()) {
173176
if (entry.getKey().contains(lut)) {

‎src/main/java/sc/fiji/snt/util/SNTColor.java‎

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,24 @@ public String toString() {
158158
* @param color the input AWT color
159159
* @return the converted string
160160
*/
161-
public static String colorToString(final Color color) {
162-
if (color == null) return "null color";
163-
return String.format("#%02x%02x%02x%02x", color.getRed(), color.getGreen(),
164-
color.getBlue(), color.getAlpha());
165-
}
161+
public static String colorToString(final Object color) {
162+
switch (color) {
163+
case null -> {
164+
return "null color";
165+
}
166+
case Color colorAWT -> {
167+
return String.format("#%02x%02x%02x%02x", colorAWT.getRed(), colorAWT.getGreen(),
168+
colorAWT.getBlue(), colorAWT.getAlpha());
169+
}
170+
case ColorRGB colorRGB -> {
171+
return String.format("#%02x%02x%02x%02x", colorRGB.getRed(), colorRGB.getGreen(),
172+
colorRGB.getBlue(), colorRGB.getAlpha());
173+
}
174+
default -> {
175+
return null;
176+
}
177+
}
178+
}
166179

167180
/**
168181
* Returns an AWT Color from a (#)RRGGBB(AA) hex string.
@@ -314,6 +327,36 @@ public static ColorRGB[] getDistinctColors(final int nColors, final String exclu
314327
return colors;
315328
}
316329

330+
/**
331+
* Returns distinct colors based on Kenneth Kelly's 22 colors of maximum
332+
* contrast (black and white excluded) as Hex values. More details on this
333+
* <a href="https://stackoverflow.com/a/4382138">SO discussion</a>
334+
*
335+
* @param nColors the number of colors to be retrieved
336+
* @param excludedHue an optional string defining a hue to be excluded. Either 'red', 'green', 'blue', or 'dim'
337+
* @return the maximum contrast colors as hex color strings
338+
*/
339+
public static String[] getDistinctColorsHex(final int nColors, final String excludedHue) {
340+
final ColorRGB[] colors = getDistinctColors(nColors, excludedHue);
341+
final String[] colorsHex = new String[nColors];
342+
for (int i = 0; i < nColors; i++) {
343+
colorsHex[i] = colorToString(colors[i]);
344+
}
345+
return colorsHex;
346+
}
347+
348+
/**
349+
* Returns distinct colors based on Kenneth Kelly's 22 colors of maximum
350+
* contrast (black and white excluded) as Hex values. More details on this
351+
* <a href="https://stackoverflow.com/a/4382138">SO discussion</a>
352+
*
353+
* @param nColors the number of colors to be retrieved
354+
* @return the maximum contrast colors as hex color strings
355+
*/
356+
public static String[] getDistinctColorsHex(final int nColors) {
357+
return getDistinctColorsHex(nColors, "");
358+
}
359+
317360
public static Color[] getDistinctColorsAWT(final int nColors) {
318361
final Color[] colors = new Color[nColors];
319362
final ColorRGB[] colorsRGB = getDistinctColors(nColors);

0 commit comments

Comments
 (0)