欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 游戏 > Processing练习之变换颜色

Processing练习之变换颜色

2024/10/24 4:48:19 来源:https://blog.csdn.net/m0_65111980/article/details/141286687  浏览:    关键词:Processing练习之变换颜色

       案例代码如下:

import generativedesign.*;
import processing.pdf.*;
import java.util.Calendar;

boolean savePDF=false;
int tileCountX =2; //jianbianshuliang
int tileCountY=10;//hangshu

color[] colorsLeft =new color[tileCountY];
color[] colorsRight=new color[tileCountY];
color[] colors;
boolean interpolateShortest = true;//yansemoshikongzhi

void setup(){
  size(800,800);
  colorMode(HSB,360,100,100,100);//sexiang,baohedu,liangdu,toumingdu
  noStroke();
  shakeColors();//suijichanshengmeihangdezuoyoubianyanse
}
void draw(){
  if(savePDF){
    beginRecord(PDF,timestamp()+".pdf");
    noStroke();
    colorMode(HSB,360,100,100,100);
  }
  tileCountX=(int) map(mouseX,0,width,2,100);//map:yingshe
  tileCountY = (int) map(mouseY,0,height,2,10);
  float tileWidth =width/ (float)tileCountX;
  float tileHeight = height/ (float)tileCountY;
  color interCol;//chazhise
  colors =new color[tileCountX*tileCountY];//zongdeyanseshu
  int i=0;
 
  for(int gridY=0;gridY<tileCountY;gridY++)
  {
    color col1 = colorsLeft[gridY];
    color col2=colorsRight[gridY];
    for(int gridX=0;gridX<tileCountX;gridX++){
      float amount=map(gridX,0,tileCountX-1,0,1);//amount:jianbianguocheng(0[left],1[right])
      if(interpolateShortest){
        colorMode(RGB,255,255,255,255);
        interCol=lerpColor(col1,col2,amount);
        colorMode(HSB,360,100,100,100);
      }
      else{
        interCol=lerpColor(col1,col2,amount);
      }
      fill(interCol);//chazhitianchong
      float posX=tileWidth*gridX;
      float posY = tileHeight*gridY;
      rect(posX,posY,tileWidth,tileHeight);
      colors[i]=interCol;
      i++;
    }
  }    if(savePDF){
     savePDF=false;
     endRecord();
  }
}

  void shakeColors(){
    for(int i=0;i<tileCountY;i++){
      colorsLeft[i]=color(random(0,60),random(0,100),100);
      colorsRight[i]=color(random(160,190),100,random(0,100));
    }
  }
  void mouseReleased(){
    shakeColors();//yansesuijichansheng
  }
 
  void keyReleased(){
    if(key=='c'||key=='C')GenerativeDesign.saveASE(this,colors,timestamp()+".ase");
    if(key=='s'||key=='S')saveFrame(timestamp()+"_##.png");
    if(key=='p'||key=='P')savePDF=true;
    if(key=='1')interpolateShortest=true;
    if(key=='2')interpolateShortest=false;
  }
  String  timestamp(){
    Calendar now = Calendar.getInstance();
    return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tyS",now);
  }

       保存代码并运行如图1

                                         图1

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com