search for a pixel inside a spesific rectangle

Ask gaming related questions (AHK v1.1 and older)
iJohnny
Posts: 8
Joined: 25 Sep 2022, 13:22

search for a pixel inside a spesific rectangle

Post by iJohnny » 30 Sep 2022, 04:37

Hello guys, I'm pretty new and I just wanted some help to figure out how I would go about searching for a color in specific rectangle inside my game, instead of searching in just 1 single spot or the whole game window I wanted it to just search for the color red in this part see picture below

https://imgur.com/mSAC830

User avatar
boiler
Posts: 16932
Joined: 21 Dec 2014, 02:44

Re: search for a pixel inside a spesific rectangle

Post by boiler » 30 Sep 2022, 04:53

See PixelSearch. There is an example on that page.

iJohnny
Posts: 8
Joined: 25 Sep 2022, 13:22

Re: search for a pixel inside a spesific rectangle

Post by iJohnny » 30 Sep 2022, 08:34

This is my script so far, everything works fine except pressing the E key when it finds the red color in that rectangle

Code: Select all

F3::
CoordMode, pixel,screen
 Loop 
 { 
 PixelSearch,,, 809, 799, 809, 799, 0xE6E6E5,, Fast RGB
AisWhite := !ErrorLevel
PixelSearch,,, 816, 793, 816, 793, 0x2F6F32,, Fast RGB
BisGreen := !ErrorLevel
PixelSearch,,, 809, 759, 1111, 800, 0x653130,, Fast RGB
CisRed := !ErrorLevel
Switch AisWhite BisGreen CisRed
{
Case True True False:
	sleep 25
Case False False False:
	Send, {e down}
      sleep 25
      Send, {e up}   
Case  True True True:
	Send, {e down}
      sleep 25
      Send, {e up}  

}
} 
return
~F4::ExitApp


iJohnny
Posts: 8
Joined: 25 Sep 2022, 13:22

Re: search for a pixel inside a spesific rectangle

Post by iJohnny » 30 Sep 2022, 08:46

The only thing that I think that I'm doing wrong is capturing that rectangle because I tried to edit it to msgbox, do 1 if it finds the color red in that rectangle and it's not capturing it.
I put the x1 and y1 top left corner of the rectangle and then x2 and y2 as bottom right of the rectangle and then choose the hex of the color.

I beg anyone to help me fix this, been trying to learn how to do it from scratch for a while now without any success :(

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: search for a pixel inside a spesific rectangle

Post by mikeyww » 30 Sep 2022, 08:55

Display the ErrorLevel in your script. If pixel is not found, use PixelGetColor to see what is really there. You will then know. MouseMove to that pixel.

Code: Select all

x = 809
y = 759
CoordMode, Pixel
CoordMode, Mouse
MouseMove, x, y
PixelSearch,,, x, y, x, y, 0x653130,, Fast RGB
If ErrorLevel {
 WinGetActiveTitle, title
 PixelGetColor, rgb, x, y, RGB
 MsgBox, 64, Nope, Found %rgb%`n`nwhen active window is`n`n%title%
} Else MsgBox, 64, Yep, Found!
Return

iJohnny
Posts: 8
Joined: 25 Sep 2022, 13:22

Re: search for a pixel inside a spesific rectangle

Post by iJohnny » 30 Sep 2022, 09:06

mikeyww wrote:
30 Sep 2022, 08:55
Display the ErrorLevel in your script. If pixel is not found, use PixelGetColor to see what is really there. You will then know. MouseMove to that pixel.

Code: Select all

x = 809
y = 759
CoordMode, Pixel
CoordMode, Mouse
MouseMove, x, y
PixelSearch,,, x, y, x, y, 0x653130,, Fast RGB
If ErrorLevel {
 WinGetActiveTitle, title
 PixelGetColor, rgb, x, y, RGB
 MsgBox, 64, Nope, Found %rgb%`n`nwhen active window is`n`n%title%
} Else MsgBox, 64, Yep, Found!
Return
Thank you for your reply, that isn't much help because there's a short time like 2 seconds, while the color is visible in that rectangle and it doesn't always appear exactly in the same place, it appears anywhere in between the rectangle.

iJohnny
Posts: 8
Joined: 25 Sep 2022, 13:22

Re: search for a pixel inside a spesific rectangle

Post by iJohnny » 30 Sep 2022, 10:02

I think I might know why it's not working, the color I'm trying to detect (red) has different shading depending on the time of the day inside the game, if it's night or day, it changes.
Is there anyway that I can put a bunch of different hex shades of red, without doing PixelSearch for each shade? So let's say I did something like

Code: Select all

Red := "0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF"
And then in PixelSearch I would do

Code: Select all

PixelSearch,,, x, y, x, y, Red,, Fast RGB
Would this work? Sorry for the noob questions

sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: search for a pixel inside a spesific rectangle

Post by sofista » 30 Sep 2022, 11:06

PixelSearch has a specific parameter to deal with color variation, so it would be better to try it:

Code: Select all

PixelSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ColorID , Variation, Mode
where:
Variation

A number between 0 and 255 (inclusive) to indicate the allowed number of shades of variation in either direction for the intensity of the red, green, and blue components of the color (can be an expression). For example, if 2 is specified and ColorID is 0x444444, any color from 0x424242 to 0x464646 will be considered a match. This parameter is helpful if the color sought is not always exactly the same shade. If you specify 255 shades of variation, all colors will match. The default is 0 shades.

iJohnny
Posts: 8
Joined: 25 Sep 2022, 13:22

Re: search for a pixel inside a spesific rectangle

Post by iJohnny » 30 Sep 2022, 12:17

sofista wrote:
30 Sep 2022, 11:06
PixelSearch has a specific parameter to deal with color variation, so it would be better to try it:

Code: Select all

PixelSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ColorID , Variation, Mode
where:
Variation

A number between 0 and 255 (inclusive) to indicate the allowed number of shades of variation in either direction for the intensity of the red, green, and blue components of the color (can be an expression). For example, if 2 is specified and ColorID is 0x444444, any color from 0x424242 to 0x464646 will be considered a match. This parameter is helpful if the color sought is not always exactly the same shade. If you specify 255 shades of variation, all colors will match. The default is 0 shades.
This was very helpful, thank you will try to edit this tonight might actually be exactly what I needed without having to try get every single shade one by one and writing a massive script.
Will report back whenever I have tested.

With the script, there's no issues though right? The way it's written should be able to achieve what I need? Pretty new and I am also not sure if I'm going about this the right way.

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: search for a pixel inside a spesific rectangle

Post by mikeyww » 30 Sep 2022, 16:09

I do not see a specific problem with the approach.

Post Reply

Return to “Gaming Help (v1)”