Skip to content

Commit 5c6d232

Browse files
committed
Fix color parsing in v4 traces files (#260)
1 parent e36992f commit 5c6d232

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

src/main/java/sc/fiji/snt/PathAndFillManager.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,11 +1686,15 @@ public void startElement(final String uri, final String localName,
16861686
}
16871687

16881688
if (colorString != null) {
1689-
current_path.setColor(SNTColor.fromHex(colorString));
1689+
try {
1690+
current_path.setColor(SNTColor.fromHex(colorString));
1691+
} catch (final IllegalArgumentException ignored) {
1692+
// see https://github.com/morphonets/SNT/issues/260
1693+
SNTUtils.log("Could not read color '"+ colorString + "', " + current_path);
1694+
}
16901695
}
16911696
if (channelString != null && frameString != null) {
1692-
current_path.setCTposition(Integer.parseInt(channelString), Integer
1693-
.parseInt(frameString));
1697+
current_path.setCTposition(Integer.parseInt(channelString), Integer.parseInt(frameString));
16941698
}
16951699
if (spineString != null) {
16961700
current_path.setSpineOrVaricosityCount(Integer.parseInt(spineString));

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package sc.fiji.snt.util;
2424

25+
import ij.plugin.Colors;
2526
import org.scijava.util.ColorRGB;
2627

2728
import java.awt.*;
@@ -187,17 +188,29 @@ public static Color fromHex(final String hex) {
187188
final ColorRGB color = ColorRGB.fromHTMLColor(hex.toLowerCase()); // backwards compatibility for v4.2.0 that used capitalized strings
188189
if (color != null)
189190
return new Color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
190-
if (hex.length() < 6) throw new IllegalArgumentException(
191-
"Unsupported format. Only (#)RRGGBB(AA) allowed");
191+
if (hex.length() < 6) {
192+
final Color legacyColor = getLegacyColor(hex);
193+
if (legacyColor != null)
194+
return legacyColor;
195+
throw new IllegalArgumentException("Unsupported format. Only (#)RRGGBB(AA) allowed");
196+
}
192197
final String input = hex.charAt(0) == '#' ? hex.substring(1) : hex;
193-
final int r = Integer.valueOf(input.substring(0, 2), 16);
194-
final int g = Integer.valueOf(input.substring(2, 4), 16);
195-
final int b = Integer.valueOf(input.substring(4, 6), 16);
196-
final int a = (hex.length() < 8) ? 255 : Integer.valueOf(hex.substring(6,
197-
8), 16);
198+
final int r = Integer.parseInt(input.substring(0, 2), 16);
199+
final int g = Integer.parseInt(input.substring(2, 4), 16);
200+
final int b = Integer.parseInt(input.substring(4, 6), 16);
201+
final int a = (input.length() < 8) ? 255 : Integer.parseInt(input.substring(6, 8), 16);
198202
return new Color(r / 255f, g / 255f, b / 255f, a / 255f);
199203
}
200204

205+
private static Color getLegacyColor(String colorName) {
206+
// see https://github.com/morphonets/SNT/issues/260
207+
if (colorName == null) colorName = "none";
208+
Color color = null;
209+
color = Colors.getColor(colorName, color);
210+
if (color == null) color = Colors.decode(colorName, color);
211+
return color;
212+
}
213+
201214
/**
202215
* Averages a collection of colors
203216
*

0 commit comments

Comments
 (0)