// this code is based on code found on // http://siliconrepublic.blogspot.nl/2011/02/arduino-based-pc-ambient-lighting.html // But modified by Bart Venneker 2013 to support 3 channel atmolight import processing.serial.*; import java.awt.event.InputEvent; import java.awt.AWTException; import java.awt.Robot; import java.awt.image.BufferedImage; import java.awt.Dimension; import java.awt.Rectangle; Serial serport; //creates object "port" of serial class Robot robby; //creates object "robby" of robot class void setup() { // baud rate for atmolight = 38400 // default comport for arduino uno rev3= /dev/ttyACM0 // for windows you should use COM3 (or whatever) serport = new Serial(this, "/dev/ttyACM0",38400); //set baud rate size(100, 100); //window size (doesn't matter) try //standard Robot class error check { robby = new Robot(); } catch (AWTException e) { println("Robot class not supported by your system!"); exit(); } } void draw() { int pixel, h=0, i=0, j=0; //ARGB variable with 32 int bytes where float rt=0, gt=0, bt=0; // red-top, green-top and blue-top float rl=0, gl=0, bl=0; // red-left, green-left and blue-left float rr=0, gr=0, br=0; // red-right, green-right and green-left int screenwidth = displayWidth, screenheight = displayHeight;; // gets the width and height of the desktop //get screenshot into object "screenshot" of class BufferedImage BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(screenwidth,screenheight))); // TOP // Top box starts at 50 from the top, 300 from the left and stops at 300 before the end of the screen for(i =300;i<(screenwidth - 300); i=i+2){ for(j=50; j<300;j=j+2){ pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j) rt = rt+(int)(255&(pixel>>16)); //add up reds gt = gt+(int)(255&(pixel>>8)); //add up greens bt = bt+(int)(255&(pixel)); //add up blues h=h+1; } } rt=rt/h; //average red gt=gt/h; //average green bt=bt/h; //average blue // LEFT h=0; for(i =50;i<300; i=i+2){ for(j=300; j<600;j=j+2){ pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j) rl = rl+(int)(255&(pixel>>16)); //add up reds gl = gl+(int)(255&(pixel>>8)); //add up greens bl = bl+(int)(255&(pixel)); //add up blues h = h + 1; } } rl=rl/(h); //average red 150 gl=gl/(h); //average green 50 bl=bl/(h); //average blue 50 // RIGHT h=0; for(i =(screenwidth - 300 );i<(screenwidth-50); i=i+2){ for(j=300; j<600;j=j+2){ pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j) rr = rr+(int)(255&(pixel>>16)); //add up reds gr = gr+(int)(255&(pixel>>8)); //add up greens br = br+(int)(255&(pixel)); //add up blues h = h + 1; } } rr=rr/(h); //average red gr=gr/(h); //average green br=br/(h); //average blue // adjust full colors // Maybe you do not want to use this, // What does this do?? : if for example the color consists of // primarily red shades it changes the output to pure RED if ((gl + bl) < rl) { rl = 255; gl = 0; bl = 0; } if ((bl + rl) < gl) { gl = 255; bl = 0; rl = 0; } if ((rl + gl) < bl) { bl = 255; rl = 0; gl = 0; } if ((gr + br) < rr) { rr = 255; gr = 0; br = 0; } if ((br + rr) < gr) { gr = 255; br = 0; rr = 0; } if ((rr + gr) < br) { br = 255; rr = 0; gr = 0; } if ((gt + bt) < rt) { rt = 255; gt = 0; bt = 0; } if ((bt + rt) < gt) { gt = 255; bt = 0; rt = 0; } if ((rt + gt) < bt) { bt = 255; rt = 0; gt = 0; } serport.write(0xff); //write marker (0xff) for synchronization serport.write(0x00); // heigh byte serport.write(0x00); // low byte serport.write(0x0f); // Number of channels // first center channel will be ignored serport.write(0x00); // r serport.write(0x00); // g serport.write(0x00); // b // LEFT serport.write((byte)(rl)); //write red value serport.write((byte)(gl)); //write green value serport.write((byte)(bl)); //write blue value // RIGHT serport.write((byte)(rr)); //write red value serport.write((byte)(gr)); //write green value serport.write((byte)(br)); //write blue value // TOP serport.write((byte)(rt)); //write red value serport.write((byte)(gt)); //write green value serport.write((byte)(bt)); //write blue value // Nothing (bottom channel) serport.write(0x00); // r serport.write(0x00); // g serport.write(0x00); // b delay(10); //delay for safety background(rt,gt,bt); //make window background the same as the top color }