Java Color Encode encode(Color color)

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

Description

Encodes the specified color to a hexadecimal string representation.

License

Open Source License

Parameter

Parameter Description
color The color that is encoded.

Return

An hexadecimal string representation of the specified color.

Declaration

public static String encode(Color color) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;

public class Main {
    private static final int MAX_RGB_VALUE = 255;

    /**//from www  .  java2s .  co  m
     * Encodes the specified color to a hexadecimal string representation.
     * The output format is:
     * <ul>
     * <li>#RRGGBB - For colors without alpha</li>
     * <li>#AARRGGBB - For colors with alpha</li>
     * </ul>
     * Examples: <br>
     * <code>Color.RED</code> = "#ff0000"<br>
     * <code>new Color(255, 0, 0, 200)</code> = "#c8ff0000"
     * 
     * @param color
     *          The color that is encoded.
     * @return An hexadecimal string representation of the specified color.
     * 
     * @see ColorHelper#decode(String)
     * @see Color
     * @see Color#getRGB()
     * @see Integer#toHexString(int)
     */
    public static String encode(Color color) {
        if (color == null) {
            return null;
        }

        if (color.getAlpha() == MAX_RGB_VALUE) {
            return "#" + Integer.toHexString(color.getRGB()).substring(2);
        } else {
            return "#" + Integer.toHexString(color.getRGB());
        }
    }
}

Related

  1. encode(Color c)
  2. encode(Color color)
  3. encode(Color color)
  4. encode(Color color)
  5. encode(java.awt.Color c)
  6. encodeColor(Color color)
  7. encodeColor(Color color)