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

相关推荐
wb043072018 分钟前
使用 Java 开发 MCP 服务并发布到 Maven 中央仓库完整指南
java·开发语言·spring boot·ai·maven
Rsun045519 分钟前
设计模式应该怎么学
java·开发语言·设计模式
汀、人工智能14 分钟前
[特殊字符] 第2课:字母异位词分组
数据结构·算法·链表·数据库架构··字母异位词分组
5系暗夜孤魂32 分钟前
系统越复杂,越需要“边界感”:从 Java 体系理解大型工程的可维护性本质
java·开发语言
二月夜1 小时前
Spring循环依赖深度解析:从三级缓存原理到跨环境“灵异”现象
java·spring
小O的算法实验室1 小时前
2026年SEVC,面向主动成像卫星任务规划问题的群体智能与动态规划混合框架,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
网安INF1 小时前
数据结构第一章复习:基本概念与算法复杂度分析
数据结构·算法
nbwenren1 小时前
Springboot中SLF4J详解
java·spring boot·后端
幻风_huanfeng1 小时前
人工智能之数学基础:什么是凸优化问题?
人工智能·算法·机器学习·凸优化
三雷科技1 小时前
使用 `dlopen` 动态加载 `.so` 文件
开发语言·c++·算法