简体   繁体   中英

String to color

I have VARCHAR in the database for colors. The format is rrr,ggg,bbb (example 225,225,0).

How can I convert that String into java.awt.Color and visualize it in a JSF page?

Do this :
1. Read you color column value
2. Split your string values with , or any other separator you have used
3. Parse each value to Integer value 4. Passed this value into java.awt.color constructors.

Sample Example :

int red,green,blue;
String colorStr = "225,225,0"; //Value from DB
String[] ar_color = colorStr.split(",");
red = Integer.parseInt(ar_color[0]);
green = Integer.parseInt(ar_color[1]);
blue = Integer.parseInt(ar_color[2]);  
Color myColor = new Color(red, green, blue);  

I would parse the database entry and cast the values to integers, then you can use one of the constructors for a java.awt.color .

However I am not sure that is what you want. You want to use this color value in one of your JSF pages.

You might be setting a color on a component like this :

this.myComponent.setStyle("color:'225,225,0'");

So you could dynamically then change the color like this from the DB :

this.myComponent.setStyle("color:" + myColorStringFromDB); 

No need to convert to a java color.

Create a Color object, using the values you have

Color color = new Color(int rrr, int ggg, int bbb)

Example

Color color = new Color(255, 255, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM