encode Color to String - Java 2D Graphics

Java examples for 2D Graphics:Color String

Description

encode Color to String

Demo Code


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

public class Main {
    public static String encode(Color c) {
        return "#" + hex(c.getAlpha(), 2) + hex(c.getRed(), 2)
                + hex(c.getGreen(), 2) + hex(c.getBlue(), 2);
    }//  w  ww . j ava  2  s .  c  o m

    public static String hex(int i, int places) {
        String s = Integer.toHexString(i);
        while (s.length() < places)
            s = "0" + s;
        return s;
    }
}

Related Tutorials