How to get the rbg value of a pixel on screen

I need a way to get the rgb values of a pixel at (x,y). I've tried searching for a solution, but all of the solutions seem to involve a crate to take screenshots, and then analyzing the screenshot, which seems like it might create too many screenshots, since I need to get the rgb value a lot. Is there a way to do this without screenshots? If not, how do I read the rgb values of a specific pixel in a screenshot I already have? Something similar exists in pyautogui, but rsautogui's "get_pixel" is too slow.

The answer can be different depending on a scenario - and there's a whole lot of possible scenarios, but what's more importantly - this feels a lot like an XY problem. What are you trying to do with getting a pixel color?

I'm trying to get the color of a pixel on a webpage to determine if it's white or not - the idea is to click if it's white, and that can change pretty quickly. So my idea was the code opens the page and checks the pixel colors repeatedly until one turns white, then it clicks there.

use windows::Win32::Graphics::Gdi::GetDC;
use windows::Win32::Graphics::Gdi::GetPixel;

fn main() {
    let x = 1332;
    //The x-coordinate, in logical units, of the pixel to be examined.
    let y = 500;
    //The y-coordinate, in logical units, of the pixel to be examined.
    unsafe {
        let hwnd = GetDC(None);

        let a = GetPixel(hwnd, x, y);
        let a_u32: u32 = a.0;
        println!("Pixel as hexadecimal 0x{:X}", a_u32);
        if a_u32 ==0x00FFFFFF {
            println!("The pixel at {} , {} is white", x, y);
        } else {
            println!("The pixel at {} , {} is not white", x, y);

Put this in cargo.toml

windows = { version = "0.52", features = [
] }

This code returns COLORREF value for a pixel. This can be converted into RGB.

I don't know how you are supposed to do this but I got this hacky solution by looking at github code for crates which take a screenshot and guessing.


wow. Thanks, that's exactly what I wanted

It is only for getting a single pixel though. If you wanted the whole screen the code would be different. Calling this to get each pixel individually won't work well.

that's fine, this is what I wanted

Could you please select that reply at the solution? There is a checkbox at the bottom of the reply.