Attempts to convert the given string to a color. - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Attempts to convert the given string to a color.

Demo Code

/**//  w  ww. j a v a  2 s.c om
 * Copyright 1998-2008, CHISEL Group, University of Victoria, Victoria, BC, Canada.
 * All rights reserved.
 */
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.geom.Rectangle2D;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageObserver;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.util.HashMap;
import java.util.Iterator;
import javax.swing.GrayFilter;

public class Main{
    private static final HashMap COLORS = new HashMap(13);
    /**
     * Attempts to convert the given string to a color.
     * The string can either be one of the 13 main colors:<br>
     * {black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yellow}<br>
     * or a hex value:<br>#00FF00 or 00FF00
     * @param value the string to parse
     * @param defaultColor the default color to return if the value can't be parsed
     * @return Color or null
     */
    public static Color stringToColor(String value, Color defaultColor) {
        Color c = GraphicsUtils.stringToColor(value);
        return (c == null ? defaultColor : c);
    }
    /**
     * Attempts to convert the given string to a color.
     * The string can either be one of the 13 main colors:<br>
     * {black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, yellow}<br>
     * or a hex value:<br>#00FF00 or 00FF00
     * @param value the string to parse
     * @return Color or null if the string couldn't be parsed
     */
    public static Color stringToColor(String value) {
        Color c = null;
        if (value != null) {
            String key = value.toLowerCase();
            if (COLORS.containsKey(key)) {
                c = (Color) COLORS.get(key);
            } else {
                c = GraphicsUtils.hexStringToColor(value);
            }
            if (c == null) {
                c = rgbStringToColor(value);
            }
        }
        return c;
    }
    /**
     * Parses a hex color string (e.g. #FF0000 or FF0000).
     * If the color value couldn't be parsed correctly, null will be returned.
     * @param value   the hex color value to parse
     * @return the color represented by the string, or null if the string was malformed
     */
    public static Color hexStringToColor(String value) {
        Color c = null;
        if (value != null) {
            value = value.trim();
            if (value.startsWith("#")) {
                value = value.substring(1);
            }
            try {
                c = new Color(Integer.parseInt(value, 16));
            } catch (NumberFormatException ignore) {
                c = null;
            }
        }
        return c;
    }
    /**
     * Attempts to parse an RGB triplet or an RGBA quadruplet from the string.
     * It searches through each character and tries to pull out 3 or 4 integers (between 0-255)
     * to use for red, green, blue, and alpha.
     * @param value the string to parse
     * @return the {@link Color} or null
     */
    public static Color rgbStringToColor(String value) {
        Color c = null;
        if ((value != null) && (value.length() > 0)) {
            int[] rgba = { -1, -1, -1, 255 };
            StringBuffer buffer = new StringBuffer(3);
            ;
            boolean inNumber = false;
            int index = 0;
            try {
                if (Character.isDigit(value.charAt(value.length() - 1))) {
                    // handles the case where the last character is a number
                    value = value + " ";
                }
                for (int i = 0; (i < value.length())
                        && (index < rgba.length); i++) {
                    char ch = value.charAt(i);
                    if (Character.isDigit(ch)) {
                        inNumber = true;
                        buffer.append(ch);
                    } else if (inNumber) {
                        inNumber = false;
                        int num = Integer.parseInt(buffer.toString());
                        num = Math.max(0, Math.min(255, num));
                        rgba[index++] = num;
                        buffer = new StringBuffer(3);
                    }
                }
                if (index >= 3) {
                    c = new Color(rgba[0], rgba[1], rgba[2], rgba[3]);
                }
            } catch (NumberFormatException ignore) {
            }
        }
        return c;
    }
}

Related Tutorials