Social Icons

Friday, June 4, 2010

How to get HTML hex color number – java


Following code shows how to get the HTML color code in Hexadecimal base in your java code which then can be used in a HTML code . java.awt.Color object must be passes as the argument.

/**
* Get #ffffff html hex number for a colour
*
* @see #toHexString(int)
* @param c
*        Color object whose html colour number you want as a string
* @return # followed by exactly 6 hex digits
*/
public final static String toString ( Color c ){
   String s = Integer.toHexString( c.getRGB() & 0xffffff );
   if ( s.length() < 6 )
      { // pad on left with zeros
      s = "000000".substring( 0, 6 - s.length() ) + s;
      }
   return '#' + s;
}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Blogger Templates http://slots.to/