Search in sources :

Example 11 with InvalidArgumentException

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());
    }
}
Also used : InvalidArgumentException(com.serotonin.InvalidArgumentException) Matcher(java.util.regex.Matcher) Color(java.awt.Color)

Aggregations

InvalidArgumentException (com.serotonin.InvalidArgumentException)11 Color (java.awt.Color)4 IOException (java.io.IOException)4 DataPointDao (com.serotonin.m2m2.db.dao.DataPointDao)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 ArrayList (java.util.ArrayList)3 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)2 ReportPointVO (com.serotonin.m2m2.reports.vo.ReportPointVO)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)2 PointTimeSeriesCollection (com.serotonin.m2m2.util.chart.PointTimeSeriesCollection)2 User (com.serotonin.m2m2.vo.User)2 File (java.io.File)2 TimeZone (java.util.TimeZone)2 JsonException (com.serotonin.json.JsonException)1 ImageSaveException (com.serotonin.m2m2.ImageSaveException)1 LicenseViolatedException (com.serotonin.m2m2.LicenseViolatedException)1 SystemSettingsDao (com.serotonin.m2m2.db.dao.SystemSettingsDao)1 UserDao (com.serotonin.m2m2.db.dao.UserDao)1 PostEmailRunnable (com.serotonin.m2m2.email.PostEmailRunnable)1