代码+注释如下:
PImage selected;
float hueLv;
//苹果那张图里的颜色变化用的是HSB色彩模式下的色相变化,也就是hue()
int spacing=12;
//相邻圆角矩形中心点之间的距离,可以自己调整,然后看看有啥变化
int sideLen;
//圆角矩形的边长
void setup() {
selected=loadImage("guita.png");
selected.resize(400, 0);
//调整logo图片的尺寸,当resize后面的参数一个为0时表示图片等比例缩放,比如这里表示把图片的宽度调为400,同时图片高度等比例缩放。
selected.filter(THRESHOLD, 0.9);
//图片分成黑白两色
//selected.filter(INVERT);
//有些logo颜色是反的,看情况来uncomment这句
selected.filter(BLUR, int(spacing*2/3));
//模糊滤镜,模糊程度影响后面圆角矩形群的观感,所以这里把模糊程度和间距参数相关联
size(selected.width, selected.height);
//图片大小决定画布窗口大小
background(255);
noLoop();
//让后面的void draw()只执行一次
}
void draw() {
for(int i=spacing/2;i<selected.width;i+=spacing) {
//在图片宽度内,每隔一个“间距”就打个点
if (map(i, 0, selected.width-1, 0, 255)-156<0) {
hueLv=map(i, 0, selected.width-1, 0, 255)+100;
}
else {
hueLv=map(i, 0, selected.width-1, 0, 255)-156;
}
//把该点的横坐标相对于宽度的比例关系投射到色相带上得到相应的hue值
//用了一个if判断是因为在苹果原图里并不是从左到右正好对应着色相的从0到255,
//而是有一个偏移,这里用if检测并纠正。
println(hueLv);
//这行没用,我自己调试色相带偏移程度时用的
colorMode(HSB);
//颜色模式不是默认的RGB而是HSB,这行很关键
fill(hueLv, 150,220);
//算出每一列的颜色了
for (int j=spacing/2;j<selected.height;j+=spacing) {
sideLen=int(map(brightness(selected.get(i, j)), 0, 255, 0,spacing*7/8));
//每一列内,用各点的颜色的亮度值(前面BLUR出来的变化)算出圆角矩形的边长
Rrect(i, j, sideLen);
//画圆角矩形,Rrect这个自己写的函数在后面
}
}
}
void Rrect(int x, int y, int side) {
//每个点上画圆角矩形时都要调用
noStroke();
beginShape();
vertex(x-side*3/8, y-side/2);
vertex(x+side*3/8, y-side/2);
arc(x+side*3/8,y-side*3/8, side/4, side/4, -HALF_PI, 0);
vertex(x+side/2, y-side*3/8);
vertex(x+side/2, y+side*3/8);
arc(x+side*3/8, y+side*3/8, side/4, side/4, 0, HALF_PI);
vertex(x+side*3/8, y+side/2);
vertex(x-side*3/8, y+side/2);
arc(x-side*3/8, y+side*3/8, side/4, side/4, HALF_PI, PI);
vertex(x-side/2, y+side*3/8);
vertex(x-side/2, y-side*3/8);
arc(x-side*3/8, y-side*3/8, side/4, side/4, PI, PI+HALF_PI);
endShape();
//这段没啥说的,四条线段四个弧线在beginShape和endShape间间次顺时针排列
}
2014年06月03日 08点06分
10