Returns a hex string representing the given color object. - Java 2D Graphics

Java examples for 2D Graphics:Color String

Description

Returns a hex string representing the given color object.

Demo Code

/**//from   w  w w  .  j  a v  a  2 s.c  o m
 * This class offers a few utility methods useful when handling Colors.
 * <p/>
 * <hr/> Copyright 2006-2012 Torsten Heup
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
//package com.java2s;
import java.awt.*;

public class Main {
    /**
     * Returns a hex string representing the given color object.
     *
     * @param color color to be converted to hex string, non-null.
     * @return a hex string representing the given color.
     */
    public static String asHexString(final Color color) {
        if (color == null)
            throw new IllegalArgumentException(
                    "Parameter 'color' must not be null!");

        return String.format("%02x%02x%02x", color.getRed(),
                color.getGreen(), color.getBlue());
    }
}

Related Tutorials