在 HSB 颜色模式中更改不透明度

我正在尝试更改笔画的不透明度,但不确定如何在 HSB 颜色模式下进行。或者,如果我切换到 RGB 模式,如何让颜色随时间变化?


这是我的代码:(任何帮助将不胜感激)


float rainbow=0;

int dir=1;


void setup() {

  size(600, 600);

  background(0);

  colorMode(HSB, 255);

 } 


void draw() {

  if ( rainbow < 255 && dir==1) {

     rainbow++;

  }


  if ( rainbow > 0 && dir==-1) {

    rainbow--;

  }


  if ( rainbow == 255) {

    dir*= -1;

  }


  if ( rainbow == 0) {

    dir*= -1;

  }


  stroke(rainbow, 255, 200);

  line(mouseX,mouseY,width/2,height/2); 


  if (mousePressed){

  stroke(rainbow, 255, 200);

  line(mouseX,mouseY,0,mouseX);

  }


  if (mousePressed){

  stroke(rainbow, 100, 200);

  line(mouseX,mouseY,600,mouseX);

  }

}


绝地无双
浏览 196回答 1
1回答

慕的地10843

您可以给stroke()orfill()函数指定不透明度的第四个参数。这是一个例子:void setup(){&nbsp; size(500, 500);&nbsp; colorMode(HSB, 255);&nbsp; background(255);}void draw(){&nbsp; fill(0, 255, 200, 100);&nbsp; ellipse(mouseX, mouseY, 20, 20);}请注意,您还可以使用 RGB 模式并随着时间的推移修改 R、G 和 B 参数。这比循环 HSB 值要复杂一些,但您可以创建任何您想要的模式。这是一个例子:float r = 0;float g = 0;float b = 0;float deltaR = 1;float deltaG = 2;float deltaB = 4;void setup(){&nbsp; size(500, 500);&nbsp; background(255);}void draw(){&nbsp;&nbsp;&nbsp; r += deltaR;&nbsp; g += deltaG;&nbsp; b += deltaB;&nbsp;&nbsp;&nbsp; if(r < 0 || r > 255){&nbsp; &nbsp; deltaR *= -1;&nbsp; &nbsp; r = constrain(r, 0, 255);&nbsp; }&nbsp;&nbsp;&nbsp; if(g < 0 || g > 255){&nbsp; &nbsp;deltaG *= -1;&nbsp; &nbsp;g = constrain(g, 0, 255);&nbsp; }&nbsp;&nbsp;&nbsp; if(b < 0 || b > 255){&nbsp; &nbsp; deltaB *= -1;&nbsp; &nbsp; b = constrain(b, 0, 255);&nbsp; }&nbsp;&nbsp;&nbsp; fill(r, g, b);&nbsp; ellipse(mouseX, mouseY, 20, 20);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java