get Hex Color Value - Java 2D Graphics

Java examples for 2D Graphics:Color HEX

Description

get Hex Color Value

Demo Code


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

public class Main {
    public static String getHexColorValue(Color c) {
        return getHexColorValue(c.getRed(), c.getGreen(), c.getBlue());
    }// www.  j  a  v  a2s .com

    /**
     * @param r
     * @param g
     * @param b
     * @return hex color value
     */
    public static String getHexColorValue(int r, int g, int b) {
        String rHex = Integer.toHexString(r).toUpperCase();
        String gHex = Integer.toHexString(g).toUpperCase();
        String bHex = Integer.toHexString(b).toUpperCase();

        rHex = rHex.length() == 1 ? "0" + rHex : rHex;
        gHex = gHex.length() == 1 ? "0" + gHex : gHex;
        bHex = bHex.length() == 1 ? "0" + bHex : bHex;
        return "#" + rHex + gHex + bHex;
    }
}

Related Tutorials