diff --git a/java/java-impl/src/com/intellij/codeInsight/javadoc/ColorUtil.java b/java/java-impl/src/com/intellij/codeInsight/javadoc/ColorUtil.java index 745693b94d71..0ae045a61d51 100644 --- a/java/java-impl/src/com/intellij/codeInsight/javadoc/ColorUtil.java +++ b/java/java-impl/src/com/intellij/codeInsight/javadoc/ColorUtil.java @@ -29,22 +29,8 @@ public class ColorUtil { private ColorUtil() { } - public static String toHex(@NotNull final Color color) { - final StringBuffer sb = new StringBuffer(); - for (int i = 0; i < 3; i++) { - String s = Integer.toHexString(i == 0 ? color.getRed() : i == 1 ? color.getGreen() : color.getBlue()); - if (s.length() < 2) { - sb.append('0'); - } - - sb.append(s); - } - - return sb.toString(); - } - public static String generatePreviewHtml(@NotNull final Color color) { - return String.format("
#abc123,
+ * ABC123,
+ * ab5,
+ * #FFF.
+ *
+ * @param str hex string
+ * @return Color object
+ */
+ public static Color fromHex(String str) {
+ if (str.startsWith("#")) {
+ str = str.substring(1);
+ }
+ if (str.length() == 3) {
+ return new Color(
+ 17 * Integer.valueOf(String.valueOf(str.charAt(0)), 16).intValue(),
+ 17 * Integer.valueOf(String.valueOf(str.charAt(1)), 16).intValue(),
+ 17 * Integer.valueOf(String.valueOf(str.charAt(2)), 16).intValue());
+ } else if (str.length() == 6) {
+ return Color.decode("0x" + str);
+ } else {
+ throw new IllegalArgumentException("Should be String of 3 or 6 chars length.");
+ }
+ }
+
+ public static Color fromHex(String str, Color defaultValue) {
+ try {
+ return fromHex(str);
+ } catch (Exception e) {
+ return defaultValue;
+ }
+ }
}