Does anyone know how to fix the background color?

Hello! I setup the color could be changed from 2 pink colors. I hope after dragging to a color that I like (between color A and color B), I can decide and make the color as background color (maybe by mousePressed()) And then the color will not change when the mouse move again. I am not sure how to add the void mousePressed and hopefully you could help! Thanks! :slight_smile:

void setup() {
size(600, 600);
}

void draw() {
color colorA = color(#F2ACB9);
color colorB = color(#f27999);
float value = map(mouseX, 0, width, 0, 1);
color colorC = lerpColor(colorA, colorB, value);
background(colorC);
}

Did you try out a similar approach as the example on the mousePressed() reference page?

1 Like

Thank you! But my problem is that I need to change between the range of color A and color B, instead of switching from color A to color B.

Hi

Change color with mouse

void setup () {
size (600,600);
colorMode(HSB);
}
void draw(){
for(int i= 500; i >0; i=i -2) {
  background ((i/2) + (mouseX/3), 255,255);
}
}

1 Like

Do you mean that you want to be able to drag the mouse and the background color changes while the mouse is dragging across screen?
:nerd_face:

1 Like

Hi

Your sketch is working in think you picked close colors value

https://funprogramming.org/82-Program-a-gradient-of-colors.html

void setup() {
size(600, 600);
}

void draw() {
color colorA = color(#F200B0);
color colorB = color(#f279f9);
float value = map(mouseX, 0, width, 0, 1);
color colorC = lerpColor(colorA, colorB, value);
background(colorC);
}

1 Like

Hi

With mouse pressed



void setup() {
  
size(600, 600);
}

void draw() {

}
void mousePressed() {
 color colorA = color(#F200B0);
color colorB = color(#f279f9);
float value = map(mouseX, 0, width, 0, 1);
color colorC = lerpColor(colorA, colorB, value);
background(colorC);
  
}

1 Like

Hello! Yes, I hope the color would change while the mouse is dragging across screen. But actually, I hope after dragging to a color that I like (between color A and color B), I can decide and make the color as background color (maybe by MouseClicked?) The color will not change when the mouse move again. But I don’t know how to do that function. Thank you~

Hello @learningprocessing23,

Consider this in the draw() loop:

if(mousePressed)
    {
    // change colorC here
    }
  background(colorC);

Keep in mind that some you will have to declare some variables globally and change them inside the if() statement.

https://processing.org/examples/variablescope.html

Once I released the mouse the color was applied to the background.

References:

:)

1 Like

@learningprocessing23

With the sketch I provided color won’t change unless you press mouse and move it

Moving mouse only won’t change the color …what you need exactly?

In this video close idea of what you want

https://funprogramming.org/81-How-to-read-the-color-of-a-pixel.html

2 Likes

Thank you! This turtorial is very helpful!!

1 Like