Sign in to follow this  
Followers 0
ViciousXUSMC

PixelSearch Based on Mouse Position

1 post in this topic

#1 ·  Posted (edited)

I was just messing around, the code is probably sloppy and redundant but it works.

The purpose here was that with PixelSearch() very often there is more than one result and you get the wrong result since your color matches somewhere else on screen.

The way I did this was when you call the function it uses your mouses current position as the starting point for the search, so if you are closer to your desired location you have a stronger chance of finding the result you want.  How close you need to be for the initial search is the $range varible.

If you are not close enough to your location and no other pixel exist with the proper color for a match it starts expanding the search using the $expand variable. 

So you get good control of how large you want the initial search vector to be, smaller is more accurate but higher chances of calling to the expand, the expand is slower but you can change the pace of the expansion to make it more accurate or faster.

I noticed after the fact that values less than 0 or greater than your screen resolution do not break pixelsearch() so all the If statments to ensure I stayed in those values are not nessisary but I left them in just to show my thought process.

Once you fire this up mouse over a spot and hit Control+Z to capture your pixel location/color and then Control+R to search for it and Mouse Move.

I'll keep the other functions in here as well that were the building blocks for the code (Get Position, Get Color, Etc)

Also I am not a great coder, would love tips on cleaning it up, properly stating my variables, etc.

Global $x = 0
Global $y = 0
Global $range = 50
Global $xmax = 1920
Global $ymax = 1200
Global $expandrate = 10


HotKeySet("{Escape}", "Terminate")
HotKeySet("^c", "GetPos")
HotKeySet("^v", "GetColor")
HotKeySet("^x", "FindColor")
HotKeySet("^z", "PosColorCombo")
HotKeySet("^r", "FindRanged")



Func Terminate()
    Exit
EndFunc

Func GetPos()
    $aPOS = MouseGetPos()
    $x = $aPOS[0]
    $y = $aPOS[1]
    ;Debug
    MsgBox(0, "", "Your X is " & $x & "  Your Y is " & $y)
EndFunc

Func GetColor()
    Global $vColor = PixelGetColor($x, $y)
    MsgBox(0, "", "Your Color Is " & $vColor)
EndFunc

Func FindColor()
    $aPix = PixelSearch(0, 0, 1920, 1200, $vColor)
    $x = $aPix[0]
    $y = $aPix[1]
    MouseMove($x, $y, 10)
    EndFunc

Func PosColorCombo()
    $aPOS = MouseGetPos()
    $x = $aPOS[0]
    $y = $aPOS[1]
    Global $vColor = PixelGetColor($x, $y)
EndFunc

Func FindRanged()
    $aPOS = MouseGetPos()
    $x = $aPOS[0]
    $y = $aPOS[1]

    If $x - $range > 0 Then
        $lb = $x - $range
    Else
        $lb = 0
    EndIf

    If $y - $range > 0 Then
        $tb = $y - $range
    Else
        $tb = 0
    EndIf

    If $x + $range > $xmax Then
        $rb = $xmax
    Else
        $rb = $x + $range
    EndIf

    If $y + $range > $ymax Then
        $bb = $ymax
    Else
        $bb = $y + $range
    EndIf

#cs
    $debugtext = "Your Left Boundary Is " & $lb & @CRLF
    $debugtext &= "Your Top Boundary Is " & $tb & @CRLF
    $debugtext &= "Your Right Boundary Is " & $rb & @CRLF
    $debugtext &= "Your Bottom Boundary Is " & $bb
    MsgBox(0, "", $debugtext)
#CE


    $aPix = PixelSearch($lb, $tb, $rb, $bb, $vColor)
    If Not @Error Then
    $x = $aPix[0]
    $y = $aPix[1]
    MouseMove($x, $y, 10)
    Else
    $Expand = 1
    Do
    $tb -=$expandrate
    $lb -=$expandrate
    $rb +=$expandrate
    $bb +=$expandrate
    $aPix = PixelSearch($lb, $tb, $rb, $bb, $vColor)
    If Not @Error Then $Expand =2
    Until $Expand = 2
    EndIf
    $x = $aPix[0]
    $y = $aPix[1]
    MouseMove($x, $y, 10)
EndFunc






While 1
    Sleep(200)
WEnd
Edited by ViciousXUSMC

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0