views:

320

answers:

3

Hi all,

Is it possible to specify a color in a properties file that has an alpha component? When I put a hexadecimal number in the properties file that has an alpha channel, the alpha is ignored. I notice that the decode method of string says "Converts a String to an integer and returns the specified opaque Color.", and that the only single argument constructor that takes in an int or hexadecimal number also says that it's an opaque color. What is the best practice in this regard?

The alternatives I see are to have something like

component.color.red=128
component.color.green=25
component.color.blue=54
component.color.alpha=244

or

component.color=128_25_54_244

and then manually splitting up the string into its constituent RGBA components. Neither of these solutions looks very appealing to me.

I remember at one time I saw an alternative properties project, I think for swingx, that had better support for colors. Does anyone know what that was, and whether it could solve my problem?

EDIT:

As I say in the comments to one of the posts, I want a solution where I can specify colors in whatever means I want in the properties file (hexadecimal for opaque colors, signed integer for translucent) and not have to change the source code that deals with interpreting the key/value pairs for the color.

Second Edit: The library I was talking about is Fuse, which is a resource injection framework for GUI programming.

+2  A: 

Store the color as a full 32-bit int (use Color.getRGB()) and reconstitute the Color using Color(int, true)

This will make it hard to hand-edit the properties files since you'll see numbers like

somecolor = -123875123

Some more fun example code w/ output

public static void main(String[] args)
{
    Color r = Color.red;
    int ir = r.getRGB();
    Color nr = new Color(ir, true);
    System.out.println(r + ":" + r.getAlpha() + ", " + ir + ", " + nr + ":" + nr.getAlpha());

    Color c = new Color(1.0f, 0.5f, 1.0f, 0.5f);
    int ic = c.getRGB();
    Color nc = new Color(ic, true);
    System.out.println(c + ":" + c.getAlpha() + ", " + ic + ", " + nc + ":" + nc.getAlpha());
}

Output

java.awt.Color[r=255,g=0,b=0]:255, -65536, java.awt.Color[r=255,g=0,b=0]:255
java.awt.Color[r=255,g=128,b=255]:128, -2130738945, java.awt.Color[r=255,g=128,b=255]:128
basszero
The problem with this approach is that I cannot use the same code for translucent and opaque colors. For instance, if I specify my color as a standard opaque color FFFFFF, I need to make sure that I read the property file, parse the string using base 16. If I have a translucent color I need to read the property file, parse the string using base 10. I want a mechanism that will be the same in either case - when I change my properties file, I don't want to have to change any source code.
I82Much
If you specify them both with hex, that will work. ie: FFFFFFFF for opaque or 77FFFFFF to specify translucent. then just use the Color(int, true) ctor. You could use a little trickery to see if there are only 6 digits, to just prepend the FF on the front.
Kylar
I left out the hex conversion parts. In my application we write colors exactly as Kylar has indicated
basszero
+1  A: 

What about the Color( int rgb, boolean hasAlpha ) constructor.

It says that I will take the alpha from the same number, that may work for you, just remember to set it to true always and for opaque colors use the 255

OscarRyz
+2  A: 

The easiest way would be to store them all as ARGB, then you can parse and pass directly in. (prepend FF if it's an opaque color). Repost as answer :)

Kylar