From 3f8ab894a1ccc8234d8a920a714b202117f96472 Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Mon, 5 Mar 2012 18:53:29 +0400 Subject: [PATCH] extracted duplicates --- .../xml/util/ColorSampleLookupValue.java | 25 +++++++++++++ .../intellij/xml/util/UserColorLookup.java | 35 +------------------ 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/xml/impl/src/com/intellij/xml/util/ColorSampleLookupValue.java b/xml/impl/src/com/intellij/xml/util/ColorSampleLookupValue.java index 916c3fcaf3d5..ea5d0e6a52d8 100644 --- a/xml/impl/src/com/intellij/xml/util/ColorSampleLookupValue.java +++ b/xml/impl/src/com/intellij/xml/util/ColorSampleLookupValue.java @@ -21,6 +21,7 @@ import com.intellij.codeInsight.lookup.LookupValueWithPriority; import com.intellij.codeInsight.lookup.LookupValueWithUIHint; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Iconable; +import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.PsiElement; import com.intellij.xml.XmlBundle; import org.jetbrains.annotations.NonNls; @@ -435,4 +436,28 @@ public class ColorSampleLookupValue implements LookupValueWithUIHint, DeferredUs String colorBox = "
"; buf.append(XmlBundle.message("color.preview", colorBox)).append(BR); } + + public static Color getColor(String text) { + if (StringUtil.isEmptyOrSpaces(text)) { + return null; + } + String hexValue = text.charAt(0) == '#' ? text : getHexCodeForColorName(text.toLowerCase()); + if (hexValue != null) { + String hexValue2 = hexValue.substring(1); + if (hexValue2.length() == 3) { + StringBuilder buf = new StringBuilder(6); + buf.append(hexValue2.charAt(0)).append(hexValue2.charAt(0)); + buf.append(hexValue2.charAt(1)).append(hexValue2.charAt(1)); + buf.append(hexValue2.charAt(2)).append(hexValue2.charAt(2)); + + hexValue2 = buf.toString(); + } + + try { + return Color.decode("0x" + hexValue2); + } + catch (NumberFormatException e) {} + } + return null; + } } diff --git a/xml/impl/src/com/intellij/xml/util/UserColorLookup.java b/xml/impl/src/com/intellij/xml/util/UserColorLookup.java index 5be7f1f924f7..29c431f04c7f 100644 --- a/xml/impl/src/com/intellij/xml/util/UserColorLookup.java +++ b/xml/impl/src/com/intellij/xml/util/UserColorLookup.java @@ -80,41 +80,8 @@ public class UserColorLookup extends LookupElementDecorator { @Nullable public static Color getColorFromElement(final PsiElement element) { - Color myColorAtCaret = null; - if (!(element instanceof XmlToken)) return null; - final String text = element.getText(); - - if (text.length() > 0 && text.charAt(0) == '#') { - myColorAtCaret = decodeColor(text); - } - else { - final String hexCodeForColorName = ColorSampleLookupValue.getHexCodeForColorName(text); - if (hexCodeForColorName != null) myColorAtCaret = decodeColor(hexCodeForColorName); - } - - return myColorAtCaret; + return ColorSampleLookupValue.getColor(element.getText()); } - - private static Color decodeColor(final String text) { - Color myColorAtCaret = null; - try { - String s = text.substring(1); - - if (s.length() == 3) { // css color short hand - StringBuilder buf = new StringBuilder(6); - buf.append(s.charAt(0)).append(s.charAt(0)); - buf.append(s.charAt(1)).append(s.charAt(1)); - buf.append(s.charAt(2)).append(s.charAt(2)); - - s = buf.toString(); - } - myColorAtCaret = Color.decode("0x" + s); - } - catch (Exception ignore) { - } - return myColorAtCaret; - } - }