Jump to content

How to make a loop which will end when a certain color appears on an area of the screen?


Recommended Posts

Hello. It's the first time that I post here and I got a question (was searching for something similiar on the forums but couldn't find it). The exact thing which I need is a "do" loop which will be executed until a certain color appears on a certain area of the screen. I was thinking on something like:

Do
       ...
Until PixelSearch (...)

, but it always ends up being an endless loop. Sorry if that was a stupid question, I'm kinda new to AutoIt, but I know the basics. Please keep the answers as simple as possible, thanks in advance.

Link to comment
Share on other sites

Edit: ye I also got endless loop after I quit the script for a moment. must be a new♠ bug. worked fine next time I run it tho.

While   1

    Sleep(500)

    $var = PixelGetColor( 10 , 100 ) ; position x & y axis on the screen
    $var = Hex($var, 6)

    ConsoleWrite($var & @CRLF)

    If $var = 'FFFFFF' then ExitLoop

WEnd

Edit2:

there is a UDF,I dont remember who made it cuz he did not include that information, but its great if u want to choose a color: I think its called FLEXY

#include <GUIConstants.au3>
#include <Color.au3>
#include <Array.au3>
#Include <Misc.au3>

$Form1 = GUICreate("FLEXY", 321, 148, 340, 243, -1, BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE))
$Label1 = GUICtrlCreateLabel("Mouse Coordinates :", 16, 12, 101, 17)
$Label2 = GUICtrlCreateLabel("Hex Color :", 16, 36, 56, 17)
$Label3 = GUICtrlCreateLabel("Decimal Color :", 16, 60, 75, 17)
$Label4 = GUICtrlCreateLabel("RGB Color :", 16, 84, 60, 17)
$Label5 = GUICtrlCreateLabel("", 216, 34, 42, 40)
$Input1 = GUICtrlCreateInput("X", 120, 8, 65, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY))
$Input2 = GUICtrlCreateInput("Y", 192, 8, 65, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY))
$Input3 = GUICtrlCreateInput("", 120, 32, 89, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY))
$Input4 = GUICtrlCreateInput("", 120, 56, 89, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY))
$Input5 = GUICtrlCreateInput("Red", 120, 80, 41, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY))
$Input6 = GUICtrlCreateInput("Green", 168, 80, 41, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY))
$Input7 = GUICtrlCreateInput("Blue", 216, 80, 41, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL, $ES_READONLY))
$Button1 = GUICtrlCreateButton("Copy", 264, 6, 41, 24, 0)
$Button2 = GUICtrlCreateButton("Copy", 264, 30, 41, 24, 0)
$Button3 = GUICtrlCreateButton("Copy", 264, 54, 41, 24, 0)
$Button4 = GUICtrlCreateButton("Copy", 264, 78, 41, 24, 0)
$Button5 = GUICtrlCreateButton("Copy All", 128, 112, 57, 25, 0)
$Button6 = GUICtrlCreateButton("Freeze", 16, 112, 57, 25, 0)
$Button7 = GUICtrlCreateButton("Hotkeys", 72, 112, 57, 25, 0)
$Button8 = GUICtrlCreateButton("Pixel Search", 184, 112, 81, 25, 0)
$Button9 = GUICtrlCreateButton("Exit", 264, 112, 41, 25, 0)

