FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Suggestions
   Software Suggestions
(Moderator: fry)
   script / eval style functionality
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: script / eval style functionality  (Read 2076 times)
TomC

WWW
script / eval style functionality
« on: Mar 22nd, 2004, 1:39pm »

on Mar 22nd, 2004, 7:00am, lunetta wrote:
Hello to all
 
I have a code that defines three colors:
 
  color c1 = color (225, 15,10);
  color c2 = color (35, 200, 5);
  color c3 = color (35, 120, 120);
 
then I'm drawing some stuff; the fill of these stuff needs to be one of those 3 predefined colors, at random; or c1, or c2 or c3;
 
1) is it possible to do something like
 
String randc = "c"+int(random(3)+1);
 
and then use the String randc as a color fill -  fill(randc); ? how?
 

 
My personal opinion is that eval() style functions are the wrong way to do things, but since they are possible in Java I thought I would post this.  It might help with quick porting of flash projects to processing
 
Code:

 
// if you want to access these by name,  
// using reflection, they have to be public
public color c1, c2, c3;
 
void setup() {
  c1 = color (225, 15,10);  
  c2 = color (35, 200, 5);  
  c3 = color (35, 120, 120);
}
 
void loop() {
  background(0);
 
  // choose c1, c2, or c3
  String randColorName = "c"+int(random(2.9999999)+1);
 
  // get color with given name
  color randColor = colorForName(randColorName);
 
  // apply color
  fill(randColor);
 
  rect(10,10,width-20,height-20);
}
 
// uses reflection API to pull out value  
// of given field name... field must be public
// returns black if it fails
color colorForName(String name) {
  int col = 0;
  try {
    col = this.getClass().getField(name).getInt(this);
  }
  catch(Exception e) {
    e.printStackTrace();
  }
  return col;
}
 
 
REAS

WWW
Re: script / eval style functionality
« Reply #1 on: Mar 24th, 2004, 7:55pm »

A very easy way to solve the initial issue requested by lunetta is to make an array of colors and then access a member of this array with a random value converted to an int.
 
cello

marcello3d WWW
Re: script / eval style functionality
« Reply #2 on: Sep 14th, 2004, 2:19am »

If you want to go all out, check out rhino for scripting java/processing.
 
Marcello
« Last Edit: Sep 14th, 2004, 2:19am by cello »  
Pages: 1 

« Previous topic | Next topic »