The reason your expression is not working is because doing color+number is simply concatenation. This means that if it’s basically looking for the value of the variable color, and then adding a number to it, not dynamically calling the variable color01. There are 2 ways to fix this.
The first way (and this is the WRONG way) is to use eval(). Using eval() basically forces whatever is inside the parenthesis to execute as code, so the string “color02” wrapped in eval() would actually return the value of the color02 variable. Again, this is NOT THE RIGHT WAY to do this, as eval() is typically considered dangerous for security reasons. While it most likely would be okay in a small project that you never plan on sending out, it’s typically best to try and develop without relying on eval().
The expression w/ eval() would look like this:
number = 01;
myPath = "~/Desktop/colors.txt";
try{
$.evalFile (myPath);
r = parseInt((eval(color+number)).substr(0,2),16)/255;
g = parseInt((eval(color+number)).substr(2,2),16)/255;
b = parseInt((eval(color+number)).substr(4,2),16)/255;
[r,g,b,1]
}catch (err){
[1,1,1,1]
}
The second, and better, method is to construct a new variable inside $.evalFile() which can then be used in the r,g and b variable lines as a shorthand. This way you just change the variable c to whatever color you would like to retrieve. The code for this method looks like this:
myPath = "~/Desktop/colors.txt";
try{
$.evalFile (myPath);
var c = color01;
r = parseInt(c.substr(0,2),16)/255;
g = parseInt(c.substr(2,2),16)/255;
b = parseInt(c.substr(4,2),16)/255;
[r,g,b,1]
}catch (err){
[1,1,1,1]
}
Best of luck, friend.
David Conklin
mographcode.tumblr.com