BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 11-03-2005, 11:31 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2005
Location: Portland, OR., USA
Model: 9800
Carrier: AT&T
Posts: 82
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default COLOR reference by string

Please Login to Remove!

Ok, so my java skill is lacking in a technique to do this.

I want to be able to use a string to represent a color, and to use that string to find the color integer value in the COLOR class.

IE. Where I would do...
graphics.setBackgroundColor(Color.SKYBLUE);

I want to find the INT color value from COLOR using a string value "SKYBLUE"

Since the colors are constants that represent integers, is there a way to find which constant equals a string value and then take its integer value?

Thanks,
Eric
Offline  
Old 11-03-2005, 11:47 AM   #2 (permalink)
Thumbs Must Hurt
 
SCODonnell's Avatar
 
Join Date: Oct 2005
Location: Chicago
Model: 9930
OS: 7.1.0.267
PIN: the_tail_on_the_donkey
Carrier: Sprint
Posts: 71
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Color values -- not the easiest way probably

It's probably not the easiest way (it may be in a reference book somewhere), but you could use the Java Development Environment (JDE) and set breakpoints before and after you perform a color asignment. When you hit the break, check in your "Locals" watchlist to see what the int values are before and after the assignment:

Sample Code:

Code:
    ListField myList = new ListField() {
        protected void paint(Graphics g) {
            g.setBackgroundColor(Color.SKYBLUE);
            g.clear();            
            ...
Locals:

Code:
Name                 Value                 Type
------------------------------------------------
g
|--- _bgColour       8900331                int
Offline  
Old 11-03-2005, 03:19 PM   #3 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Sep 2004
Model: 8700r
Carrier: Rogers
Posts: 221
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

A couple of ways to do this:

1. Use a Hashtable with key as the static int Color map and the value as the String representation of that colour.

2. Use 2 arrays; a 'int' array as the Color values and a 'String' array as the string description. The int and String arrays are mapped directly (i.e. 1 to 1).

ASIDE: Interesting that RIM being a canadian company decided to call the class Color rather than Colour (Canadian Spelling).
Offline  
Old 11-03-2005, 03:50 PM   #4 (permalink)
Thumbs Must Hurt
 
Join Date: Apr 2005
Location: Portland, OR., USA
Model: 9800
Carrier: AT&T
Posts: 82
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, well barring an existing built in method to reference via the static variables, I went and created a class that I thought you all might find useful, so here it is. And no, I didn't type all color names; a little copy/paste and some editor macros did all the work for me.

The main reason I wanted/needed a way of doing this is because I wanted the ability to allow user customization on one of our apps. This way, they can specify the color, make that, colour name, rather than entering some integer value.

Because its static, you can reference without having to instanciate, like the existing Color.
IE.
int color = Colors.getColor("DARKGOLDENROD");

Cheers,
Eric

Code:
package //your package;

import net.rim.device.api.ui.Color;

class Colors {
        private Colors() {}

        public static int getColor(String colorName) {

                if (colorName.equals("RED")) return Color.RED;
                if (colorName.equals("ALICEBLUE")) return Color.ALICEBLUE;
                if (colorName.equals("ANTIQUEWHITE")) return Color.ANTIQUEWHITE;
                if (colorName.equals("AQUA")) return Color.AQUA;
                if (colorName.equals("AQUAMARINE")) return Color.AQUAMARINE;
                if (colorName.equals("AZURE")) return Color.AZURE;
                if (colorName.equals("BEIGE")) return Color.BEIGE;
                if (colorName.equals("BISQUE")) return Color.BISQUE;
                if (colorName.equals("BLACK")) return Color.BLACK;
                if (colorName.equals("BLANCHEDALMOND")) return Color.BLANCHEDALMOND;
                if (colorName.equals("BLUE")) return Color.BLUE;
                if (colorName.equals("BLUEVIOLET")) return Color.BLUEVIOLET;
                if (colorName.equals("BROWN")) return Color.BROWN;
                if (colorName.equals("BURLYWOOD")) return Color.BURLYWOOD;
                if (colorName.equals("CADETBLUE")) return Color.CADETBLUE;
                if (colorName.equals("CHARTREUSE")) return Color.CHARTREUSE;
                if (colorName.equals("CHOCOLATE")) return Color.CHOCOLATE;
                if (colorName.equals("CORAL")) return Color.CORAL;
                if (colorName.equals("CORNFLOWERBLUE")) return Color.CORNFLOWERBLUE;
                if (colorName.equals("CORNSILK")) return Color.CORNSILK;
                if (colorName.equals("CRIMSON")) return Color.CRIMSON;
                if (colorName.equals("CYAN")) return Color.CYAN;
                if (colorName.equals("DARKBLUE")) return Color.DARKBLUE;
                if (colorName.equals("DARKCYAN")) return Color.DARKCYAN;
                if (colorName.equals("DARKGOLDENROD")) return Color.DARKGOLDENROD;
                if (colorName.equals("DARKGRAY")) return Color.DARKGRAY;
                if (colorName.equals("DARKGREEN")) return Color.DARKGREEN;
                if (colorName.equals("DARKKHAKI")) return Color.DARKKHAKI;
                if (colorName.equals("DARKMAGENTA")) return Color.DARKMAGENTA;
                if (colorName.equals("DARKOLIVEGREEN")) return Color.DARKOLIVEGREEN;
                if (colorName.equals("DARKORANGE")) return Color.DARKORANGE;
                if (colorName.equals("DARKORCHID")) return Color.DARKORCHID;
                if (colorName.equals("DARKRED")) return Color.DARKRED;
                if (colorName.equals("DARKSALMON")) return Color.DARKSALMON;
                if (colorName.equals("DARKSEAGREEN")) return Color.DARKSEAGREEN;
                if (colorName.equals("DARKSLATEBLUE")) return Color.DARKSLATEBLUE;
                if (colorName.equals("DARKSLATEGRAY")) return Color.DARKSLATEGRAY;
                if (colorName.equals("DARKTURQUOISE")) return Color.DARKTURQUOISE;
                if (colorName.equals("DARKVIOLET")) return Color.DARKVIOLET;
                if (colorName.equals("DEEPPINK")) return Color.DEEPPINK;
                if (colorName.equals("DEEPSKYBLUE")) return Color.DEEPSKYBLUE;
                if (colorName.equals("DIMGRAY")) return Color.DIMGRAY;
                if (colorName.equals("DODGERBLUE")) return Color.DODGERBLUE;
                if (colorName.equals("FIREBRICK")) return Color.FIREBRICK;
                if (colorName.equals("FLORALWHITE")) return Color.FLORALWHITE;
                if (colorName.equals("FORESTGREEN")) return Color.FORESTGREEN;
                if (colorName.equals("FUCHSIA")) return Color.FUCHSIA;
                if (colorName.equals("GAINSBORO")) return Color.GAINSBORO;
                if (colorName.equals("GHOSTWHITE")) return Color.GHOSTWHITE;
                if (colorName.equals("GOLD")) return Color.GOLD;
                if (colorName.equals("GOLDENROD")) return Color.GOLDENROD;
                if (colorName.equals("GRAY")) return Color.GRAY;
                if (colorName.equals("GREEN")) return Color.GREEN;
                if (colorName.equals("GREENYELLOW")) return Color.GREENYELLOW;
                if (colorName.equals("HONEYDEW")) return Color.HONEYDEW;
                if (colorName.equals("HOTPINK")) return Color.HOTPINK;
                if (colorName.equals("INDIANRED")) return Color.INDIANRED;
                if (colorName.equals("INDIGO")) return Color.INDIGO;
                if (colorName.equals("IVORY")) return Color.IVORY;
                if (colorName.equals("KHAKI")) return Color.KHAKI;
                if (colorName.equals("LAVENDER")) return Color.LAVENDER;
                if (colorName.equals("LAVENDERBLUSH")) return Color.LAVENDERBLUSH;
                if (colorName.equals("LAWNGREEN")) return Color.LAWNGREEN;
                if (colorName.equals("LEMONCHIFFON")) return Color.LEMONCHIFFON;
                if (colorName.equals("LIGHTBLUE")) return Color.LIGHTBLUE;
                if (colorName.equals("LIGHTCORAL")) return Color.LIGHTCORAL;
                if (colorName.equals("LIGHTCYAN")) return Color.LIGHTCYAN;
                if (colorName.equals("LIGHTGOLDENRODYELLOW")) return Color.LIGHTGOLDENRODYELLOW;
                if (colorName.equals("LIGHTGREEN")) return Color.LIGHTGREEN;
                if (colorName.equals("LIGHTGREY")) return Color.LIGHTGREY;
                if (colorName.equals("LIGHTPINK")) return Color.LIGHTPINK;
                if (colorName.equals("LIGHTSALMON")) return Color.LIGHTSALMON;
                if (colorName.equals("LIGHTSEAGREEN")) return Color.LIGHTSEAGREEN;
                if (colorName.equals("LIGHTSKYBLUE")) return Color.LIGHTSKYBLUE;
                if (colorName.equals("LIGHTSLATEGRAY")) return Color.LIGHTSLATEGRAY;
                if (colorName.equals("LIGHTSTEELBLUE")) return Color.LIGHTSTEELBLUE;
                if (colorName.equals("LIGHTYELLOW")) return Color.LIGHTYELLOW;
                if (colorName.equals("LIME")) return Color.LIME;
                if (colorName.equals("LIMEGREEN")) return Color.LIMEGREEN;
                if (colorName.equals("LINEN")) return Color.LINEN;
                if (colorName.equals("MAGENTA")) return Color.MAGENTA;
                if (colorName.equals("MAROON")) return Color.MAROON;
                if (colorName.equals("MEDIUMAQUAMARINE")) return Color.MEDIUMAQUAMARINE;
                if (colorName.equals("MEDIUMBLUE")) return Color.MEDIUMBLUE;
                if (colorName.equals("MEDIUMORCHID")) return Color.MEDIUMORCHID;
                if (colorName.equals("MEDIUMPURPLE")) return Color.MEDIUMPURPLE;
                if (colorName.equals("MEDIUMSEAGREEN")) return Color.MEDIUMSEAGREEN;
                if (colorName.equals("MEDIUMSLATEBLUE")) return Color.MEDIUMSLATEBLUE;
                if (colorName.equals("MEDIUMSPRINGGREEN")) return Color.MEDIUMSPRINGGREEN;
                if (colorName.equals("MEDIUMTURQUOISE")) return Color.MEDIUMTURQUOISE;
                if (colorName.equals("MEDIUMVIOLETRED")) return Color.MEDIUMVIOLETRED;
                if (colorName.equals("MIDNIGHTBLUE")) return Color.MIDNIGHTBLUE;
                if (colorName.equals("MINTCREAM")) return Color.MINTCREAM;
                if (colorName.equals("MISTYROSE")) return Color.MISTYROSE;
                if (colorName.equals("MOCCASIN")) return Color.MOCCASIN;
                if (colorName.equals("NAVAJOWHITE")) return Color.NAVAJOWHITE;
                if (colorName.equals("NAVY")) return Color.NAVY;
                if (colorName.equals("OLDLACE")) return Color.OLDLACE;
                if (colorName.equals("OLIVE")) return Color.OLIVE;
                if (colorName.equals("OLIVEDRAB")) return Color.OLIVEDRAB;
                if (colorName.equals("ORANGE")) return Color.ORANGE;
                if (colorName.equals("ORANGERED")) return Color.ORANGERED;
                if (colorName.equals("ORCHID")) return Color.ORCHID;
                if (colorName.equals("PALEGOLDENROD")) return Color.PALEGOLDENROD;
                if (colorName.equals("PALEGREEN")) return Color.PALEGREEN;
                if (colorName.equals("PALETURQUOISE")) return Color.PALETURQUOISE;
                if (colorName.equals("PALEVIOLETRED")) return Color.PALEVIOLETRED;
                if (colorName.equals("PAPAYAWHIP")) return Color.PAPAYAWHIP;
                if (colorName.equals("PEACHPUFF")) return Color.PEACHPUFF;
                if (colorName.equals("PERU")) return Color.PERU;
                if (colorName.equals("PINK")) return Color.PINK;
                if (colorName.equals("PLUM")) return Color.PLUM;
                if (colorName.equals("POWDERBLUE")) return Color.POWDERBLUE;
                if (colorName.equals("PURPLE")) return Color.PURPLE;
                if (colorName.equals("RED")) return Color.RED;
                if (colorName.equals("ROSYBROWN")) return Color.ROSYBROWN;
                if (colorName.equals("ROYALBLUE")) return Color.ROYALBLUE;
                if (colorName.equals("SADDLEBROWN")) return Color.SADDLEBROWN;
                if (colorName.equals("SALMON")) return Color.SALMON;
                if (colorName.equals("SANDYBROWN")) return Color.SANDYBROWN;
                if (colorName.equals("SEAGREEN")) return Color.SEAGREEN;
                if (colorName.equals("SEASHELL")) return Color.SEASHELL;
                if (colorName.equals("SIENNA")) return Color.SIENNA;
                if (colorName.equals("SILVER")) return Color.SILVER;
                if (colorName.equals("SKYBLUE")) return Color.SKYBLUE;
                if (colorName.equals("SLATEBLUE")) return Color.SLATEBLUE;
                if (colorName.equals("SLATEGRAY")) return Color.SLATEGRAY;
                if (colorName.equals("SNOW")) return Color.SNOW;
                if (colorName.equals("SPRINGGREEN")) return Color.SPRINGGREEN;
                if (colorName.equals("STEELBLUE")) return Color.STEELBLUE;
                if (colorName.equals("TAN")) return Color.TAN;
                if (colorName.equals("TEAL")) return Color.TEAL;
                if (colorName.equals("THISTLE")) return Color.THISTLE;
                if (colorName.equals("TOMATO")) return Color.TOMATO;
                if (colorName.equals("TURQUOISE")) return Color.TURQUOISE;
                if (colorName.equals("VIOLET")) return Color.VIOLET;
                if (colorName.equals("WHEAT")) return Color.WHEAT;
                if (colorName.equals("WHITE")) return Color.WHITE;
                if (colorName.equals("WHITESMOKE")) return Color.WHITESMOKE;
                if (colorName.equals("YELLOW")) return Color.YELLOW;
                if (colorName.equals("YELLOWGREEN")) return Color.YELLOWGREEN;
                
                return Color.BLACK;
        }
}
Offline  
Old 11-04-2005, 07:20 AM   #5 (permalink)
Talking BlackBerry Encyclopedia
 
Join Date: Sep 2004
Model: 8700r
Carrier: Rogers
Posts: 221
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Good solution also.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.