在HSB色彩模式下改变不透明度

3

我正在尝试更改描边的不透明度,但不确定如何在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);
  }
}
1个回答

1

您可以给stroke()fill()函数添加第四个参数来设置透明度。以下是一个示例:

void setup(){
  size(500, 500);
  colorMode(HSB, 255);
  background(255);
}

void draw(){
  fill(0, 255, 200, 100);
  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(){
  size(500, 500);
  background(255);
}

void draw(){
  
  r += deltaR;
  g += deltaG;
  b += deltaB;
  
  if(r < 0 || r > 255){
    deltaR *= -1;
    r = constrain(r, 0, 255);
  }
  
  if(g < 0 || g > 255){
   deltaG *= -1;
   g = constrain(g, 0, 255);
  }
  
  if(b < 0 || b > 255){
    deltaB *= -1;
    b = constrain(b, 0, 255);
  }
  
  fill(r, g, b);
  ellipse(mouseX, mouseY, 20, 20);
}

你能解释一下我如何随着时间修改RGB参数吗? - Sebastien Karo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接