Converts a color into a String like #RRGGBB. - Java 2D Graphics

Java examples for 2D Graphics:Color

Description

Converts a color into a String like #RRGGBB.

Demo Code

/**/*  w ww. j a  va 2s.  com*/
 * Copyright 1998-2008, CHISEL Group, University of Victoria, Victoria, BC, Canada.
 * All rights reserved.
 */
//package com.java2s;
import java.awt.Color;

public class Main {
    /**
     * Converts a color into a String like #RRGGBB.
     * @param color   Color to convert into a String.
     * @return String color String.
     */
    public static String colorToHexString(Color color) {
        String colorStr = "";
        if (color != null) {
            colorStr = Integer.toHexString(color.getRGB());
            colorStr = "#" + colorStr.substring(2); // chop off the alpha value
        }
        return colorStr;
    }
}

Related Tutorials