ColorUtils.java Source code

Java tutorial

Introduction

Here is the source code for ColorUtils.java

Source

/*
 * 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;
    }
}