PixelGetColor search in a specified region relative to cursor

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

PixelGetColor search in a specified region relative to cursor

25 Apr 2024, 20:44

Hi guys/gals back with another fun one (yeah right)..

I'm looking for the color under the cursor, but not just one pixel. It would need to be, say, 5 pixels in all 4 directions outwards from the cursor tip. So in other words, PixelGetColor searches a single pixel under the cursor (as far as I know), so I would need it to instead search all 5 pixels to the left of the cursor, 5 to the right, 5 up and 5 down, including the 1 center pixel under the cursor tip. So in other words, a very small cube.

Imagine a small 10x10 cube centered under and following the mouse cursor and whenever it detects the color in *all* 100 pixels (or is it 104, I don't know) within that cube, it sends a messagebox or something. Here's an idea I've come up with:

Code: Select all

CoordMode pixel, screen
coordmode, mouse, screen

loop
{
MouseGetPos, x1, y1

NORTH:=(y1-5)
SOUTH:=(y1+5)
EAST:=(x1+5)
WEST:=(x1-5)

y_TOTAL:=(NORTH+SOUTH)
x_TOTAL:=(EAST+WEST)

PixelGetColor, Color, %x_TOTAL%, %y_TOTAL%, RGB
tooltip, color is %Color%
}
Return
This code doesn't work but hopefully it helps convey what I'm trying to do.

I suppose PixelGetColor would need to be something more like RegionGetColor or something, if tha makes sense. It would need to search the x,y of the upper-left-hand-corner of the box, down to the lower-right-hand-corner x,y coordinates of the box... so double the amount of co-ordinates that PixelgetColor searches for.

Any tips would be great, sorry that I come up with all these crazy questions lately lol
scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

Re: PixelGetColor search in a specified region relative to cursor

25 Apr 2024, 21:05

After a bit more searching I found something a little closer but still doesn't work:

Code: Select all

CoordMode, Pixel, Screen
CoordMode, Mouse, Screen

^j::
MouseGetPos, mX, mY
X1 := mX
Y1 := mY
X2 := (mX + 100)
Y2 := (mY + 100)
    
PixelSearch, OutputVarX, OutputVarY, %X1%, %Y1%, %X2%, %Y2%, 0xffff9c, 1, Fast

if (ErrorLevel = 0) 
{
tooltip, FOUND
}
else
{
tooltip, NOT FOUND
}
return
User avatar
Spawnova
Posts: 557
Joined: 08 Jul 2015, 00:12
Contact:

Re: PixelGetColor search in a specified region relative to cursor

29 Apr 2024, 03:10

You can use my image scanning class for this -> ShinsImageScanClass

Code: Select all

#NoEnv
#SingleInstance, Force
SetBatchLines, -1
#persistent

#include ShinsImageScanClass.ahk ;https://github.com/Spawnova/ShinsImageScanClass

scan := new ShinsImageScanClass("Untitled - Paint") ;paint example, draw a region of red (0xFF0000) and move the mouse around to test

settimer,check,100 ;check every 100ms (1/10th of a second)
return


check:
if (!WinActive("ahk_id " scan.hwnd)) ;if the window is not active return
	return
mousegetpos,x,y
if (scan.PixelCountRegion(0xFF0000,x-5,y-5,10,10) = 100) { ;search for pure red pixels in a region
	tooltip % "Found 100 red pixel at region: " x-5 ", " y-5 ", 10, 10"
}
return


f8::exitapp
f9::reload
alternatively, if you don't want to use additional libraries, you can save a 10x10 image consisting of only the pixel color you want and do an ahk imagesearch for the region starting from -5,-5 of the mouse position
User avatar
boiler
Posts: 17120
Joined: 21 Dec 2014, 02:44

Re: PixelGetColor search in a specified region relative to cursor

29 Apr 2024, 04:56

scriptor2016 wrote: Imagine a small 10x10 cube centered under and following the mouse cursor and whenever it detects the color in *all* 100 pixels (or is it 104, I don't know) within that cube, it sends a messagebox or something.
Just to clarify the terminology: It’s not a 10x10 cube. It’s an 11x11 square. Cubes are 3-dimensional. The square has 121 pixels, not 100 or 104. To help picture it, if you instead wanted the square to extend 1 pixel in all 4 directions from a center pixel, it would be a 3x3 square of 9 pixels, not a 2x2 square of 4 pixels. A 2x2 square wouldn’t have a center pixel, and neither does a 10x10 square.

The distinction on dimensions is especially important for creating the reference image of the correct size if the ImageSearch approach that Spawnova suggested is used. That would be a very fast and straightforward way to accomplish this. You just need to create the 11x11 square reference image with all 121 pixels of the target color as a .png file.
scriptor2016
Posts: 864
Joined: 21 Dec 2015, 02:34

Re: PixelGetColor search in a specified region relative to cursor

06 May 2024, 00:34

thanks guys I just saw this now. Ended up going with an other approach for my task, which was probably a better way to go. Thanks boiler for the 3x3 square of 9 pixels info, it makes total sense now.

It would have made more sense for me to explain that I was maybe looking for a 5x5 square of 25 pixels, all containing the exact same color. And when found, do something. I just couldn't figure out how to word it lol. I actually tried saving a .png file of those same dimensions and all the same color and then using imagesearch to locate the targeted area as Spawnova mentioned, but couldn't get that going either. Probably something in the syntax I was using... but either way I got it going with a different approach as I mentioned above, but I won't get into that here and bore everyone to death :)

I'll try out the class provided by Spawnova as well, since it will likely come in handy sooner or later considering the scope of the projects I like to work on with ahk.

Thanks guys again

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], peter_ahk and 104 guests