Jump to content

Finding two diffrent colors with PixelSearch


Recommended Posts

I am trying to get a program to do a pixel search for two very different colors at the same time.

dim $Green = 0x26B219
dim $Red = 0xF30000
dim $Coord

$coord = pixelsearch(100, 100, 200, 200, $Green) or $coord = pixelsearch(100, 100, 200, 200, $Red)
    if not @error then
        ;Found Color so no need to loop
    elseif @error Then
        while @error = 1
            ;Search until you find Green or Red
            $coord = pixelsearch(100, 100, 200, 200, $Green) or $coord = pixelsearch(100, 100, 200, 200, $Red)
        WEnd
    endif

What happens is the script loops forever despite Either the Red or Green color showing up in the correct place. What do I need to do so that both colors can be searched for and when one of them appears the loop ends?

Edited by Schwarzwald
Link to comment
Share on other sites

Do

.

.

.

Until IsArray($Coords1) Or IsArray($Corrds2)

It might be better to store the @error macro value after each call and test them as shown above...

It's the responsibility of you to develop a steel rule to avoid infinite loop, so you might want to think it a different way.

Also, I don't think the $coord = PixelSearch(...) Or $coord = PixelSearch(...) is something valid as a statement in this context...

Link to comment
Share on other sites

do
$search1 = pixelsearch(100, 100, 200, 200, 0x26B219)
$search2 = pixelsearch(100, 100, 200, 200, 0xF30000)
sleep(100) ;saves cpu 
until $search1 <> 1 or $search2 <>1

Edited by Aceguy
Link to comment
Share on other sites

It isn't a valid statement, the best way to do that would be to incorporate it into a array.

Dim $aiCoord, $asColors[2] = [0x26B219, 0xF30000]
Dim $bFound = False

While Not $bFound
    For $sColor In $asColors
        $aiCoord = PixelSearch(100, 100, 200, 200, $sColor)
        If IsArray($aiCoord) Then $bFound = True
    Next
WEnd
Edited by TerarinK

0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

Link to comment
Share on other sites

Well if your are to corcerned with the CPU, on mine it only uses a 5% of the total CPU power so I can almost guarntee that you are using wait or sleep in the PixelSearch function, you should add a timer. Maybe even put it is a a UDF if it maybe so much used:

Dim $asTesting

$asTesting = _PixelSearchArray(100, 100, 200, 200, "0x26B219|0xF30000|0xF0D28C")
If @error Then MsgBox(0, "", "Color not found on _PixelSearchArray: " & @error)
If IsArray($asTesting) Then MsgBox(0, "", $asTesting[0] & ", " & $asTesting[1])

; #FUNCTION# ====================================================================================================================
; Name...........: _PixelSearchArray
; Description ...: Handles a Pixel search of multiple colors with a timeout function inside.
; Syntax.........: _PixelSearchArray($iLeft, $iTop, $iRight, $iBottom, $sArrayColor[, $iVariation[, $iStep[, $hWindow[, $iTimeout]]]])
; Parameters ....: $iLeft - Left coordinate of rectangle.
;                  $iTop - Top coordinate of rectangle.
;                  $iRight - Right coordinate of rectangle.
;                  $iBottom - Bottom coordinate of rectangle.
;                  $sArrayColor - Array of color separated with a "|" with the value of pixel to find (in decimal or hex).
;                  $iVariation [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.
;                  $hWindow [optional] - Window handle to be used.
;                  $iTimeout [optional] - Specifies how long to wait (in seconds). Default is to wait 30 seconds.
; Return values .: Success: Returns a two-element array of pixel's coordinates. (Array[0] = x, Array[1] = y)
;                  Failure: Sets @error to 1 if color is not found.
; Author ........: Terarin Kerowyn <terarink_msn at hotmail dot com>
; Modified.......:
; Remarks .......:
; Related .......: PixelSearch
; Link ..........: http://www.autoitscript.com/forum/index.php?showtopic=89171&st=0&gopid=640877&#entry640877
; ===============================================================================================================================
Func _PixelSearchArray($iLeft = 0, $iTop = 0, $iRight = 0, $iBottom = 0, $sArrayColor = "", $iVariation = Default, $iStep = Default, $hWindow = Default, $iTimeout = 30)
    Dim $aiCoord, $asColors = StringSplit($sArrayColor, "|")
    Dim $iDiffTimer, $iTimer = TimerInit()

    $iTimeout *= 1000

    While $iDiffTimer <= $iTimeout
        For $sColor In $asColors
            $aiCoord = PixelSearch($iLeft, $iTop, $iRight, $iBottom, $sColor, $iVariation, $iStep, $hWindow)
            If IsArray($aiCoord) Then Return $aiCoord
        Next
        MsgBox(0, "", "Timer", 3)
        $iDiffTimer = TimerDiff($iTimer)
    WEnd

    Return SetError(1, 0, 0)
EndFunc   ;==>_PixelSearchArray

This will work

Edited by TerarinK

0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...