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 › save rgbstring in an array
Page Index Toggle Pages: 1
save rgbstring in an array (Read 499 times)
save rgbstring in an array
May 23rd, 2008, 3:04pm
 
hello
i'm desperately juggling with array-syntax.
i simply want to create a new array with 6 different color values which i can later access through
background(colorarray[0]...) something
in every arrayposition a string lookalike 255,0,255 should be stored but I'm completely confused by getting various errors could somebody give me a hint on an elegant solution of storin g a fixed amount of given colors to be accessed at any time easily?
thanks for reading and helping
ingles (:
Re: save rgbstring in an array
Reply #1 - May 23rd, 2008, 3:27pm
 
Do you need the colours to be stored as strings for any particular reason?

It'd be easier to store colors in an array of colors:

Code:
color[] colors;

void setup()
{
//...
colors=new color[6];
color[0]=color(255,0,255);
color[1]=color(255,255,0);
color[2]=color(0,255,255);
// etc for [3] [4] and [5].
}

void draw()
{
background(colors[0]);
}
Re: save rgbstring in an array
Reply #2 - May 23rd, 2008, 4:02pm
 
thanks JohnG
i was thinking a bit too complicated thanks for the hint using color() but still I get the "Expecting RBRACK, found '0'" error by using your piece of code... I'm a bit confused about the general array syntax in a way... maybe there is any need for a for-loop?!
Re: save rgbstring in an array
Reply #3 - May 23rd, 2008, 4:43pm
 
JohnG probably typed in the browser, he made a little mistake, init. is colors[0] (resp. 1, 2, etc.), not color[0].

And yes, 'for' loops are a natural way to use arrays.
Re: save rgbstring in an array
Reply #4 - May 23rd, 2008, 4:44pm
 
oops I missed an s off the color[ part, it shoudl be colors[0] etc..
Re: save rgbstring in an array
Reply #5 - May 26th, 2008, 12:28pm
 
oh thx
now everything is working concerning this but I'm stuck at a further problem.
I have another array which randomly chooses colors for a determined amount of objects out of this colorarray.
but I'm drawing the objects(rects) with 2 for-loops for columns and rows.
But if i want to assign every object from 0-20 (e.g. in a 5x4 grid) its own color, i have to assign some kind of ID to every object that my draw-function can look like this in the end:
       fill(colors[definedcolorarray[ID].dc]);

how can i assign a ID to every object?
during creating the array like
 for(int i=0; i<(rows*cols); i++){
   rectcolorarray[i] = new rectcolor();
}

I'm really stuck...
plz help
or ask for the whole code if necessary
Re: save rgbstring in an array
Reply #6 - May 26th, 2008, 1:39pm
 
I am not sure what you are trying to do, but this code snippet works:
Code:
color[] colors;

int COLOR_NB = 6;
int ROW_NB = 5, COL_NB = 4;
int SIZE = 20, MARGIN = 5;
Rect[] rect = new Rect[ROW_NB * COL_NB];

void setup()
{
size(200, 200);
int c = 0;
colors = new color[COLOR_NB];
colors[c++] = #FF0000;
colors[c++] = #00FF00;
colors[c++] = #0000FF;
colors[c++] = #FFFF00;
colors[c++] = #00FFFF;
colors[c++] = #FF00FF;

for (int i = 0; i < ROW_NB * COL_NB; i++)
{
rect[i] = new Rect(int(random(0, COLOR_NB)));
}
}

void draw()
{
for (int i = 0; i < ROW_NB; i++)
{
for (int j = 0; j < COL_NB; j++)
{
fill(colors[rect[i * COL_NB + j].colorIndex]);

rect(MARGIN + (SIZE + MARGIN) * i, MARGIN + (SIZE + MARGIN) * j, SIZE, SIZE);
}
}
}

class Rect
{
int colorIndex;
Rect(int _colorIndex)
{
colorIndex = _colorIndex;
}
}

Now, you can put more information in Rect, like its coordinates and color index, and let this object draw itself.
Page Index Toggle Pages: 1