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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Servlets » HTML Output 




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
 
 *   http://www.sun.com/cddl/
 *   
 * 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.
 */

//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 = (ColorcolorNames.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 Color.red)
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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.