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

相关推荐
百事老饼干2 分钟前
Java[面试题]-真实面试
java·开发语言·面试
customer0810 分钟前
【开源免费】基于SpringBoot+Vue.JS医院管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·开源·intellij-idea
2402_8575893620 分钟前
SpringBoot框架:作业管理技术新解
java·spring boot·后端
HBryce2423 分钟前
缓存-基础概念
java·缓存
一只爱打拳的程序猿38 分钟前
【Spring】更加简单的将对象存入Spring中并使用
java·后端·spring
幸运超级加倍~39 分钟前
软件设计师-上午题-16 算法(4-5分)
笔记·算法
杨荧39 分钟前
【JAVA毕业设计】基于Vue和SpringBoot的服装商城系统学科竞赛管理系统
java·开发语言·vue.js·spring boot·spring cloud·java-ee·kafka
minDuck41 分钟前
ruoyi-vue集成tianai-captcha验证码
java·前端·vue.js
yannan201903131 小时前
【算法】(Python)动态规划
python·算法·动态规划