HTML color and Java Color : HTML Output « Servlets « Java

HTML color and Java Color


 * 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();


Related examples in the same category

1.Servlet Output HTML Demo
2.Servlet Display Static HTML
3.Prints a conversion table of miles per gallon to kilometers per liter
4.Servlet: Print Table
5.Html utilities
6.Html Parse Servlet
7.Escape and unescape string
8.Escapes newlines, tabs, backslashes, and quotes in the specified string
9.Web Calendar
10.HTML Helper
11.Escape HTML
12.Convert HTML to text
13.Text To HTML
14.Unescape HTML
15.Java object representations of the HTML table structure
16.Entity Decoder
17.Format a color to HTML RGB color format (e.g. #FF0000 for
18.Definitions of HTML character entities and conversions between unicode characters and HTML character entities
19.Encode special characters and do formatting for HTML output
20.HTML color names
21.Utility methods for dealing with HTML
22.Filter the specified message string for characters that are sensitive in HTML
23.A collection of all character entites defined in the HTML4 standard.
24.Decode an HTML color string like '#F567BA;' into a Color
25.Normalize Post Data
26.Get HTML Color String from Java Color object
27.HTML Decoder
28.HTML Parser
29.HTML form Utilites
30.Html Dimensions
31.break Lines with HTML
32.insert HTML block dynamically
33.Convert an integer to an HTML RGB value
34.Convert to HTML string