Converts a color's RGB presentation to a textual hexadecimal representation. - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

Converts a color's RGB presentation to a textual hexadecimal representation.

Demo Code

/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://  www.  j av a2 s  . co  m
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int red = 2;
        int green = 2;
        int blue = 2;
        System.out.println(rgbToText(red, green, blue));
    }

    /**
     * Converts a color's RGB presentation to a textual hexadecimal representation.
     * <p>
     * Example: r = 255, g = 0, b = 0 --> "#ff0000"
     * <p>
     * Note: the hexadecimal representation is lowercase
     * 
     * @return hexadecimal representation of RGB in lowercase.
     * @since 4.0-M7
     */
    public static String rgbToText(int red, int green, int blue) {
        return String.format("#%02x%02x%02x", red, green, blue);
    }
}

Related Tutorials