Java Color Encode encodeColor(Color color)

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

Description

Converts a Color to a hex color string.

License

Open Source License

Parameter

Parameter Description
color Color to encode

Return

A hex Color string in the format #rrggbb.

Declaration

public static String encodeColor(Color color) 

Method Source Code


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

import java.awt.Color;

public class Main {
    /**//from  w w w.  j  a  v a 2s  . co m
     * Converts a Color to a hex color string. For example Color.GREEN will return "#00FF00".
     * @see java.awt.Color#decode(String) which does the opposite
     * @param color Color to encode
     * @return A hex Color string in the format #rrggbb. 
     */
    public static String encodeColor(Color color) {
        if (color == null) {
            //If no color, then return a middle-of-the-road gray.
            return "#808080";
        }
        return "#" + String.format("%06x", color.getRGB() & 0xffffff);

    }
}

Related

  1. encode(Color color)
  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)
  8. encodeColor(int r, int g, int b)
  9. encodeColorForCss(Color color)