public static Color toColor()

in src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java [319:359]


    public static Color toColor(final Object value) throws ConversionException {
        if (value instanceof Color) {
            return (Color) value;
        }
        if (!(value instanceof String) || StringUtils.isBlank((String) value)) {
            throw new ConversionException("The value " + value + " can't be converted to a Color");
        }
        String color = ((String) value).trim();

        final int[] components = new int[3];

        // check the size of the string
        final int minlength = components.length * 2;
        if (color.length() < minlength) {
            throw new ConversionException("The value " + value + " can't be converted to a Color");
        }

        // remove the leading #
        if (color.startsWith("#")) {
            color = color.substring(1);
        }

        try {
            // parse the components
            for (int i = 0; i < components.length; i++) {
                components[i] = Integer.parseInt(color.substring(2 * i, 2 * i + 2), HEX_RADIX);
            }

            // parse the transparency
            final int alpha;
            if (color.length() >= minlength + 2) {
                alpha = Integer.parseInt(color.substring(minlength, minlength + 2), HEX_RADIX);
            } else {
                alpha = Color.black.getAlpha();
            }

            return new Color(components[0], components[1], components[2], alpha);
        } catch (final Exception e) {
            throw new ConversionException("The value " + value + " can't be converted to a Color", e);
        }
    }