Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting String to Color  RSS feed

 
Sanam Chandrasekaran
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am working on applets,
i am passing a color parameter as shown below

/*
<applet code = "ZoominApplet3.class" width = 300 height = 200>

<param name = "color" value = "Font.RED">
</applet>
*/




fontColor =getParameter("color");


now fontColor is of String type,
i want to convert it to Color type, so that
i can pass it as shown below.

txtA.setBackground(fontColor);


It would be kindful if anyone could tell me how to convert a variable
from String to Color

Regards
Sanam
 
Norm Miller
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The Color class has a static method "decode" that takes a string and returns a Color.

Something like:
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Color does indeed have a "decode" method, and it's what most people would use in this situation. But note that it won't take an argument like "Font.RED" (Did you mean "Color.RED"?) It take hexadecimal color values like "#FFFFFF" (white) and "#FF0000" (red).

If your applet parameters must be symbolic color names like "RED", then your applet will need to contain a table mapping between these and the hexadecimal equivalents.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!