color to Web Color String - Java 2D Graphics

Java examples for 2D Graphics:Color String

Description

color to Web Color String

Demo Code


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

public class Main {

    public static String color2Web(final Color color) {

        final String rCo = Integer.toHexString(color.getRed());
        final String gCo = Integer.toHexString(color.getGreen());
        final String bCo = Integer.toHexString(color.getBlue());
        final String aCo = Integer.toHexString(color.getAlpha());
        return "#" + (rCo.length() == 1 ? "0" + rCo : rCo)
                + (gCo.length() == 1 ? "0" + gCo : gCo)
                + (bCo.length() == 1 ? "0" + bCo : bCo)
                + (aCo.length() == 1 ? "0" + aCo : aCo);
    }/*  ww w  . ja v  a  2 s .co  m*/
}

Related Tutorials