Reputation: 27326
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.
Upvotes: 4
Views: 3380
Reputation: 9334
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 :)
Upvotes: 3
Reputation: 30004
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
Upvotes: 2
Reputation: 199215
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
Upvotes: 1