Fixing color to squares with eyedropper

Hi, I’m not very good at processing and am stuck with this code.
I drew 5 squares with the color of the x and y position of the mouse. Next, I want to state that with the first mouseclick the color the mouse is on at that moment will be fixed in the first square. and the second click has to fill the second square with the color of the mouse position. Anyone have any clue how to do this? thank so much in advance!

PImage salad;
import processing.pdf.*;
boolean record;

void setup(){
  size(875, 875);
  noStroke();
  salad = loadImage("salad.jpg");
}

void draw() {
  if(record){
    beginRecord(PDF, "frame-####.pdf");
  }
  image(salad, 0,0);
  color a = get (mouseX, mouseY);
  color b = get (mouseX, mouseY);
  color c = get (mouseX, mouseY);
  color d = get (mouseX, mouseY);
  color e = get (mouseX, mouseY);
  fill(a);
  rect(100,100,100,100);
  fill(b);
  rect(100,250,100,100);
  fill(c);
  rect(100,400,100,100);
  fill(d);
  rect(100,550,100,100);
  fill(e);
  rect(100,700,100,100);
  
  if(record){
    endRecord();
    record = false;
  }
}

void mouseClicked() { //I want the square filled in with the color my mouse is on at that time
  
}

void keyPressed() { //if you press enter the frame gets saved as a pdf
  record = true; 
}

before setup
int counter;

define all colors before setup

color a,b,c,d,e;

it’s all black then

OR

color a=color(111),b=color(255,0,0),c,d,e; iirc

then in function mouseClicked() or mousePressed() say

counter ++

if(counter==0)
a=....;
else if(counter==1)
b=......;
else if.....
....
....

Thank you so much for your help this is really nice. but I’m wondering do I have to call the different rectangles on the dots after a = … ?
because then the rect’s should have different names or not? idk how to change that.

this entire section stays the same in draw():

I am sorry!

Yeah, but it’s saying it needs a variable there… i don’t know which one haha

show your entire code then

basically when you say color a,b,c… before setup,

don’t declare a,b,c… somewhere else

so in mouseClicked() just say a = ............. NOT color a =.......

also in draw() don’t say color a = ....

Aaah right! that was the mistake, still had the colors in draw…
that’s amazing thank you!
do you think there is a possibility to still see the colors in the squares when the mouse is hovering over the image, before the click. so you can realy select a color?

I think you need this section then in draw before the rects

if(counter==0)
a=....;
else if(counter==1)
b=......;
else if.....
....
....

BUT without the counter++; of course

wauw i got it, thank you so much for your help!

1 Like