extracted duplicates

This commit is contained in:
Maxim.Mossienko
2012-03-05 20:49:16 +04:00
parent 90f6bada64
commit 3f8ab894a1
2 changed files with 26 additions and 34 deletions
@@ -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 = "<div style=\"border: 1px solid #000000; width: 50px; height: 20px; background-color:" + code + "\"></div>";
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;
}
}
@@ -80,41 +80,8 @@ public class UserColorLookup extends LookupElementDecorator<LookupElement> {
@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;
}
}