#include "_ArraySort_2ary.au3"

Dim $DateiOut = @ScriptDir & "\Farbbereiche.txt"
Dim $Farbe = 0x00314A
Dim $Schrittweite = 12

_pix_scan($Farbe, $Schrittweite, $DateiOut)

;------------------------------------------------------------------------------------------------------------
;	Funktion		_pix_scan($color, $step, $file)
;
;	Beschreibung	scannt Display nach gesuchter Farbe, Ausgabe in Datei
;					Pixelbereiche 2-stufig sortiert
;
;	Parameter		$color:		zu suchende Farbe
;					$step:		Rastergröße
;					$file:		Ausgabedatei
;
;	Return			Datei mit den gefundenen Pixelpositionen
;
;	Erfordernisse	Funktion: _ArraySort_2ary( )
;					Nach dem Funktionsaufruf wird der Scan erst mit {F9} gestartet
;					um z.B. vorab ein Spiel starten zu können
;					
;	Autor			BugFix (bugfix@autoit.de)
;------------------------------------------------------------------------------------------------------------
#include <File.au3>
#Include <Misc.au3>
Func _pix_scan($color, $step, $file)
Local $endX = @DesktopWidth, $endY = @DesktopHeight
Local $left = 0, $top = 0, $right = $step, $bottom = $step
Local $arPos, $arSort[1][1]	
	Do
		Sleep(100)
	Until _IsPressed("78") ; F9
	$fh = FileOpen($file,1)
	Do
		For $left = 0 To $endX Step $step
			$pos = PixelSearch($left, $top, $right, $bottom, $color, 0, $step)
			If IsArray($pos) Then FileWriteLine($file, $pos[0] & "|" & $pos[1])
			$right += $step
		Next
		$left = 0
		$right = $step
		$top += $step
		$bottom += $step
	Until $bottom > $endY
	FileClose($fh)
	Beep(500, 2000)
	_FileReadToArray($file, $arPos)
	FileDelete($file)	
	ReDim $arSort[UBound($arPos)][2]
	For $i = 0 To UBound($arPos)-1
		$var = StringSplit($arPos[$i], "|")
		If ( IsArray($var) And UBound($var) = 3 ) Then
			$arSort[$i][0] = Number($var[1])
			$arSort[$i][1] = Number($var[2])
		EndIf
	Next
	$arSort = _ArraySort_2ary($arSort)
	$fh = FileOpen($file,1)
	For $i = 0 To UBound($arSort)-1
		If $arSort[$i][0] <> "" And $arSort[$i][1] <> "" Then _
			FileWriteLine($fh, "X: " & $arSort[$i][0] & " , Y: " & $arSort[$i][1])
	Next
	FileClose($fh)
EndFunc