/** * Parses a string into a {@code java.awt.Color} instance. * * <p>The parsing pattern is chosen given the length of the input string * * <ul> * <li>4 - {@code #RGB} * <li>5 - {@code #RGBA} * <li>7 - {@code #RRGGBB} * <li>9 - {@code #RRGGBBAA} * </ul> * * The input string may also be any of the Color constants identified by {@code * griffon.swing.support.Colors}. * * @param str the string representation of a {@code java.awt.Color} * @return a {@code java.awt.Color} instance matching the supplied RGBA color components * @throws ParseException if the string cannot be parsed by the chosen pattern * @see griffon.swing.support.Colors */ @Nonnull @SuppressWarnings("ConstantConditions") public static Color parseColor(@Nonnull String str) throws ParseException { requireNonBlank(str, "Argument must not be blank"); if (str.startsWith("#")) { switch (str.length()) { case 4: return SHORT.parse(str); case 5: return SHORT_WITH_ALPHA.parse(str); case 7: return LONG.parse(str); case 9: return LONG_WITH_ALPHA.parse(str); default: throw parseError(str, Color.class); } } else { // assume it's a Color constant try { return Colors.valueOf(str.toUpperCase()).getColor(); } catch (Exception e) { throw parseError(str, Color.class, e); } } }