Jump to content
Sign in to follow this  
jaberwacky

PixelSearch Modification?

Recommended Posts

jaberwacky

Hi!

I searched the forums and couldn't find a function that does this. Guess I didn't look hard enough?

Anyway, I hacked together a function that makes PixelSearch a little more user friendly. I hope I didn't overlook a similiar function in the helpfile. Instead of specifying two separate coordinates for the top left corner and the bottom right corner. Or as it is described in the helpfile as the top and bottom line or something or another. You can now specify the center of the PixelSearch square and the total height of the square.

My main reason to post is to see your thoughts and for you to suggest your improvements.

; $centerX & $centerY = (X,Y) coordinates of the center of the rectangle
; $height = height of the square from the top to bottom but not from the center of the square.

Func PixelSearchCenter($centerX,$centerY,$height,$color,$shadeVariation="",$step="",$hwnd="")

    Local $xyCoord[ 2 ]

    Local $halfHeight = $height/2

    Local $left = $centerX - $halfHeight

    Local $top = $centerY - $halfHeight

    Local $right = $centerX + $halfHeight

    Local $bottom = $centerY + $halfHeight

    $xyCoord = PixelSearch($left,$top,$right,$bottom,$color,$shadeVariation,$step,$hwnd)    

    Return $xyCoord

EndFunc
Edited by jaberwocky6669

Share this post


Link to post
Share on other sites
jaberwacky

; $centerX & $centerY = (X,Y) coordinates of the center of the rectangle
; $height = height of the square from the top to bottom but not from the center of the square.

Func PixelSearchCenter($x,$y,$height,$color,$shadeVariation="",$step="",$hwnd="")
    Local $xyCoord[ 2 ]
    Local $half = $height/2
    $xyCoord = PixelSearch($x-$half,$y-$half,$x+$half,$y+$half,$color,$shadeVariation,$step,$hwnd)  
    Return $xyCoord
EndFunc

Edited by jaberwocky6669

Share this post


Link to post
Share on other sites
jaberwacky

; $centerX & $centerY = (X,Y) coordinates of the center of the square
; $height = height of the square from the top to bottom but not from the center of the square.

Func PixelSearchCenter($x,$y,$height,$color,$shadeVariation="",$step="",$hwnd="")
    Local $xyCoord[ 2 ] = [0]
    Local $half = $height/2
    $xyCoord = PixelSearch($x-$half,$y-$half,$x+$half,$y+$half,$color,$shadeVariation,$step,$hwnd)
    If @error = 1 Then 
        SetError( 1 )
    Else
        Return $xyCoord
    EndIf
EndFunc

Edited by jaberwocky6669

Share this post


Link to post
Share on other sites
jaberwacky

I followed some UDF formatting instructions that I found:

#cs
#FUNCTION# ===============================================================================
 Name...........: _PixelSearchCenter
 Description ...: This UDF wraps PixelSearch in order to allow users to specify the center -
                  coordinates of a PixelSearch square and the height of the square instead -
                  of the slightly confusing: 
                          "left coordinate of rectangle." 
                          "top coordinate of rectangle." 
                          "right coordinate of rectangle." 
                          "bottom coordinate of rectangle."
                   This is more intuitive to those of us who are new to AutoIt and/or -
                   computer programming in general.
 Syntax .........: _PixelSearchCenter($iCenterX, $iCenterY, $iHeight, $iColor, $iShadeVariation="", $iStep="", $sHWND="")
 Parameters ....: $iCenterX -- X coordinate of PixelSearch center
                  $iCenterY -- Y coordinate of PixelSearch center
                  $iHeight -- The total height of the PixelSearch square.
                  $iColor -- "Colour value of pixel to find (in decimal or hex)."
                  $iShadeVariation -- "[optional] A number between 0 and 255 to indicate the -
                                       allowed number of shades of variation of the red, green, -
                                       and blue components of the colour. Default is 0 (exact match)."
                  $iStep -- "[optional] Instead of searching each pixel use a value larger than 1 to skip -
                                pixels (for speed). E.g. A value of 2 will only check every other pixel. Default is 1."
                  $sHWND -- "[optional] Window handle to be used."
 Return values .: Success - The first set of coordinates of the desired color.
                  Failure - Returns 0 and Sets @Error:
                  |0 - No error.
                  |1 - Color was not found.
 Author ........: Matthew R. Goude
 Modified.......:
 Remarks .......: All quotes are from the helpfile. -
                  I got this idea from a Processing language tutorial. (http://www.processing.org/)
 Related .......: PixelSearch
 Link ..........; (http://www.autoitscript.com/forum/index.php?showtopic=87319)
 Example .......; Yes
 ==========================================================================================
#ce

#include-Once

Local $iColor = 0 
Local $iHalf = 0
Local $iHeight = 0
Local $iShadeVariation = 0
Local $iStep = 0
Local $iXYCoord[ 2 ] = [ 0 ]
Local $iCenterX = 0 
Local $iCenterY = 0 
Local $iLeft = 0
Local $iTop = 0
Local $iRight = 0
Local $iBottom = 0
Local $sHWND = ""

Func _PixelSearchCenter($iCenterX,$iCenterY,$iHeight,$iColor,$iShadeVariation="",$iStep="",$sHWND="")
    $iHalf = $iHeight/2
    $iLeft = $iCenterX-$iHalf
    $iTop = $iCenterY-$iHalf
    $iRight = $iCenterX+$iHalf
    $iBottom = $iCenterY+$iHalf
    $iXYCoord = PixelSearch($iLeft,$iTop,$iRight,$iBottom,$iColor,$iShadeVariation,$iStep,$sHWND)
    If @error = 0 Then
        SetError( 0 )
        Return $iXYCoord
    Else
        SetError( 1 )
        Return 0
    EndIf
EndFunc
Edited by jaberwocky6669

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  

×