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;
}
}
}
}
}