How can you access the color of a pixel on the screen using NodeJS?
My use-case is for Windows, but a cross-platform solution would be ideal.
Here's an example in C# which could be helpful: C# get pixel color from another window
How can you access the color of a pixel on the screen using NodeJS?
My use-case is for Windows, but a cross-platform solution would be ideal.
Here's an example in C# which could be helpful: C# get pixel color from another window
I ended up converting the linked C# answer to NodeJS by using node-ffi:
import ffi from "ffi";
import chroma from "chroma-js";
var user32 = new ffi.Library("user32", {
"GetDC": ["int32", ["int32"]],
"ReleaseDC": ["int32", ["int32", "int32"]],
});
var gdi32 = new ffi.Library("gdi32", {
"GetPixel": ["uint32", ["int32", "int32", "int32"]],
});
export function GetPixelColor(x: number, y: number) {
let hdc = user32.GetDC(0);
let pixel = gdi32.GetPixel(hdc, x, y);
user32.ReleaseDC(0, hdc);
let color = chroma(pixel & 0x000000FF, (pixel & 0x0000FF00) >> 8, (pixel & 0x00FF0000) >> 16);
return color.hex().substr(1); // to match with robotjs.getPixelColor() returns
}
To use:
let pixelColor = GetPixelColor(0, 0);
console.log("Color: #" + pixelColor);
UPDATE
Since then, I put together this library: https://github.com/Venryx/windows-ffi
It supports screenshotting of regions of the screen, rather than just individual pixels. This makes it much faster for reading image-data over an area, and is the solution I use nowadays.
windows-ss: https://github.com/Sxxov/windows-ss
screenshot-desktop: https://github.com/bencevans/screenshot-desktop
desktop-screenshot: https://github.com/johnvmt/node-desktop-screenshot
Note: Does not have a built-in way to access the raw pixel data. (you'd have to read and parse the file manually)
Warning: It currently causes crashes if you call getPixelColor when a UAC prompt is open. (see issue)