RGB values of screen pixels (not from a loaded image)

Hi, I want to capture and store the RGB values from an area of pixels anywhere on the screen (- not from a preloaded image - but of the screen) and that’s where I’m stumbling. As all the examples I’ve seen, first load an image for processing, whereas I would like to capture an area of the screen itself. This will allow me to save various colour sequences and play them back for a RGB (WS2812B) lamp that I have built.

Are there any kind souls who could helpfully point me in the right direction? Many thanks in advance. Chris.

Hello,

To get you started:

// Get Colors
// v1.0.0
// GLV 2022-04-02

PImage getImg;

void setup() 
  {
  size(500, 500);
  background(128);
  noStroke();
  // 8 random RGB colors
  for(int i= 0; i< 200; i++)
    {
    fill(int(random(2))*255, int(random(2))*255, int(random(2))*255);  
    circle(random(width), random(height), 50);
    }
  }

void draw() 
  {
  // Get an area 50x50 from sketch window and store in a PImage
  getImg = get(mouseX, mouseY, 50, 50);
  //Display it
  image(getImg, width-50, 0);
  
  // Get a single pixel from the 50x50 PImage
  int getPixelColor1 = getImg.get(0, 0);
  println("1:", hex(getPixelColor1));
  
  // Get a single pixel from the sketch window
  int getPixelColor2 = get(mouseX, mouseY);
  println("2:", mouseX, mouseY, hex(getPixelColor1));
  }

References:

Have fun!

:)

Glv, thank you for your kind example and references. My stumbling block is I want to be able to sample the desktop or another window not necessarily created by processing (sorry I said screen which was I think, a misleading description!). In your example I tried making the background transparent using background(128,255); in the hope of setting the alpha of the window transparent (so I could sample anything beneath it?) - but it’s still a solid gray. I also tried using tint(255,255); but that didn’t make the window transparent either. In the meantime I’ll follow up the references you gave, if you have any other pointers - all gratefully received :smile:

1 Like

Hello,

This is very quick exploration of this from the couple of resources I found:

// Screen Color
// v1.0.0
// GLV 2022-04-02

// https://stackoverflow.com/questions/1439022/get-mouse-position
// https://www.tabnine.com/code/java/methods/java.awt.Robot/getPixelColor

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Color;
import java.awt.MouseInfo;

Robot robot;


void setup() 
  {
  size(400, 400);
  try 
    { 
    robot = new Robot(); 
    } 
  catch (AWTException ex) 
    {
    System.out.println(ex.getMessage());
    }
  }

void draw() 
  {
  background(0);
  println(getMouseColor(mouseX, mouseY));
  
  int x = MouseInfo.getPointerInfo().getLocation().x;
  int y = MouseInfo.getPointerInfo().getLocation().y;
  
  println(getMouseColor(x, y));
  
  int[] components = new int[3];
  Color c = robot.getPixelColor(x, y);
  components[0] = c.getRed();
  components[1] = c.getGreen();
  components[2] = c.getBlue();
  println(components[0], components[1], components[2]);
  fill(components[0], components[1], components[2]);
  circle(width/2, height/2, 300);
  }

public Color getMouseColor(int x, int y) 
  {
  return robot.getPixelColor(x, y);
  }

Again… just a quick exploration to get you started.

Have fun!

:)