Java Color to Hex toHexColor(final Color color)

Here you can find the source of toHexColor(final Color color)

Description

Converts a given color to it's hexidecimal equivalent.

License

Open Source License

Parameter

Parameter Description
color Color to get hexidecimal string from.

Return

Hexidecimal string representing the given color, in the form "#abcdef".

Declaration

public static String toHexColor(final Color color) 

Method Source Code


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

public class Main {
    /**//from   w w  w .j a  v  a2  s.co m
     * Converts a given color to it's hexidecimal equivalent.
     *
     * @param color Color to get hexidecimal string from.
     * @return      Hexidecimal string representing the given color, in the form "#abcdef".
     */
    public static String toHexColor(final Color color) {
        return "#" + colorToHexCode(color);
    }

    /**
     * Gets the RGB hex color code of the passed color.
     *
     * @param color The color to get a hex code from.
     * @return      A lower-cased string of the RGB hex code of color.
     */
    public static String colorToHexCode(final Color color) {
        return String.format("%06x", color.getRGB() & 0xFFFFFF);
    }
}

Related

  1. toHex0x(Color col)
  2. toHexCode(String prefix, Color color)
  3. toHexColor(Color col)
  4. toHexColor(Color col)
  5. toHexColor(final Color color)
  6. toHexString(Color c)
  7. toHexString(Color col)
  8. toHexString(Color col)
  9. toHexString(Color color)