Transparent colors in Java through property files

Can I specify a color in a property file that has an alpha component? When I put a hexadecimal number in a properties file that has an alpha channel, alpha is ignored. I notice that the string decoding method says: "Converts the string to an integer and returns the specified opaque color .", And that the only single argument constructor that accepts an int or hexadecimal number also says that it is an opaque color. What is the best practice in this regard?

The alternatives that I see should 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 breaking the string into its constituent RGBA components. None of these solutions look very attractive to me.

I remember once I saw a project of alternative properties, I think for swingx, which supported colors better. Does anyone know what it was, and can it solve my problem?

EDIT:

As I said in the comments on one of the posts, I want a solution where I can specify the colors in any way that I want in the properties file (hex for opaque colors, signed integer for translucent) and there is no need to change the source code, which deals with the interpretation of key / value pairs for color.

Second edit: In the library I talked about, Fuse , which is the foundation for introducing resources for GUI programming.

+4
source share
3 answers

The easiest way is to save them all as ARGB, then you can analyze and transmit directly. (add FF if it is an opaque color). Repost as an answer :)

+2
source

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

This makes it difficult to edit the property files manually, as you will see numbers such as

 somecolor = -123875123 

Another fun example exit code

 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()); } 

Exit

 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 
+2
source

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

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

+1
source

Source: https://habr.com/ru/post/1300222/


All Articles