Java Color Decode decodeColor(String color, Color defaultColor)

Here you can find the source of decodeColor(String color, Color defaultColor)

Description

decode Color

License

Open Source License

Declaration

public static Color decodeColor(String color, Color defaultColor) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;
import java.util.Hashtable;

public class Main {
    public static Hashtable HTMLColors;

    public static Color decodeColor(String color, Color defaultColor) {
        String colorVal = "";
        if (color.length() > 0) {
            colorVal = color.trim();//  w w  w .j  a v  a2 s.  c  om
            if (colorVal.startsWith("#"))
                colorVal = colorVal.substring(1);
            try {
                colorVal = new Integer(Integer.parseInt(colorVal, 16)).toString();
                return Color.decode(colorVal.toLowerCase());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else
            return defaultColor;
        return getColorForName(color, defaultColor);
    }

    public static Color decodeColor(String color) {
        return decodeColor(color, Color.white);
    }

    public static Color getColorForName(String name, Color defaultColor) {
        if (HTMLColors.contains(name.toLowerCase()))
            return (Color) HTMLColors.get(name.toLowerCase());
        return defaultColor;
    }
}

Related

  1. decode(String color)
  2. decodeColor(final double v)
  3. decodeColor(final String value)
  4. decodeColor(int color)
  5. decodeColor(int value)
  6. decodeColor(String string)
  7. decodeColor(String value, Color dflt)
  8. decodeHexColor(String hexString)
  9. decodeHtmlColorString(String colourString)