We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello my name is RenV and I'm a student trying to make a little hobbyproject.
I want to make an ambilight using this : blinkstick I want to create an Ambilight app that calculates the average color of my screen and then changes the color of my blinkstick accordingly.
The Blinkstick comes with software for ambilight itself, but it doesn't work. So I thought of just programming it myself.
import blinkstick.*;
import java.awt.image.BufferedImage;
import java.awt.SystemTray;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsDevice;
import java.awt.DisplayMode;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.AWTException;
//import java.awt.Color;
int colr;
PImage screenShot;
BlinkStick device;
GraphicsEnvironment ge;
GraphicsDevice[] gs;
DisplayMode mode;
Rectangle bounds;
BufferedImage desktop;
void setup()
{
size(100,100);
frameRate( 30 );
device = BlinkStick.findFirst();
if (device == null) println("Not found...");
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
gs = ge.getScreenDevices();
mode = gs[0].getDisplayMode();
bounds = new Rectangle(0, 0, mode.getWidth(), mode.getHeight());
}
void draw ()
{
screenShot = getScreen();
setAmbiColor();
device.setColor(colr);
}
PImage getScreen()
{
desktop = new BufferedImage(mode.getWidth(), mode.getHeight(), BufferedImage.TYPE_INT_RGB);
try
{
desktop = new Robot(gs[0]).createScreenCapture(bounds);
}
catch(AWTException e)
{
//System.err.println("Screen capture failed.");
}
return (new PImage(desktop));
}
void setAmbiColor()
{
screenShot.loadPixels();
int r = 0, g = 0, b = 0;
for (int i=0; i<screenShot.pixels.length; i++)
{
color c = screenShot.pixels[i];
r += c>>16&0xFF;
g += c>>8&0xFF;
b += c&0xFF;
}
r /= screenShot.pixels.length;
g /=screenShot.pixels.length;
b /= screenShot.pixels.length;
colr = color(r,g,b);
print("original : " + colr );
// colr*=-1; //Inverse Color
if(r<150 && g<150 && b<150)
{
changeSaturation(2f);
}
}
void changeSaturation(float change)
{
float R = red(colr);
float G = green(colr);
float B = blue(colr);
float P=sqrt(
(R)*(R)*0.299+
(G)*(G)*0.587+
(B)*(B)*0.114 ) ;
R=P+((R)-P)*change;
G=P+((G)-P)*change;
B=P+((B)-P)*change;
int r = Math.round(R);
int g = Math.round(G);
int b = Math.round(B);
colr = color(r,g,b);
println(" adjusted : " + colr);
}
I would really appreciate any kind of help. Do you think it's possible to do this in processing 2?
Thanks a lot.
Answers
"it uses a lot of resources"
Do you really need to make 30 screenshots per second?
"the mouse blinks everytime a screenshot is made"
Perhaps related to the remark above.
"I would like to minimize my app to a tasktray"
Look for Java-specific solutions.
Just take the color at some (maybe 20) randomly/uniformly distributed screen coordinates (with Robot.getPixelColor(x, y)) and average the resulting colors.