SHARE
TWEET

Processing - RGB ambient backlight code

w0lfiesmith Dec 6th, 2012 2,625 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Developed by Rajarshi Roy, modified by James Bruce
  2. import java.awt.Robot; //java library that lets us take screenshots
  3. import java.awt.AWTException;
  4. import java.awt.event.InputEvent;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.Rectangle;
  7. import java.awt.Dimension;
  8. import processing.serial.*; //library for serial communication
  9.  
  10.  
  11. Serial port; //creates object "port" of serial class
  12. Robot robby; //creates object "robby" of robot class
  13.  
  14. void setup()
  15. {
  16.   println(Serial.list());
  17. port = new Serial(this, Serial.list()[4],9600); //set baud rate
  18. size(100, 100); //window size (doesn't matter)
  19. try //standard Robot class error check
  20. {
  21. robby = new Robot();
  22. }
  23. catch (AWTException e)
  24. {
  25. println("Robot class not supported by your system!");
  26. exit();
  27. }
  28. }
  29.  
  30. void draw()
  31. {
  32. int pixel; //ARGB variable with 32 int bytes where
  33. //sets of 8 bytes are: Alpha, Red, Green, Blue
  34. float r=0;
  35. float g=0;
  36. float b=0;
  37.  
  38. int skipValue = 2;
  39. int x = screen.width; //possibly displayWidth
  40. int y =  screen.height; //possible displayHeight instead
  41.  
  42. //get screenshot into object "screenshot" of class BufferedImage
  43. BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(x,y)));
  44. //1368*928 is the screen resolution
  45.  
  46.  
  47. int i=0;
  48. int j=0;
  49. //I skip every alternate pixel making my program 4 times faster
  50. for(i =0;i<x; i=i+skipValue){
  51. for(j=0; j<y;j=j+skipValue){
  52. pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
  53. r = r+(int)(255&(pixel>>16)); //add up reds
  54. g = g+(int)(255&(pixel>>8)); //add up greens
  55. b = b+(int)(255&(pixel)); //add up blues
  56. }
  57. }
  58. int aX = x/skipValue;
  59. int aY = y/skipValue;
  60. r=r/(aX*aY); //average red
  61. g=g/(aX*aY); //average green
  62. b=b/(aX*aY); //average blue
  63.  
  64. //println(r+","+g+","+b);
  65.  
  66. // filter values to increase saturation
  67. float maxColorInt;
  68. float minColorInt;
  69.  
  70. maxColorInt = max(r,g,b);
  71. if(maxColorInt == r){
  72.   // red
  73.   if(maxColorInt < (225-20)){
  74.     r = maxColorInt + 20;  
  75.   }
  76. }
  77. else if (maxColorInt == g){
  78.   //green
  79.   if(maxColorInt < (225-20)){
  80.     g = maxColorInt + 20;  
  81.   }
  82. }
  83. else {
  84.    //blue
  85.    if(maxColorInt < (225-20)){
  86.     b = maxColorInt + 20;  
  87.   }  
  88. }
  89.  
  90. //minimise smallest
  91. minColorInt = min(r,g,b);
  92. if(minColorInt == r){
  93.   // red
  94.   if(minColorInt > 20){
  95.     r = minColorInt - 20;  
  96.   }
  97. }
  98. else if (minColorInt == g){
  99.   //green
  100.   if(minColorInt > 20){
  101.     g = minColorInt - 20;  
  102.   }
  103. }
  104. else {
  105.    //blue
  106.    if(minColorInt > 20){
  107.     b = minColorInt - 20;  
  108.   }  
  109. }
  110.  
  111.  
  112. port.write(0xff); //write marker (0xff) for synchronization
  113. port.write((byte)(r)); //write red value
  114. port.write((byte)(g)); //write green value
  115. port.write((byte)(b)); //write blue value
  116. delay(10); //delay for safety
  117.  
  118. background(r,g,b); //make window background average color
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top