Convert AWT color to CSS color. - Java 2D Graphics

Java examples for 2D Graphics:Color HTML

Description

Convert AWT color to CSS color.

Demo Code


//package com.java2s;
import java.awt.Color;

public class Main {
    /**//from   w  ww  . j  a va  2  s.co m
     * Convert AWT color to CSS color.
     * 
     * @param color
     *            An AWT color instance
     * @return CSS representation of the specified color
     */
    static String cssColor(Color color) {
        String red = Integer.toHexString(color.getRed());
        String green = Integer.toHexString(color.getGreen());
        String blue = Integer.toHexString(color.getBlue());
        String convertedColor = "#" + prefixZero(red) + prefixZero(green)
                + prefixZero(blue);
        return convertedColor;
    }

    private static String prefixZero(String str) {
        if (str.length() > 2) {
            throw new RuntimeException("This string, '" + str
                    + "', is not an appropriate hex number.");
        }
        if (str.length() == 2) {
            return str;
        }
        if (str.length() == 1) {
            return "0" + str;
        }
        return "00";
    }
}

Related Tutorials