PixelSearch - GlitchedSouls/AHK-Guide GitHub Wiki
If you're wanting to find a colour, or a variance of that colour, PixelSearch is the function you need to be using.
PixelSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ColorID , Variation, Mode
How do I know what my X1, Y1, X2, Y2 coordinates are?
If X1 = 100, Y1 = 100, X2 = 200, Y2 = 200, then you are searching for a colour you specify in a 100x100 area. In total, that is 10,000 individual pixels that the PixelSearch function will check for. A large area with a high variation will work, however it might not work as fast as you expect. Stick to a reasonable area and find a variation that works well for your use case.
- X1, Y1 refers to the top left corner of the area.
- X2, Y2 refers to the bottom right corner of the area.
Retrieving the ColourID.
Take a look at the Window Spy Wiki Guide. it shows you how to retrieve the ColourID.
How to find the colour red inside an area.
X1 := 100 ; *
Y1 := 100 ; *
X2 := 200 ; *
Y2 := 200 ; *
ColorID := "0xFF0000" ; *
Variation := 15 ; *
PixelSearch, OutputVarX, OutputVarY, X1, Y1, X2, Y2, ColorID, Variation, RGB Fast
if (ErrorLevel) { ; Error
Msgbox, % "The colour " ColorID " has NOT been found."
}
else if (!ErrorLevel) { ; No Error
Msgbox, The colour %ColorID% has been found at x = %OutputVarX%, y = %OutputVarY%.
}
return
* Lines marked with the asterisk don't need to be present, you can change the values in the PixelSearch function itself.
If you were to run the script above, you will most likely get a MsgBox popup stating:
- "The colour 0xFF0000 has NOT been found."
If you do have the red hex colour "0xFF0000" inside the PixelSearch area, a MsgBox will popup stating:
- "The colour 0xFF0000 has been found at x = (xcoord), y = (ycoord)."
What's ErrorLevel?
ErroLevel is able to throw an exception if there was a problem while searching, in a nutshell, it allows you to perform actions if or if not the colour has been found.
I don't want to search top left to bottom right, can I change that?
Yes, all you have to do is swap the X1, Y1, with X2, Y2, and you will search bottom right to top left, the code would look like this:
PixelSearch, OutputVarX, OutputVarY, X2, Y2, X1, Y1, ColorID , Variation, Mode
PixelSearch has found the colour, now what?
Well, that depends on what you want to do. If you're wanting to click or move to the found pixel, OutputVarX and OutputVarY are the x, y coordinates where the colour has been found.