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

}

保存代码并运行如图1

图1

相关推荐
Reese_Cool2 分钟前
【C语言二级考试】循环结构设计
android·java·c语言·开发语言
儿创社ErChaungClub12 分钟前
解锁编程新境界:GitHub Copilot 让效率翻倍
人工智能·算法
前端西瓜哥16 分钟前
贝塞尔曲线算法:求贝塞尔曲线和直线的交点
前端·算法
小灰灰爱代码24 分钟前
C++——求3个数中最大的数(分别考虑整数、双精度数、长整数的情况),用函数模板来实现。
开发语言·c++·算法
严文文-Chris27 分钟前
【设计模式-享元】
android·java·设计模式
南加第一划水27 分钟前
Leetcode 每日一题:Evaluate Division
算法·leetcode·职场和发展
Flying_Fish_roe43 分钟前
浏览器的内存回收机制&监控内存泄漏
java·前端·ecmascript·es6
c#上位机1 小时前
C#事件的用法
java·javascript·c#
逝去的秋风1 小时前
【代码随想录训练营第42期 Day61打卡 - 图论Part11 - Floyd 算法与A * 算法
算法·图论·floyd 算法·a -star算法