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 › data table for color values
Page Index Toggle Pages: 1
data table for color values (Read 947 times)
data table for color values
Nov 7th, 2008, 11:07am
 
I have a data table with two columns. in the first column in have values from 0 to 999. in the second column are color values like #111111.
so consequently it looks like this.

0     #FF0000
1     #FFFF00
2     #FF00FF
3     #FFFFFF

now, i want to use this color values to fill an ellipse. depending on the number in the first column the color value in the second column has to be used.

in several examples i only found a way, to use all values of the data table at the same time. but i actually only need one value out of those 1000. what sort of function or piece of code do i have to use to compare the value in my application with the value in my data table?

I'm glad for help and many thanks in advance!!
Re: data table for color values
Reply #1 - Nov 7th, 2008, 7:56pm
 
Code:

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


Then you can use fill(colorList[4]) or whatever.. Or if you're comparing against another color (I didn't quite follow your question), you can go through colorList to see what matches.
Re: data table for color values
Reply #2 - Nov 8th, 2008, 2:50pm
 
thanks a lot, thats what i was looking for.

there's just one more problem using this script.

when using my application you are supposed to type in a number. let' s say 29. And after pressing ENTER a rect appears in the middle of the screen and this rect is supposed to be filled with a certain color out of the data table. so when typing 29, the programm has to pick up the color that is in one row with 29.

How can I compare the value of an int to a value inside my data table?

using colorList [29] might be a possibilitly, but the numbers are not from 0 to  999, as there are skips like

520
522
540

so the 540. position in the data table must not be equal to the number 540.
If there is a possibility to get to the number one has typed in it would be great!!!


Re: data table for color values
Reply #3 - Nov 8th, 2008, 4:04pm
 
There are two possibilities:
- the simpler is to just read as fry shown, but also taking the index from the file, and using this index to put the color in the table at the right place:

read line
separate it at tab place, into index and color
colorList[index] = color

You will have holes, but that's not important, an array of 1000 entries isn't a big deal today. You will have to manage when user types a number corresponding to an empty row.

- Another solution would be to use a HashMap, a good way to handle sparse arrays.
Re: data table for color values
Reply #4 - Nov 9th, 2008, 6:10pm
 
ahh, i don't know what i made wrong, but when executing the script you've written i get this error:
java.lang.NumberFormatException: For input string: "FF0000 "

this is my script

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 +1);  
 // Convert the color from hex
 int c = unhex(stuff);
 // Put it in the list, the 0xff part makes it opaque
 colorList[i] = 0xFF000000 | c;
}


and this is my colors.txt file:

01#FF0000
02#FFFF00
03#FF00FF
04#FFFFFF
05
06#00FFFF

between the number 01, 02 etc. and the hex codes is a Tab.



Re: data table for color values
Reply #5 - Nov 9th, 2008, 8:07pm
 
the unhex manual page uses an 8 hex digit input, maybe that's the problem (haven't tested it)

your colors.txt file also looks to be missing the tab separator that the code's expecting (although that may be a pasting problem). println("Stuff: [" + stuff + "]"); before the unhex() to make sure.

> between the number 01, 02 etc. and the hex codes is a Tab.  

ah, ok 8)

> For input string: "FF0000 "

trailing space?
Re: data table for color values
Reply #6 - Nov 9th, 2008, 11:14pm
 
the colors.txt is indeed just a pasting problem. in my real file there is tab between each 01, 02 etc. and the hex code.

i tried: println("Stuff: [" + stuff + "]");

and it gave me:

Stuff: [ #FF0000 ]
Stuff: [ #FFFF00 ]
Stuff: [ #FF00FF ]
Stuff: [ #FFFFFF]

but when using the unhex it still gives me the error:
java.lang.NumberFormatException: For input string: "FF0000 "

i don't know what might be wrong...
Re: data table for color values
Reply #7 - Nov 10th, 2008, 7:34am
 
Code:
String[] lines = {
"01#FF0000",
"02#FFFF00",
"03#FF00FF",
"04#FFFFFF",
"06#00FFFF",
};  
int[] colorList = new int[1000];
for (int i = 0; i < lines.length; i++) {  
 // I assume that it's tabs separating the columns  
 int tab = lines[i].indexOf(TAB);  
 // Get index value
 String idx = lines[i].substring(0, tab);
 int index = int(idx);
 // Skip over the TAB character, and the # character  
 String stuff = lines[i].substring(tab + 2);  
// println(index + "-" + stuff + ".");
 // Convert the color from hex  
 int c = unhex(stuff.trim());  
 // Put it in the list, the 0xff part makes it opaque  
 colorList[index] = 0xFF000000 | c;  
}
exit();

Several problems: we needed tab+2, not tab+1. We needed to trim the input (or remove the extra spaces in the file). You should not leave numbers without color values, or you should handle this case (no "stuff" value). So I made as suggested, a big array and put color at the read index.
Re: data table for color values
Reply #8 - Nov 10th, 2008, 3:49pm
 
don't know what's going wrong with me...when executing the script it highlightes the
Sting idx = lines [i].substring (0,tab);

and as an error i get:

java.lang.StringIndexOutOfBoundsException: String index out of range: -1


Re: data table for color values
Reply #9 - Nov 10th, 2008, 7:46pm
 
As I wrote, you must either get rid of lines without color values (and probably no tabs, hence the -1 value in tab variable), or handle this case (testing the value, as we should have done anyway...).
Page Index Toggle Pages: 1