Hello,
For a gradient example of Illustrator or Photoshop, we can easily change the center point of two colors. I am trying to make a similar thing in Processing.
What I’ve tried in my code is:
- When mouseX is 0, the first color(c1) is dominant, like amt= 0.
- When mouseX is at “width/2”, lerpColor(the first color, the second color, 0.5);
- When mouseX is equal to “width” the second color(c2) is dominant, like amt =1.
OR it would be great if the center point of the gradient is following mouseX.
My code seems like it is working, but it is not perfect. and I feeling like there must be a right way to do this. I appreciate if you could help me.
Here’s my code:
int X_AXIS = 1;
color c1, c2;//gradient
PGraphics gradientBG;void setup(){
size(512, 512, P2D);
gradientBG = createGraphics(width, height);c1 = color(204, 102, 0);
c2 = color(0, 102, 153);
}void draw(){
setGradient(gradientBG, 0, 0, width, height, c1, c2, X_AXIS);
image(gradientBG, 0, 0);
}//starting x, starting y
void setGradient(PGraphics pg1, int x, int y, float w, float h, color c1, color c2, int axis){
pg1.beginDraw();
pg1.noFill();
if(axis == X_AXIS){
for(int i = x; i <=x+w; i++){float amt = map(i, x, x+w, 0, 1); color c = lerpColor(c1, c2, amt/map(mouseX*2, 0, 512, 0, 1)); pg1.stroke(c); pg1.line(i, y, i, y+h);
}
}
pg1.endDraw();
}