$2Form1 = GUICreate("FLEXY - Pixel Search", 321, 308, 340, 423, -1, BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE))
$2Label1 = GUICtrlCreateLabel("First Coordinate :", 16, 24, 83, 17)
$2Label2 = GUICtrlCreateLabel("Second Coordinate :", 16, 48, 101, 17)
$2Input1 = GUICtrlCreateInput("X", 128, 18, 65, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL))
$2Input2 = GUICtrlCreateInput("Y", 200, 18, 65, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL))
$2Input3 = GUICtrlCreateInput("X", 128, 42, 65, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL))
$2Input4 = GUICtrlCreateInput("Y", 200, 42, 65, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL))
$2Label3 = GUICtrlCreateLabel("Hex Color - Variation :", 16, 72, 106, 17)
$2Input5 = GUICtrlCreateInput("", 128, 66, 97, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL))
$2Input6 = GUICtrlCreateInput("0", 232, 66, 33, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL))
$2Button1 = GUICtrlCreateButton("C", 272, 18, 25, 21, 0)
GUICtrlSetTip(-1, "Copy here main window coordinates.")
$2Button2 = GUICtrlCreateButton("C", 272, 42, 25, 21, 0)
GUICtrlSetTip(-1, "Copy here main window coordinates.")
$2Button3 = GUICtrlCreateButton("C", 272, 66, 25, 21, 0)
GUICtrlSetTip(-1, "Copy here main window hex color.")
$2Button4 = GUICtrlCreateButton("Search 1", 16, 96, 65, 25, 0)
GUICtrlSetTip(-1, "Search and if found only show result.")
$2Button5 = GUICtrlCreateButton("Search 2", 88, 96, 65, 25, 0)
GUICtrlSetTip(-1, "Search and if found move mouse there.")
$2Button6 = GUICtrlCreateButton("Search 3", 160, 96, 65, 25, 0)
GUICtrlSetTip(-1, "Search and if found click there.")
$2Button7 = GUICtrlCreateButton("Search 4", 232, 96, 65, 25, 0)
GUICtrlSetTip(-1, "Search and if found click there and move mouse preceding position.")
$2Group1 = GUICtrlCreateGroup("Repeat search ?", 16, 168, 281, 41)
$2Radio2 = GUICtrlCreateRadio("No", 25, 185, 41, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetTip(-1, "Normal search.")
$2Radio1 = GUICtrlCreateRadio("Yes", 67, 185, 41, 17)
GUICtrlSetTip(-1, "Search until press esc button.")
$2Radio8 = GUICtrlCreateRadio("Loop until error", 112, 185, 89, 17)
GUICtrlSetTip(-1, "Search until not find.")
$2Radio9 = GUICtrlCreateRadio("Loop until find", 204, 185, 89, 17)
GUICtrlSetTip(-1, "Search until find.")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$2Group2 = GUICtrlCreateGroup("Search where ?", 16, 210, 281, 41)
$2Radio3 = GUICtrlCreateRadio("Fullscreen search", 121, 227, 105, 17)
GUICtrlSetTip(-1, "Search in fullscreen.")
$2Radio4 = GUICtrlCreateRadio("Region search", 25, 227, 89, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetTip(-1, "Search in your coordinates.")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$2Group3 = GUICtrlCreateGroup("Click which button ?", 16, 252, 281, 41)
$2Radio5 = GUICtrlCreateRadio("Left button", 25, 269, 73, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetTip(-1, "For Search 3 and 4 methods using left mouse click.")
$2Radio6 = GUICtrlCreateRadio("Left button double", 183, 269, 105, 17)
GUICtrlSetTip(-1, "For Search 3 and 4 methods using double left mouse click.")
$2Radio7 = GUICtrlCreateRadio("Right Button", 100, 269, 81, 17)
GUICtrlSetTip(-1, "For Search 3 and 4 methods using right mouse click.")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$2Label4 = GUICtrlCreateLabel("Search Result ( Coordinate ) :", 16, 136, 143, 17)
$2Input7 = GUICtrlCreateInput("X", 160, 132, 65, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL))
$2Input8 = GUICtrlCreateInput("Y", 232, 132, 65, 21, BitOR($ES_CENTER, $ES_AUTOHSCROLL))

Global $pos, $colordec, $colorrgb, $colorhex, $position, $mousex, $mousey, $boxx, $boxy, $boxhex, $search, $search1x, $search1y, $search2x, $search2y, $searchcolor, $searchshade, $searcharray
Global $st1, $st2, $st3, $st4, $colorred, $colorgreen, $colorblue, $pause, $error, $s
Global Const $width = @DesktopWidth
Global Const $height = @DesktopHeight

HotKeySet("{PAUSE}", "pause")
HotKeySet("^!q", "copy1")
HotKeySet("^!w", "copy2")
HotKeySet("^!e", "copy3")
HotKeySet("^!r", "copy4")
HotKeySet("^!t", "copy5")
$key = "09" ;TAB key

GUISetState(@SW_SHOW, $Form1)

While 1
    $msg = GUIGetMsg(1)
    If $pause = 0 Then
        start()
    EndIf
    Select
        Case $msg[0] = $Button1
            copy1()
        Case $msg[0] = $Button2
            copy2()
        Case $msg[0] = $Button3
            copy3()
        Case $msg[0] = $Button4
            copy4()
        Case $msg[0] = $Button5
            copy5()
        Case $msg[0] = $Button6
            If $pause = 0 Then
                $pause = 1
            Else
                $pause = 0
            EndIf
        Case $msg[0] = $Button7
            MsgBox(0, "Hotkeys", "Pause/Break = Freeze or Unfreeze" & @CRLF & "Ctrl + Alt + Q = Copy Mouse Coordinates " & @CRLF & "Ctrl + Alt + W = Copy Hex Color" & @CRLF & "Ctrl + Alt + E = Copy Decimal Color" & @CRLF & "Ctrl + Alt + R = Copy RGB Color" & @CRLF & "Ctrl + Alt + T = Copy All Datas" & @CRLF & "TAB = Stop Pixel Search Repeat")
        Case $msg[0] = $Button8
            GUISetState(@SW_SHOW, $2Form1)
        Case $msg[0] = $Button9
            Exit
        Case $msg[0] = $2Button1
            $boxx = GUICtrlRead($Input1)
            GUICtrlSetData($2Input1, $boxx)
            $boxy = GUICtrlRead($Input2)
            GUICtrlSetData($2Input2, $boxy)
        Case $msg[0] = $2Button2
            $boxx = GUICtrlRead($Input1)
            GUICtrlSetData($2Input3, $boxx)
            $boxy = GUICtrlRead($Input2)
            GUICtrlSetData($2Input4, $boxy)
        Case $msg[0] = $2Button3
            $boxhex = GUICtrlRead($Input3)
            GUICtrlSetData($2Input5, $boxhex)
        Case $msg[0] = $2Button4
            key1()
        Case $msg[0] = $2Button5
            key2()
        Case $msg[0] = $2Button6
            key3()
        Case $msg[0] = $2Button7
            key4()
        Case $msg[0] = $GUI_EVENT_CLOSE
            If $msg[1] = $Form1 Then Exit
            If $msg[1] = $2Form1 Then GUISetState(@SW_HIDE, $2Form1)
    EndSelect
WEnd

Func start()
    Sleep(50)
    test1()
    test2()
    GUICtrlSetData($Input1, $mousex)
    GUICtrlSetData($Input2, $mousey)
    GUICtrlSetData($Input3, $colorhex)
    GUICtrlSetData($Input4, $colordec)
    GUICtrlSetData($Input5, $colorred)
    GUICtrlSetData($Input6, $colorgreen)
    GUICtrlSetData($Input7, $colorblue)
    GUICtrlSetBkColor($Label5, $colorhex)
EndFunc   ;==>start

Func test1()
    $pos = MouseGetPos()
    $colordec = PixelGetColor($pos[0], $pos[1])
    $colorhex = "0x" & Hex($colordec, 6)
    $colorred = _ColorGetRed($colordec)
    $colorgreen = _ColorGetGreen($colordec)
    $colorblue = _ColorGetBlue($colordec)
    $position = $pos[0] & ", " & $pos[1]
    $mousex = $pos[0]
    $mousey = $pos[1]
EndFunc   ;==>test1

Func test2()
    $st1 = StringFormat($pos[0] & ", " & $pos[1])
    $st2 = StringFormat($colorhex)
    $st3 = StringFormat($colordec)
    $st4 = StringFormat($colorred & " " & $colorgreen & " " & $colorblue)
EndFunc   ;==>test2

Func test3()
    $search1x = GUICtrlRead($2Input1)
    $search1y = GUICtrlRead($2Input2)
    $search2x = GUICtrlRead($2Input3)
    $search2y = GUICtrlRead($2Input4)
    $searchcolor = GUICtrlRead($2Input5)
    $searchshade = GUICtrlRead($2Input6)
EndFunc   ;==>test3

Func key1()
    $s = 1
    searchnow()
EndFunc   ;==>key1

Func key2()
    $s = 2
    searchnow()
EndFunc   ;==>key2

Func key3()
    $s = 3
    searchnow()
EndFunc   ;==>key3

Func key4()
    $s = 4
    searchnow()
EndFunc   ;==>key4

;test
Func kkey1()
    Sleep(2500)
    $s = 1
    searchnow()
EndFunc   ;==>kkey1

Func kkey2()
    Sleep(2500)
    $s = 2
    searchnow()
EndFunc   ;==>kkey2

Func kkey3()
    Sleep(2500)
    $s = 3
    searchnow()
EndFunc   ;==>kkey3

Func kkey4()
    Sleep(2500)
    $s = 4
    searchnow()
EndFunc   ;==>kkey4

Func searchnow()
    If GUICtrlRead($2Radio2) = $GUI_CHECKED Then
        searchnow2()
    Else
        If GUICtrlRead($2Radio1) = $GUI_CHECKED Then
            While 1
                searchnow2()
                If _IsPressed($key) Then ExitLoop
            WEnd
        Else
            If GUICtrlRead($2Radio8) = $GUI_CHECKED Then
                While 1
                    searchnow2()
                    If $error = 1 Then ExitLoop
                    If _IsPressed($key) Then ExitLoop
                WEnd
            Else
                While 1
                    searchnow2()
                    If $error = 0 Then ExitLoop
                    If _IsPressed($key) Then ExitLoop
                WEnd
            EndIf
        EndIf
    EndIf
EndFunc   ;==>searchnow

Func searchnow2()
    If $s = 1 Then
        search1()
    ElseIf $s = 2 Then
        search2()
    ElseIf $s = 3 Then
        search3()
    ElseIf $s = 4 Then
        search4()
    EndIf
EndFunc   ;==>searchnow2

Func search1()
    searchpixel()
    If $error = 0 Then
        $searcharray = $search
        GUICtrlSetData($2Input7, $searcharray[0])
        GUICtrlSetData($2Input8, $searcharray[1])
    EndIf
EndFunc   ;==>search1

Func search2()
    searchpixel()
    If $error = 0 Then
        $searcharray = $search
        MouseMove($searcharray[0], $searcharray[1], 0)
        GUICtrlSetData($2Input7, $searcharray[0])
        GUICtrlSetData($2Input8, $searcharray[1])
    EndIf
EndFunc   ;==>search2

Func search3()
    searchpixel()
    If $error = 0 Then
        $searcharray = $search
        buttons()
        GUICtrlSetData($2Input7, $searcharray[0])
        GUICtrlSetData($2Input8, $searcharray[1])
    EndIf
EndFunc   ;==>search3

Func search4()
    searchpixel()
    If $error = 0 Then
        $searcharray = $search
        buttons()
        MouseMove($pos[0], $pos[1], 0)
        GUICtrlSetData($2Input7, $searcharray[0])
        GUICtrlSetData($2Input8, $searcharray[1])
    EndIf
EndFunc   ;==>search4

Func searchpixel()
    Sleep(10)
    $error = 0
    test3()
    If $searchshade > 255 Then
        $error = 1
        MsgBox(0, "", "Variation need lower than 255", 5)
    Else
        If GUICtrlRead($2Radio4) = $GUI_CHECKED Then
            $search = PixelSearch($search1x, $search1y, $search2x, $search2y, $searchcolor, $searchshade)
            If @error Then $error = 1
        Else
            $searchcolor = GUICtrlRead($2Input5)
            $searchshade = GUICtrlRead($2Input6)
            $search = PixelSearch(0, 0, $width, $height, $searchcolor, $searchshade)
            If @error Then $error = 1
        EndIf
    EndIf
    If $error = 1 Then
        GUICtrlSetData($2Input7, "Not found!")
        GUICtrlSetData($2Input8, "Not found!")
    EndIf
EndFunc   ;==>searchpixel

Func buttons()
    If GUICtrlRead($2Radio5) = $GUI_CHECKED Then
        MouseClick("left", $searcharray[0], $searcharray[1], 1, 0)
    Else
        If GUICtrlRead($2Radio6) = $GUI_CHECKED Then
            MouseClick("left", $searcharray[0], $searcharray[1], 2, 0)
        Else
            MouseClick("right", $searcharray[0], $searcharray[1], 1, 0)
        EndIf
    EndIf
EndFunc   ;==>buttons

Func pause()
    If $pause = 0 Then
        $pause = 1
    Else
        $pause = 0
    EndIf
EndFunc   ;==>pause

Func copy1()
    ClipPut($st1)
EndFunc   ;==>copy1

Func copy2()
    ClipPut($st2)
EndFunc   ;==>copy2

Func copy3()
    ClipPut($st3)
EndFunc   ;==>copy3

Func copy4()
    ClipPut($st4)
EndFunc   ;==>copy4

Func copy5()
    ClipPut($st1 & " " & $st2 & " " & $st3 & " " & $st4)
EndFunc   ;==>copy5
Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

Your code causes undefined behaviour. Read the help on PixelSearch, it doesn't actually say what it returns if the pixel is not found. Any time that a function sets @error, the result should be treated as undefined, you must check @error before using the return value of PixelSearch. So you can't use PixelSearch as a logical function to use in a condition such as a loop terminator.

This should do as a quick fix...

Do
       ...
       PixelSearch (...)
Until Not( @error )

but I'd prefer to write a wrapper, maybe call it _PixelFound() or something like that, that does PixelSearch and returns True if the pixel is found.

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...