Colour Palette from Image
Answered
- Need more info
- Answered
- Working on it
in
Programming Questions
•
3 years ago
I have a set of images that are loaded randomly during a program. From these images I'm creating a palette of colour taken from the current image loaded. The code works except that I'm only creating a palette of colour from the first image loaded at the beginning of the program. I want to be able to create a new palette from each new image loaded during the running of the program - so resetting the value of the return function in somecolour() each time a new image is loaded. Any hints at where I'm going wrong will be much appreciated. Thanks.
PImage a;
int maxpal = 255;
int numpal = 0;
color[] goodcolor = new color[maxpal];
int f;
int count;
void setup() {
size(400, 200);
background(255);
f = int(random(0,9));
takecolor("bg_" + nf(f, 3) + ".jpg");
}
void draw() {
count ++;
if (count > 73) {
restart();
}
strokeWeight(20);
color myc = somecolor();
fill (myc);
rect(190, 0, 200, 200);
println(myc);
}
void restart() {
count = 0;
background(255);
f = int(random(0,9));
takecolor("bg_" + nf(f, 3) + ".jpg");
}
// COLOUR ROUTINE ----------------------------------------------------------------
color somecolor() {
// PICK SOME RANDOM COLOR
return goodcolor[int(random(numpal))];
}
void takecolor(String fn) {
a = loadImage(fn);
image(a,0,0);
for (int x=0;x<a.width;x++){
for (int y=0;y<a.height;y++) {
color c = a.get(x,y);
boolean exists = false;
for (int n=0;n<numpal;n++) {
if (c==goodcolor[n]) {
exists = true;
break;
}
}
if (!exists) {
// add color to pal
if (numpal<maxpal) {
goodcolor[numpal] = c;
numpal++;
}
}
}
}
}
1