I'm new to AHK and to programming in general. I've been puzzling over writing this for the last 3 days (don't laugh) and whilst it now works I suspect I've massively overcomplicated the whole thing and would appreciate a critique. I also don't fully understand what I've written or some of the problems I encountered..
The premise is that the script will search a defined area for (colour) and count the number of pixels found. I've made something similar work using PixelGetColor but it was taking too long to return a result (multiple seconds for the same search area).
Code: Select all
CoordMode, Pixel, Relative
PixelFindandCount:
count := 0
; Search area is a small rectangle in MS Paint with red pixels marked
x1 := 13
y1 := 150
x2 := 24
y2 := 164
OuterloopErr := 0
Loop
{
PixelSearch, cx, cy, x1, y1, x2, y2, 0xED1C24, 1, Fast RGB
If Errorlevel = 1
OuterloopErr := 1
If ErrorLevel = 0
{
count++
px1 := (cx + 1)
px2 := x2
py1 := cy
py2 := cy
Loop ; this loop is to complete counting on (y) where pixel found.
{
PixelSearch, cx2, cy2, px1, py1, px2, py2, 0xED1C24, 1, Fast RGB
If Errorlevel = 0
{
count++
px1 := (cx2 + 1) ; If colour found then increment x
}
if Errorlevel = 1
{
y1 := (cy + 1) ; if not found then increment y and break loop
break
}
}
continue
}
If OuterloopErr = 1 ; Have used a variable here as Errorlevel seemed to be pulling through from the inner loop?
Msgbox, Search complete. %count% found
break
}
Cheers