Mega Code Archive

 
Categories / Java / 2D Graphics GUI
 

Serializes a color to its HTML markup (e g #ff0000 for red)

/*  * ColorUtils.java Created Nov 17, 2010 by Andrew Butler, PSL  */ //package prisms.util; import java.awt.Color; /** A set of tools for analyzing and manipulating colors */ public class ColorUtils {   /**    * Serializes a color to its HTML markup (e.g. "#ff0000" for red)    *     * @param c The color to serialize    * @return The HTML markup of the color    */   public static String toHTML(Color c)   {     String ret = "#";     String hex;     hex = Integer.toHexString(c.getRed());     if(hex.length() < 2)       hex = "0" + hex;     ret += hex;     hex = Integer.toHexString(c.getGreen());     if(hex.length() < 2)       hex = "0" + hex;     ret += hex;     hex = Integer.toHexString(c.getBlue());     if(hex.length() < 2)       hex = "0" + hex;     ret += hex;     return ret;   } }