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 › editing code from the outside, color array
Page Index Toggle Pages: 1
editing code from the outside, color array (Read 199 times)
editing code from the outside, color array
Mar 18th, 2009, 10:06pm
 
Sorry for the vague topic, but i dontknow how to explain it any better.  Ive got the following problem.

Im using a color array to fill an object with a gradient. Works finde when i edit the array inside the code. But know i want to give the user the possibility to edit the array when running the sketch without editing the code.

my first thought was to use a ControlP5 textfield where i can type in different colors like  
String gradient = "#000000, #ff0000, #00ff00"  

like i would in the array it self. And tried to add this String to the color array somehow. color[] colors ={"gradient") ; but yeah, was obvious this would work...
do you have any idea how i can give users the possibility to edit the color array and add as many colors as they want? Any ideas? Thanks!
Re: editing code from the outside, color array
Reply #1 - Mar 19th, 2009, 10:26am
 
Example:
Code:
void setup()
{
 size(200, 100);

 // The string as typed by the user
 String gradient = "#000000, #ff0000, #00ff00";
 // Although I would just ask for a simpler input:
 // (less to type!)
 String gradient2 = "0000FF ff00FF FFff00";
 
 // Anyway, I can transform the former to the latter!
 String gradient1 = gradient.replaceAll("[#,]", "");
 
 // And get an array out of that:
 String[] stringGradientStops1 = gradient1.split(" ");
 String[] stringGradientStops2 = gradient2.split(" ");
 
 color[] ca = GetGradientStops(stringGradientStops1);
 ShowColors(ca, 10);
 ca = GetGradientStops(stringGradientStops2);
 ShowColors(ca, 50);
}

color[] GetGradientStops(String[] strGrStops)
{
 color[] colors = new color[strGrStops.length];
 for (int i = 0; i < strGrStops.length; i++)
 {
   try
   {
     // Convert to opaque color
     println("Converting " + strGrStops[i] + " (" + i + ")");
     colors[i] = 0xFF000000 + unhex(strGrStops[i]);
     println("Got " + hex(colors[i]));
   }
   catch (NumberFormatException nfe)
   {
     println("Problem with " + strGrStops[i] + " (" + i + ")");
     colors[i] = #FFFFFF;
   }
 }
 return colors;
}

void ShowColors(color[] colors, int yPos)
{
 for (int i = 0; i < colors.length; i++)
 {
   fill(colors[i]);
   rect(10 + i * 40, yPos, 30, 30);
 }
}
Re: editing code from the outside, color array
Reply #2 - Mar 19th, 2009, 10:57am
 
WOW! thats great. I would have never come up with something like this. Big thanks again for all your help!
Page Index Toggle Pages: 1