We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › defining color using a txt file
Page Index Toggle Pages: 1
defining color using a txt file (Read 535 times)
defining color using a txt file
Jul 9th, 2009, 12:02pm
 
hello,

ich have a txt file made of this structure:
11111 #FF00FF
11112 #FF0000
11115 #FF0FF0
and so on.
so first you have a number and seperated by a tab there is a hexcode.
Now inside of my processing sketch one can type a number (like 11111 oder 11112). those are saved in a buffer-string and then converted to an int. And now: When pressing "Enter" i want to fill a rect in the matching number above. so when typing 11111 i want to fill it with FF00FF and so on.
how can i get processing to find the relevant number inside of the sketch?

i once had a different sketch with this txt file....
01 #FF0000
02 #FFFFFF
03 #FF00FF

i therefore used this sketch:

Code:
  void FillFunction() {
   String[] lines = loadStrings("colors.txt");
   int[] colorList = new int[lines.length];
   for (int i = 0; i < lines.length; i++) {
     // I assume that it's tabs separating the columns
     int tab = lines[i].indexOf(TAB);
     // Skip over the TAB character, and the # character
     String stuff = lines[i].substring(tab + 2);
     // Convert the color from hex
     int c = unhex(stuff);
     // Put it in the list, the 0xff part makes it opaque
     colorList[i] = 0xFF000000 | c;
     int_color = colorList[int_plz - 1];
   }
 }

does anyone has an idea how to solve this problem?
may use my sktech above and just change some parameters?
i'm glad for any help!!!!
Re: defining color using a txt file
Reply #1 - Jul 9th, 2009, 12:33pm
 
What you described is a special case of "mapping"

01 -> #COLOR
02 -> #color2
03 -> #another color

So, you made an array, where the 1th index was COLOR, 2th index and so on.

I assume you're getting to the point where the "keys", as they're called, are not continuous, so this method doesn't make sense.

Depending on what you're optimizing for, there are a few simple ways to do what is known as a "map".

Learn about it here!

http://processing.org/learning/topics/hashmapclass.html
Re: defining color using a txt file
Reply #2 - Jul 9th, 2009, 2:42pm
 
so do you might give me some starting help?
why does it not make sense if the keys are not continuous?
your're right, they're are not.

but i don't want to create new colors. there are 10 fixed keys and ten fixed colours.
if the  key typed in is not in the list, the rect i want to fill does get no color....

so i cannot use my piece of code posted above?
Re: defining color using a txt file
Reply #3 - Jul 9th, 2009, 7:39pm
 
Well, I think this could work:
Code:

private int[] colorList;
private String[] colorLines;
int getFillColor(String typed) {
if (colorLines==null){ //Only load first time
String[] lines = loadStrings("colors.txt");
colorList = new int[lines.length];
colorLines = new String[lines.length];
for (int i = 0; i < lines.length; i++) {
// I assume that it's tabs separating the columns
int tab = lines[i].indexOf(TAB);
// Skip over the TAB character, and the # character
String stuff = lines[i].substring(tab + 2);
// Convert the color from hex
int c = unhex(stuff);
// Put it in the list, the 0xff part makes it opaque
colorList[i] = 0xFF000000 | c;
colorLines[i] = lines[i].substring(0,tab);
}
}
for(int k = 0; k < colorLines.length; k++){
if (typed.equalsIgnoreCase(colorLines[k])){
return colorList[k]; //Note the difference.
}
}
return color(0); //Default if not found
}
Page Index Toggle Pages: 1