mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
toHex/fromHex methods
This commit is contained in:
@@ -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("<div style=\"width: 50px; height: 30px; background-color: #%s; border: 1px solid #222;\"> </div>", toHex(color));
|
||||
return String.format("<div style=\"width: 50px; height: 30px; background-color: #%s; border: 1px solid #222;\"> </div>", com.intellij.ui.ColorUtil.toHex(color));
|
||||
}
|
||||
|
||||
public static void appendColorPreview(final PsiVariable variable, final StringBuilder buffer) {
|
||||
|
||||
@@ -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:
|
||||
* <code>#abc123</code>,
|
||||
* <code>ABC123</code>,
|
||||
* <code>ab5</code>,
|
||||
* <code>#FFF</code>.
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user