从 canvas 中挑选一种颜色

Pick up a color from canvas

我想从绘制的 canvas 中选取一种颜色。
我找到了 get() 函数,但它只能从图像中获取颜色。
有什么方法可以从当前 canvas 获取颜色吗?

你可以先get() colour from your current canvas: just address the PGraphics instance you need (even the global one) and be sure to call loadPixels()

这是 Processing > Examples > Basics > Image > LoadDisplayImage 的调整版本:

/**
 * Load and Display 
 * 
 * Images can be loaded and displayed to the screen at their actual size
 * or any other size. 
 */

PImage img;  // Declare variable "a" of type PImage

void setup() {
  size(640, 360);
  // The image file must be in the data folder of the current sketch 
  // to load successfully
  img = loadImage("https://processing.org/examples/moonwalk.jpg");  // Load the image into the program  
}

void draw() {
  // Displays the image at its actual size at point (0,0)
  image(img, 0, 0);
  // Displays the image at point (0, height/2) at half of its size
  image(img, 0, height/2, img.width/2, img.height/2);

  //load pixels so they can be read via get()
  loadPixels();
  // colour pick
  int pickedColor = get(mouseX,mouseY);

  // display for demo purposes
  fill(pickedColor);
  ellipse(mouseX,mouseY,30,30);
  fill(brightness(pickedColor) > 127 ? color(0) : color(255));
  text(hex(pickedColor),mouseX+21,mouseY+6);
}

归结为在 get() 之前调用 loadPixels();。 上面我们正在从草图的全局 PGraphics 缓冲区中读取像素。 您可以应用相同的逻辑,但根据您的设置引用不同的 PGraphics 缓冲区。