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("
 
", toHex(color)); + return String.format("
 
", com.intellij.ui.ColorUtil.toHex(color)); } public static void appendColorPreview(final PsiVariable variable, final StringBuilder buffer) { diff --git a/platform/util/src/com/intellij/ui/ColorUtil.java b/platform/util/src/com/intellij/ui/ColorUtil.java index 96beb3d05dc6..7a0ee5b85f6c 100644 --- a/platform/util/src/com/intellij/ui/ColorUtil.java +++ b/platform/util/src/com/intellij/ui/ColorUtil.java @@ -19,8 +19,14 @@ */ package com.intellij.ui; +import org.jetbrains.annotations.NotNull; + import java.awt.*; +/** + * @author max + * @author Konstantin Bulenkov + */ public class ColorUtil { private ColorUtil() { } @@ -36,4 +42,49 @@ public class ColorUtil { public static Color withAlphaAdjustingDarkness(Color c, double d) { return shift(withAlpha(c, d), d); } + + public static String toHex(@NotNull final Color c) { + final String R = Integer.toHexString(c.getRed()); + final String G = Integer.toHexString(c.getGreen()); + final String B = Integer.toHexString(c.getBlue()); + return new StringBuffer() + .append(R.length() < 2 ? "0" : "").append(R) + .append(G.length() < 2 ? "0" : "").append(G) + .append(B.length() < 2 ? "0" : "").append(B) + .toString(); + } + + /** + * Return Color object from string. The following formats are allowed: + * #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; + } + } }