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

相关推荐
shehuiyuelaiyuehao4 分钟前
算法50,分治归并,数组中的逆序对
java·算法
烽学长5 分钟前
(附源码)基于Springboot+vue的图书阅读分享系统的设计与实现
java·spring boot·后端
Ivanqhz11 分钟前
数据搬运是性能关键
java·服务器·网络·人工智能·深度学习
青山木22 分钟前
RocketMQ 入门到原理(三):消息存储原理
java·后端·中间件·架构·rocketmq
MayBaymax23 分钟前
Spring AI Alibaba Graph 实战:客服工单智能处理
java·ai·ai编程
阳明山水41 分钟前
校准分位数驱动智能库存决策
人工智能·深度学习·算法·机器学习·架构
光依旧1 小时前
MCP实战手记系列(五):让工具返回一个能点的界面(文末附github源码链接)
java·spring ai·前端集成·mcp·mcp apps
APIshop1 小时前
淘宝选品接口实战:从召回、详情、佣金到转链的 Java 接入笔记
java
泡茶喝茶写代码1 小时前
量化数据进阶:多维实战篇(第 11 篇):历史涨跌停价:涨跌停序列与止损线测算
java·python·股票数据api·股票数据·股票数据api接口·股票api数据接口·股票量化数据api
景熙55231 小时前
单体项目编写思路和落地形式
java·开发语言