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 › Making a color array with colours from the start
Page Index Toggle Pages: 1
Making a color array with colours from the start (Read 827 times)
Making a color array with colours from the start
Aug 3rd, 2009, 5:46pm
 
Hello!
Another easy question I presume:

I tried to make a color array like this
Code:

color[] mycolours = {(255,255,255),(255,255,255)}


but failed. Is there a way to make a color array and load colors into it with one line or do I have to load every single color with new lines?

Thanks!
h
Re: Making a color array with colours from the start
Reply #1 - Aug 3rd, 2009, 10:57pm
 
i would say it has to be

color[] mycolours = {color(255,255,255),color(255,255,255)}

or

color[] mycolours = {#ffffff,#ff0000}
Re: Making a color array with colours from the start
Reply #2 - Aug 4th, 2009, 4:30am
 
Oh I was pretty sure I already tried that one, but apparently not.-)

Thanks alot Cedric!
h
Re: Making a color array with colours from the start
Reply #3 - Aug 6th, 2009, 12:41am
 
Hi hampus,
I sometimes use this code that I got from another sketch.
It loads an image and creates an array of colors from those in the image.
It is quite useful if you want to use an external palette image without defining all the colors in the code.

In this example you must have an image named palette.png in your sketch folder. Then, just to test it, simply click in the processing window to create dots with random colors retrieve from the palette image.

I hope this may be useful.

Code:


int maxpal = 512;
int numpal = 10;

color[] arrColors = new color[maxpal];

boolean bMouseDown = false;

void setup(){
 takecolor("palette.png");

 background(0);
 size(500, 500,P3D);
}


//***********************************
// DRAW
//***********************************

void draw() {

 if(bMouseDown){
   //Picks a random color and draw a circle
   fill(somecolor());
   ellipse(mouseX,mouseY, 20, 20);
 }
}

void mousePressed(){
 bMouseDown = true;
}

void mouseReleased(){
 bMouseDown = false;
}

//***********************************
// COLOR METHODS
//***********************************
color somecolor() {
 // pick some random good color
 return arrColors[int(random(numpal))];
}

void takecolor(String fn) {
 PImage b;
 b = loadImage(fn);
 image(b,0,0);

 for (int x=0;x<b.width;x++){
   for (int y=0;y<b.height;y++) {
     color c = get(x,y);
     boolean exists = false;
     for (int n=0;n<numpal;n++) {
       if (c==arrColors[n]) {
         exists = true;
         break;
       }
     }
     if (!exists) {
       // add color to pal
       if (numpal<maxpal) {
         arrColors[numpal] = c;
         numpal++;
       }
       else {
         break;
       }
     }
   }
 }
}

Re: Making a color array with colours from the start
Reply #4 - Aug 9th, 2009, 12:24pm
 
Hi Dyve!

Neat idea, I guess that will be very useful when you are about to load alot of colours, or got a image with colours that fits with each other. I'll remember it!

Thanks
h
Page Index Toggle Pages: 1