补充凯文的出色答案:首先分解它。
“改变颜色”=set() 或pixels[]。
(虽然 set 可能会慢一些,但上手和掌握窍门可能更直观)
"black/white (gray scale)" - 具有相同 r,g,b 值的 color() 本质上是灰度。您可以使用恰当命名的 get() 像素的亮度
brightness() 函数。 get() 将检索给定 x,y 坐标对的光标(例如 mouseX,mouseY)
这实现起来超级简单:
- 加载图片
- 获取光标坐标处的像素颜色
- 获取像素的亮度
- 根据同一坐标处的亮度设置灰度值
这是一个快速的 sn-p:
PImage image;
void setup(){
size(200,200);
image = loadImage("https://processing.org/tutorials/pixels/imgs/tint1.jpg");
}
void draw(){
//modify output - cheap grayscale by using the pixel brightness
image.set(mouseX,mouseY,color(//make a gray scale colour...
brightness(//from the brightness...
image.get(mouseX,mouseY)//of the pixel under cursor
)
));
//draw the result;
image(image,0,0);
}
要使整个图像变为灰度需要大量的运动。
另一个选项是图像的副本,该图像已被 filtered 设置为灰度,您可以将 mask() 应用于该图像。当您移动鼠标时,此蒙版会越来越多地显示灰度图像。使这个掩码动态化的一种简单方法是使用PGraphics,正如 Kevin 已经提到的那样。它本质上作为一个单独的层使用典型的处理绘图功能进行绘制。唯一需要注意的是,您需要将这些绘图函数调用放在 beginDraw()/endDraw() 调用中。
这是一个注释演示:
PImage input;//original image
PImage output;//grayscale image
PGraphics mask;
void setup(){
size(200,200);
input = loadImage("https://processing.org/tutorials/pixels/imgs/tint1.jpg");
//copy input pixels into output
output = input.get();
//make it grayascale
output.filter(GRAY);
//setup mask
mask = createGraphics(width,height);
mask.beginDraw();
mask.background(0);//nothing black passes through the mask
mask.noStroke();
mask.fill(255);//everything white passes through the mask
mask.endDraw();
}
void draw(){
//draw color image
image(input,0,0);
//apply mask
output.mask(mask);
//draw masked output
image(output,0,0);
}
//draw into the mask
void mouseDragged(){
mask.beginDraw();
mask.ellipse(mouseX,mouseY,20,20);
mask.endDraw();
}
很酷的是您可以使用gradients 使用其他形状的蒙版和软蒙版