Java Color Decode decodeColor(String value, Color dflt)

Here you can find the source of decodeColor(String value, Color dflt)

Description

This takes the given String and tries to convert it to a color.

License

Apache License

Parameter

Parameter Description
value String value
dflt This is returned if the value cannot be converted

Return

Color defined by the String value or the dflt

Declaration

public static Color decodeColor(String value, Color dflt) 

Method Source Code


//package com.java2s;
/*/*from   w  ww  .j av a2s .com*/
* Copyright (c) 2008-2018 Geode Systems LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*     http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.awt.Color;

public class Main {
    /** Used to map named colors to color */
    public static final Color[] COLORS = { Color.blue, Color.black, Color.red, Color.gray, Color.lightGray,
            Color.white, Color.green, Color.orange, Color.cyan, Color.magenta, Color.pink, Color.yellow };
    /** Used to map named colors to color */
    public static final String[] COLORNAMES = { "blue", "black", "red", "gray", "light gray", "white", "green",
            "orange", "cyan", "magenta", "pink", "yellow" };

    /**
     * This takes the  given String and tries to convert it to a color.
     * The string may be a space or comma separated triple of RGB integer
     * values. It may be an integer or it may be a color name defined in
     * the COLORNAMES array
     *
     * @param value String value
     * @param dflt This is returned if the value cannot be converted
     * @return Color defined by the String value or the dflt
     */
    public static Color decodeColor(String value, Color dflt) {
        if (value == null) {
            return dflt;
        }
        value = value.trim();
        if (value.equals("null")) {
            return null;
        }
        String s = value;
        String lookFor = ",";
        int i1 = s.indexOf(lookFor);
        if (i1 < 0) {
            lookFor = " ";
            i1 = s.indexOf(lookFor);
        }
        if (i1 > 0) {
            String red = s.substring(0, i1);
            s = s.substring(i1 + 1).trim();
            int i2 = s.indexOf(lookFor);
            if (i2 > 0) {
                String green = s.substring(0, i2);
                String blue = s.substring(i2 + 1);
                try {
                    return new Color(Integer.decode(red).intValue(), Integer.decode(green).intValue(),
                            Integer.decode(blue).intValue());
                } catch (Exception exc) {
                    System.err.println("Bad color:" + value);
                }
            }
        }

        try {
            return new Color(Integer.decode(s).intValue());
        } catch (Exception e) {
            s = s.toLowerCase();
            for (int i = 0; i < COLORNAMES.length; i++) {
                if (s.equals(COLORNAMES[i])) {
                    return COLORS[i];
                }
            }
        }

        return dflt;
    }
}

Related

  1. decodeColor(final String value)
  2. decodeColor(int color)
  3. decodeColor(int value)
  4. decodeColor(String color, Color defaultColor)
  5. decodeColor(String string)
  6. decodeHexColor(String hexString)
  7. decodeHtmlColorString(String colourString)
  8. decodeRGB(String color)