|
22 | 22 |
|
23 | 23 | package sc.fiji.snt.util; |
24 | 24 |
|
| 25 | +import ij.plugin.Colors; |
25 | 26 | import org.scijava.util.ColorRGB; |
26 | 27 |
|
27 | 28 | import java.awt.*; |
@@ -187,17 +188,29 @@ public static Color fromHex(final String hex) { |
187 | 188 | final ColorRGB color = ColorRGB.fromHTMLColor(hex.toLowerCase()); // backwards compatibility for v4.2.0 that used capitalized strings |
188 | 189 | if (color != null) |
189 | 190 | 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 | + } |
192 | 197 | 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); |
198 | 202 | return new Color(r / 255f, g / 255f, b / 255f, a / 255f); |
199 | 203 | } |
200 | 204 |
|
| 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 | + |
201 | 214 | /** |
202 | 215 | * Averages a collection of colors |
203 | 216 | * |
|
0 commit comments