Java Color to Hex toHexString(Color color)

Here you can find the source of toHexString(Color color)

Description

Converts a color to its hex representation (0xFFFFFF).

License

Open Source License

Parameter

Parameter Description
color the color

Return

String its string representation or null

Declaration

public static final String toHexString(Color color) 

Method Source Code


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

public class Main {
    /**/*from  w  w w  .  ja v  a2 s  .co m*/
     * Converts a color to its hex representation (0xFFFFFF).
     * 
     * @param color
     *            the color
     * @return String its string representation or <code>null</code>
     */
    public static final String toHexString(Color color) {
        if (color == null) {
            return null;
        }
        StringBuilder str = new StringBuilder("0x");
        if (color.getRed() < 16) {
            str.append('0');
        }
        str.append(Integer.toHexString(color.getRed()));
        if (color.getGreen() < 16) {
            str.append('0');
        }
        str.append(Integer.toHexString(color.getGreen()));
        if (color.getBlue() < 16) {
            str.append('0');
        }
        str.append(Integer.toHexString(color.getBlue()));

        return str.toString();
    }
}

Related

  1. toHexColor(final Color color)
  2. toHexColor(final Color color)
  3. toHexString(Color c)
  4. toHexString(Color col)
  5. toHexString(Color col)
  6. toHexString(Color color)
  7. toHexString(Color color)
  8. toHexString(Color color)
  9. toHexString(final Color aColor)