Processing 从画布上选择一种颜色

Processing 从画布上选择一种颜色,processing,Processing,我想从绘制的画布上选择一种颜色。 我找到了get()函数,但它只能从图像中获取颜色。 有什么方法可以从当前画布获取颜色吗?您可以从当前画布获取颜色:只需解决您需要的PGraphics实例(甚至是全局实例),并确保首先调用 下面是经过调整的处理>示例>基础>图像>加载显示图像版本: /** * Load and Display * * Images can be loaded and displayed to the screen at their actual size * or a

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

您可以从当前画布获取颜色:只需解决您需要的PGraphics实例(甚至是全局实例),并确保首先调用

下面是经过调整的处理>示例>基础>图像>加载显示图像版本

/**
 * 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);
}
它归结为调用
loadPixels()
get()之前
。 上面我们从草图的全局PGraphics缓冲区读取像素。 您可以应用相同的逻辑,但根据设置引用不同的PGraphics缓冲区。

loadPixels()工作如我所料!谢谢你的完美回答!