HTML color and Java Color : Color « 2D Graphics « Java Tutorial

 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.

//Revised from ajax4jsf

import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

 * @author shura (latest modification by $Author: slava_kabanovich $)
 * @version $Revision: 1.2 $ $Date: 2006/07/12 14:59:33 $
public class HtmlColor {
  private static Map colorNames;
  static {
    // color names.
    colorNames = new HashMap();
      colorNames.put("black", new Color(0x000000));
    colorNames.put("green", new Color(0x008000));
    colorNames.put("silver", new Color(0xC0C0C0));
    colorNames.put("lime", new Color(0x00FF00));
    colorNames.put("gray", new Color(0x808080));
    colorNames.put("olive", new Color(0x808000));
    colorNames.put("white", new Color(0xFFFFFF));
    colorNames.put("yellow", new Color(0xFFFF00));
    colorNames.put("maroon", new Color(0x800000));
    colorNames.put("navy", new Color(0x000080));
    colorNames.put("red", new Color(0xFF0000));
    colorNames.put("blue", new Color(0x0000FF));
    colorNames.put("purple", new Color(0x800080));
    colorNames.put("teal", new Color(0x008080));
    colorNames.put("fuchsia", new Color(0xFF00FF));
    colorNames.put("aqua", new Color(0x00FFFF));

   * Decode HTML-attribute style of color to {@link Color}
   * @param color - color name or #RRGGBB string
   * @return - color for this value.
  public static Color decode(String color){
    if(null == color) {
      throw new IllegalArgumentException("NULL_COLOR_PARAMETER_ERROR");
    Color c = (Color) colorNames.get(color.trim().toLowerCase());
    if (null == c) {
      try {
        c = Color.decode(color.trim());
      } catch (NumberFormatException e) {
        throw new IllegalArgumentException("DECODE_COLOR_PARAMETER_ERROR");
    return c;
  public static Integer integerValue(String color){
    return new Integer(decode(color).getRGB());
  public static String encodeRGB(Color color){
    if(null == color) {
      throw new IllegalArgumentException("NULL_COLOR_PARAMETER_ERROR_2");
    return "#" + Integer.toHexString(color.getRGB()).substring(2).toUpperCase();

16.10.2.Drawing with Color
16.10.3.To compare two Color objects you can use the equals() method
16.10.4.Color class is used to work with colors in Java 2D
16.10.5.Using the getRGB() method for comparing colors
16.10.6.Convert RGB to HSB
16.10.7.Convert HSB to RGB value
16.10.8.Construct Color objectConstruct Color object
16.10.9.Constructs several colors and draws various objects using these colors:Constructs several colors and draws various objects using these colors:
16.10.10.Java Predefined Colors
16.10.11.Using color with and Bit andUsing color with and Bit and
16.10.12.Color Factory
16.10.13.Convert an integer to an HTML RGB value
16.10.14.Converts a given string into a color.
16.10.15.HTML color and Java Color
16.10.16.Converts the String representation of a color to an actual Color object.
16.10.17.Get HTML Color String from Java Color object
16.10.18.Derives a color by adding the specified offsets to the base color's hue, saturation, and brightness values
16.10.19.Set XOR mode to color blue
16.10.20.Map colors into names and vice versa.