Legended
Legended

Reputation: 129

How to convert COLORREF from GetPixel() to RGB in python?

I'm writing a program using ctypes that returns me the pixels in RGB of wherever I click my mouse on the screen. I'm using GetPixel() to returns a COLORREF, which I believe is a ABGR hexidecimal color space(i.e. #00BBGGRR). The issue is I'm not sure how to convert COLORREF to RGB.

Here's a code snippet:

from ctypes import *
from ctypes.wintypes import LONG
from sys import exit

# DLLs
user32 = windll.user32
gdi32 = windll.gdi32

# Virtual Keys
LM_BUTTON = 0x01
RM_BUTTON = 0x02


def main():
    while True:
        if get_key_state(LM_BUTTON) > 1:  # Detect key press.
            colorref = get_pixel(*get_cursor())
            print(colorref)
            while get_key_state(LM_BUTTON) > 1:   # Wait for key release.
                pass
        if get_key_state(RM_BUTTON) > 1:
            exit()


class POINT(Structure):
    _fields_ = [('x', LONG), ('y', LONG)]


def get_cursor():
    pos = POINT()
    user32.GetCursorPos(byref(pos))
    return pos.x, pos.y


def get_pixel(x, y, hdc=0):
    dc = user32.GetDC(hdc)
    colorref = gdi32.GetPixel(dc, x, y)
    return colorref


def get_key_state(vkey):
    return user32.GetKeyState(vkey)


if __name__ == '__main__':
    main()

There are certain macros, such as RGB macro, to convert COLORREF to RGB, but I'm not sure how to invoke these macros with ctypes.

I've tried created a conversion function, but it's hacky and very ugly, and I feel like I'm taking unnecessary steps to achieve this. There must be a more conventional method of doing this in python?

def get_rgb(colorref):
    color_hex = hex(colorref)[2:].zfill(6)[::-1]
    rgb = tuple(int(rgb, 16) if '0' not in (rgb:=color_hex[i:i+2]) else int(rgb[::-1], 16) for i in (0, 2, 4))
    return rgb

Upvotes: 4

Views: 1526

Answers (2)

Neitsa
Neitsa

Reputation: 8166

A COLORREF is defined as a 32-bit value (from windef.h):

typedef DWORD   COLORREF;

Also those 3 macros (from wingdi.h):

#define RGB(r,g,b)          ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))

#define GetRValue(rgb)      (LOBYTE(rgb))
#define GetGValue(rgb)      (LOBYTE(((WORD)(rgb)) >> 8))
#define GetBValue(rgb)      (LOBYTE((rgb)>>16))

So by looking at these macros we can see that:

  • The R component is the less significant byte (bits 0-7)
  • The G component goes from bits 8 - 15.
  • The B component goes from bits 16 - 23.

So basically all you have to do is mask and bit shift:

def rgba(colorref):
    mask = 0xff
    return [(colorref & (mask << (i * 8))) >> (i * 8) for i in range(4)]

test:

>>> r, g, b, a = rgba(0x01020304)
>>> print(r,g,b,a)
4 3 2 1

Upvotes: 3

Strive Sun
Strive Sun

Reputation: 6289

def main():
    while True:
        if get_key_state(LM_BUTTON) > 1:  # Detect key press.
            colorref = get_pixel(*get_cursor())
            R = colorref & 0xff
            G = (colorref >> 8) & 0xff
            B = (colorref >> 16) & 0xff
            print(R,G,B)
            while get_key_state(LM_BUTTON) > 1:   # Wait for key release.
                pass
        if get_key_state(RM_BUTTON) > 1:
            exit()

I didn't find a suitable API for python, I do not know if this can meet your needs.

Upvotes: 3

Related Questions