Processing练习之变换颜色

案例代码如下:

import generativedesign.*;

import processing.pdf.*;

import java.util.Calendar;

boolean savePDF=false;

int tileCountX =2; //jianbianshuliang

int tileCountY=10;//hangshu

color\[\] colorsLeft =new colortileCountY;

color\[\] colorsRight=new colortileCountY;

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 colortileCountX\*tileCountY;//zongdeyanseshu

int i=0;

for(int gridY=0;gridY<tileCountY;gridY++)

{

color col1 = colorsLeftgridY;

color col2=colorsRightgridY;

for(int gridX=0;gridX<tileCountX;gridX++){

float amount=map(gridX,0,tileCountX-1,0,1);//amount:jianbianguocheng(0left,1right)

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);

colorsi=interCol;

i++;

}

} if(savePDF){

savePDF=false;

endRecord();

}

}

void shakeColors(){

for(int i=0;i<tileCountY;i++){

colorsLefti=color(random(0,60),random(0,100),100);

colorsRighti=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("%1ty%1tm%1td_%1tH%1tM%1tyS",now);

}

保存代码并运行如图1

图1

相关推荐
朦胧之9 小时前
AI 编程-老项目改造篇
java·前端·后端
kisshyshy14 小时前
🍦 雪糕、食堂、火车厢:三幅漫画吃透栈、队列与链表
javascript·算法
程序猿大帅14 小时前
别再只当调包侠了:用 Spring AI 落地 Function Calling,我被大模型硬生生砸出了三个大坑
java
程序员晓琪15 小时前
约定大于配置:基于 Java 包名自动生成 API 版本路由的最佳实践
java·spring boot·后端
Flittly15 小时前
【AgentScope Java新手村系列】(11)中断与恢复
java·spring boot·spring
众少成多积小致巨15 小时前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
东坡白菜16 小时前
破局全栈:前端开发的Java入门实战记录—JPA(2)
java·后端
猿人谷21 小时前
不只是 CPU 阈值:STAR 如何用 GAT + Transformer 做容器级自动扩缩容?
人工智能·算法
SimonKing1 天前
艹,维护AI写的代码,我心态崩了......
java·后端·程序员
用户298698530141 天前
Java Word 文档样式进阶:段落与文本背景色设置完全指南
java·后端