Java Color to Hex toHexString(java.awt.Color c)

Here you can find the source of toHexString(java.awt.Color c)

Description

Convert the given color to is string hex representation

License

Open Source License

Parameter

Parameter Description
c color

Return

hex represenation

Declaration

public static String toHexString(java.awt.Color c) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from w w w  .  j a v a2 s.  c  om*/
     * Convert the given color to is string hex representation
     *
     * @param c color
     * @return hex represenation
     */
    public static String toHexString(java.awt.Color c) {
        return "#" + padRight(Integer.toHexString(c.getRed()), 2, "0")
                + padRight(Integer.toHexString(c.getGreen()), 2, "0")
                + padRight(Integer.toHexString(c.getBlue()), 2, "0");
    }

    /**
     * Pad the given string with spaces on the right up to the given length.
     *
     * @param s             String to pad
     * @param desiredLength ending length
     * @return padded String
     */
    public static String padRight(String s, int desiredLength) {
        return padRight(s, desiredLength, " ");
    }

    /**
     * Pad the given string with padString on the right up to the given length.
     *
     * @param s             String to pad
     * @param desiredLength ending length
     * @param padString     String to pad with (e.g, " ")
     * @return padded String
     */
    public static String padRight(String s, int desiredLength,
            String padString) {
        while (s.length() < desiredLength) {
            s = s + padString;
        }
        return s;
    }
}

Related

  1. toHexString(Color color)
  2. toHexString(Color color)
  3. toHexString(Color color)
  4. toHexString(Color color)
  5. toHexString(final Color aColor)
  6. toHexString(java.awt.Color c)
  7. toHexString(java.awt.Color colour)