use of com.serotonin.InvalidArgumentException in project ma-core-public by infiniteautomation.
the class ColorUtils method toColor.
public static Color toColor(String s) throws InvalidArgumentException {
if (s == null) {
throw new InvalidArgumentException("Color string is null");
}
String colorString = s.toLowerCase(Locale.ROOT).trim();
Object o;
Matcher m;
try {
Color color;
if ((m = SHORT_HEX_PATTERN.matcher(colorString)).matches()) {
String hex = m.group(1);
int red = Integer.parseUnsignedInt(hex.substring(0, 1), 16) * 0x11;
int green = Integer.parseUnsignedInt(hex.substring(1, 2), 16) * 0x11;
int blue = Integer.parseUnsignedInt(hex.substring(2, 3), 16) * 0x11;
color = new Color(red, green, blue);
} else if ((m = HEX_PATTERN.matcher(colorString)).matches()) {
color = new Color(Integer.parseInt(m.group(1), 16));
} else if ((m = RGB_PATTERN.matcher(colorString)).matches()) {
int alpha = 255;
boolean hasAlpha = m.group(1) != null;
int correctLength = hasAlpha ? 4 : 3;
String[] parts = m.group(2).trim().split("\\s*,\\s*");
if (parts.length != correctLength) {
throw new InvalidArgumentException("Invalid rgb/rgba format: " + s);
}
int red = Integer.parseInt(parts[0]);
int green = Integer.parseInt(parts[1]);
int blue = Integer.parseInt(parts[2]);
if (hasAlpha) {
alpha = (int) (Float.parseFloat(parts[3]) * 255 + 0.5);
}
color = new Color(red, green, blue, alpha);
} else if ((o = colorMap.get(colorString)) != null) {
if (o instanceof String) {
color = toColor((String) o);
colorMap.put(colorString, color);
} else {
color = (Color) o;
}
} else {
throw new InvalidArgumentException("Invalid color format: " + s);
}
return color;
} catch (IllegalArgumentException e) {
throw new InvalidArgumentException("Error parsing color value: " + s + ", " + e.getMessage());
}
}
Aggregations