How to Take Screen Shot and Analyse Colour On Computer Screen

There are a lot of programming language that can take screen shot and do some amazing stuff on analysing it. I will be using Processing for this purposes because

  • it’s cross-platform (can run on Windows, Mac and Linux)
  • uses C++ syntax (my favorite language)
  • has the same IDE/programming convention as the Arduino (actually the Arduino IDE was based on the Processing IDE :-D )
  • Supports some very powerful and handy Java libraries
  • it’s Free!

The pseudo code for this project would be:

  • Loop
    • Take a screenshot
    • Find the area we want to capture and analyse the colour
    • Down-sampling and Extract the RGB values from the pixels in this region
    • Take average RGB value and display it

With the help of some very powerful Java libraries, we are able to achieve this at around 60 lines of code. In this example, we have a screen of resolution 1366×768, and it’s taking the average colour from the 100×100 region around the middle. We don’t need to check each pixel, that would be expensive! (100×100 is 100,000 pixels! )

So what we do here, is to skip some of the pixels. For example check one pixel and skip one:



Here is the source code:

import java.awt.*;
import java.awt.image.*;

// Put the right screen size in here
int screenW     = 1366;
int screenH     = 768;

// Preview window size
int windowW		= 100;
int windowH		= 100;

// Define the box where we want to capture and analyse colour from
// The start point of the box (top and left coordinate)
int boxL 	= 633; // That's the middle of the screen
int boxT 	= 334;
// Size of the box 
int boxW    = 100;
int boxH    = 100;

//How many pixels to skip while reading (the more you skip, it runs faster, but result might get worse)
int pixelSkip	= 2;

// Screen Area to be captured (usually the whole screen)
Rectangle dispBounds;
// creates object from java library that lets us take screenshots
Robot bot;

void setup(){

	// Create screenshot area
	dispBounds = new Rectangle(new Dimension(screenW,screenH));
	// Create Preview Window
	size(windowW, windowH);

	// Standard Robot class error check and Create screenshot Robot
	try   {
		bot = new Robot();
	catch (AWTException e)  {
		println("Robot class not supported by your system!");

void draw(){

	//ARGB value of a pixel, contains sets of 8 bytes values for Alpha, Red, Green, Blue
	int pixel;

	int r = 0;
	int g = 0;
	int b = 0;

	// Take screenshot
	BufferedImage screenshot = bot.createScreenCapture(dispBounds);

	// Pass all the ARGB values of every pixel into an array
	int[] screenData = ((DataBufferInt)screenshot.getRaster().getDataBuffer()).getData();

	//Find the RGB values of the region we want
	for(int i = boxT; i < (boxT + boxH); i += pixelSkip){      
		for(int j = boxL; j < (boxL + boxW); j += pixelSkip){

                        pixel = screenData[ i*screenW + j ]; 			 			
                        r += 0xff & (pixel>>16);
			g += 0xff & (pixel>>8 );
			b += 0xff &  pixel;


	// take average RGB values.
	r  = r / (boxH/pixelSkip * boxW/pixelSkip);
	g  = g / (boxH/pixelSkip * boxW/pixelSkip);
	b  = b / (boxH/pixelSkip * boxW/pixelSkip);

	color rgb = color((short)r, (short)g, (short)b);
	rect(0, 0, 100, 100);


Nothing too difficult in this code, but it might take a moment to understand the bitshift part of the code. If you don’t understand what “& 0xff” means, find my explanation in another post.

3 thoughts on “How to Take Screen Shot and Analyse Colour On Computer Screen

    1. Oscar Post author

      I don’t think so, but I haven’tested every resolution myself. Try it, just change the resolution in these variables

      // Put the right screen size in here
      int screenW = 1366;
      int screenH = 768;


Leave a Reply

Your email address will not be published. Required fields are marked *

Are you Robot? *

I only check blog comments once or twice a week, if you want a quick reply you can post your question on this forum You might get a faster response from me there (multirotor related only).