Can AHK PixelSearch look for multiple colors within a single variable? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PipeDreams
Posts: 165
Joined: 19 Dec 2015, 00:20

Can AHK PixelSearch look for multiple colors within a single variable?

10 Aug 2019, 20:54

How come white is the only color that is recognized, and how do i get it to recognize the other colors?

Code: Select all

#SingleInstance Force
Process, Priority, , Above Normal
SetControlDelay, -1
SetBatchLines, -1
SetWinDelay, -1
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
CoordMode, ToolTip, Screen
MouseMove, (A_ScreenWidth/2), (A_ScreenHeight/2)
IfNotExist, %USERPROFILE%\Downloads\Testerpic2.jpg
{	UrlDownloadToFile, https://www.autohotkey.com/boards/download/file.php?id=7788, %USERPROFILE%\Downloads\Testerpic2.jpg
	SoundBeep, 300, 50
} SplashImage, %USERPROFILE%\Downloads\Testerpic2.jpg, B FS18, Teast Image
Loop,
{	Text1 = Pixel Found In Box `n%Color% Under Pointer
	Text2 = Searching In Box `n%Color% Under Pointer
	LFColors := (0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF)
	; 			(   Red	 | Green  |	 Blue  | Yellow |  Black | White  )
	Loop, Parse, LFColors, `|
	{	WinGetPos,,, WindowWidth, WindowHeight, %WinTitle%
		MouseGetPos, X, Y
		;	Uupper Left Corner		|	Lower Right Corner
		;	Left		   Top		|	Right		Bottom
		X1 := (X-50), Y1 := (Y-50), X2 := (X+50), Y2 := (Y+50) ;Adjust The Size of The Search Area/Box.
		FrameThickness := (1), FrameWidth := (X2-X1), FrameHeight := (Y2-Y1)
		Gui, 1: Margin, %FrameThickness%, %FrameThickness%
		Gui, 1: Color, %FrameColor%
		Gui, 1: Add, Text, W%FrameWidth% H%FrameHeight% 0x6
		Gui, 1: -Caption +AlwaysOnTop +ToolWindow +LastFound
		Gui, 1: Show, NoActivate, Gui1
		WinSet, TransColor, White
		WinMove, %WinTitle%,, (X-(WindowWidth/2)), (Y-(WindowHeight/2))
		;PixelSearch, X, Y, X1, Y1, X2, Y2, %LFColors%, RGB, 10, Fast	;This has the same result as below.
		PixelSearch, X, Y, X1, Y1, X2, Y2, A_LoopField, RGB, 10, Fast	;This has the same result as above.
		If (!ErrorLevel)
		{	FrameColor := ("Lime")
			MouseGetPos, X, Y
			PixelGetColor, Color, X, Y, RGB, Fast
			ToolTip, %Text1%, (X-52), (Y+57), 1
		} Else,
		{	FrameColor := ("Red")
			MouseGetPos, X, Y
			PixelGetColor, Color, X, Y, RGB, Fast
			ToolTip, %Text2%, (X-58), (Y+57), 1
}	}	} Return
~Esc::ExitApp
Attachments
Testerpic2.jpg
Testerpic2.jpg (6.96 KiB) Viewed 9246 times
User avatar
boiler
Posts: 17075
Joined: 21 Dec 2014, 02:44

Re: Can AHK PixelSearch look for multiple colors within a single variable?

11 Aug 2019, 00:21

It appears that you expect LFColors to be a string that you parse, but it is actually an expression with a bunch of values that are OR'ed together using the | operator, which results in a value of 0xFFFFFF.

Instead of:
LFColors := (0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF)
it should be the following to make it a string:
LFColors := "0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF"

I haven't checked the rest of your code. There may be other issues, but start by making that change.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Can AHK PixelSearch look for multiple colors within a single variable?

11 Aug 2019, 03:10

Boiler is correct.

LFColors := "0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF"

It could also be

LFColors = 0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF

And you can change the delimiter. Instead of `|, it could be `n (new line). Sometimes it's easier to put a list vertically, other times horizontally might work better.

Code: Select all

LFColors =
(
0xED1B24
0x23B14D
0x3F47CC
0xFEF200
0x000000
0xFFFFFF
)
or it can be this way

Code: Select all

LFColors :="
(
0xED1B24
0x23B14D
0x3F47CC
0xFEF200
0x000000
0xFFFFFF
)"
In this case the delimiter is `n and with Loop, Parse would look like this.

Code: Select all

#SingleInstance force

LFColors :="
(
0xED1B24
0x23B14D
0x3F47CC
0xFEF200
0x000000
0xFFFFFF
)"

X1 := 0, Y1 := 0, X2 := 1920, Y2 := 1080

Loop, Parse, LFColors, `n
{	
	PixelSearch, X, Y, X1, Y1, X2, Y2, A_LoopField, RGB, 0, Fast
	Msgbox % A_LoopField
	If (!ErrorLevel)
	{	
		Msgbox,,, Found Something!
	} 
	Else
	{	
		Msgbox,,, Found Nothing!
	}
} 
User avatar
PipeDreams
Posts: 165
Joined: 19 Dec 2015, 00:20

Re: Can AHK PixelSearch look for multiple colors within a single variable?

11 Aug 2019, 03:53

boiler wrote:
11 Aug 2019, 00:21
It appears that you expect LFColors to be a string that you parse, but it is actually an expression with a bunch of values that are OR'ed together using the | operator, which results in a value of 0xFFFFFF.

Instead of:
LFColors := (0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF)
it should be the following to make it a string:
LFColors := "0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF"

I haven't checked the rest of your code. There may be other issues, but start by making that change.
SOTE wrote:
11 Aug 2019, 03:10
Boiler is correct.

LFColors := "0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF"

It could also be

LFColors = 0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF

And you can change the delimiter. Instead of `|, it could be `n (new line). Sometimes it's easier to put a list vertically, other times horizontally might work better.

Code: Select all

LFColors =
(
0xED1B24
0x23B14D
0x3F47CC
0xFEF200
0x000000
0xFFFFFF
)
or it can be this way

Code: Select all

LFColors :="
(
0xED1B24
0x23B14D
0x3F47CC
0xFEF200
0x000000
0xFFFFFF
)"
In this case the delimiter is `n and with Loop, Parse would look like this.

Code: Select all

#SingleInstance force

LFColors :="
(
0xED1B24
0x23B14D
0x3F47CC
0xFEF200
0x000000
0xFFFFFF
)"

X1 := 0, Y1 := 0, X2 := 1920, Y2 := 1080

Loop, Parse, LFColors, `n
{	
	PixelSearch, X, Y, X1, Y1, X2, Y2, A_LoopField, RGB, 0, Fast
	Msgbox % A_LoopField
	If (!ErrorLevel)
	{	
		Msgbox,,, Found Something!
	} 
	Else
	{	
		Msgbox,,, Found Nothing!
	}
} 
Thank you for the suggestions you two; but unfortunately, it did not seem to do much.
Although black or white is now detectable, the script seems to cycle between true and false (i.e., doing the If AND Else instead of doing If OR Else) when placing either one in the search field (e.g., under the mouse pointer). I'm not sure why that is the case. Also, none of the other colors are detected at all.
Furthermore, when I strip the code down, PixelSearch fails to find any other color aside from black or white. Might these issues be related? I think this second issue has got something to do with the mouse pointer pixels interfering somehow because PixelSearch detects any color just fine when I don't try to bind it to the mouse pointer.

Code: Select all

#SingleInstance Force
Process, Priority, , Above Normal
SetControlDelay, -1
SetBatchLines, -1
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
CoordMode, ToolTip, Screen
MouseMove, (A_ScreenWidth/2), (A_ScreenHeight/2)
IfNotExist, %USERPROFILE%\Downloads\Testerpic2.jpg
{	UrlDownloadToFile, https://www.autohotkey.com/boards/download/file.php?id=7788, %USERPROFILE%\Downloads\Testerpic2.jpg
	SoundBeep, 300, 50
} SplashImage, %USERPROFILE%\Downloads\Testerpic2.jpg, B FS18, Teast Image
Loop, ;#3
{	Text1 = Pixel Found `n%Color% Under Pointer
	Text2 = Searching `n%Color% Under Pointer
	MouseGetPos, X, Y
	;PixelSearch, X, Y, X-1, Y-1, X+1, Y+1, 0xED1B24, 10, Fast	;Red Not Working
	;PixelSearch, X, Y, X-1, Y-1, X+1, Y+1, 0x23B14D, 10, Fast	;Green Not Working
	;PixelSearch, X, Y, X-1, Y-1, X+1, Y+1, 0x3F47CC, 10, Fast	;Blue Not Working
	;PixelSearch, X, Y, X-1, Y-1, X+1, Y+1, 0xFEF200, 10, Fast	;Yellow Not Working
	;PixelSearch, X, Y, X-1, Y-1, X+1, Y+1, 0x000000, 10, Fast	;Black Working
	PixelSearch, X, Y, X-1, Y-1, X+1, Y+1, 0xFFFFFF, 10, Fast	;White Working
	If (!ErrorLevel)
	{	MouseGetPos, X, Y
		PixelGetColor, Color, X, Y, RGB, Fast
		ToolTip, %Text1%, (X-52), (Y+57), 1
	} Else,
	{	MouseGetPos, X, Y
		PixelGetColor, Color, X, Y, RGB, Fast
		ToolTip, %Text2%, (X-58), (Y+57), 1
}	} Return
~Esc::ExitApp
SOTE, with your 3rd example I get the same result as described above, only the black and white pixels are detected, but none of the colored ones are detected, even with the PixelSearch Variation set to 255.
Attachments
SOTE.gif
SOTE.gif (247.11 KiB) Viewed 9090 times
User avatar
boiler
Posts: 17075
Joined: 21 Dec 2014, 02:44

Re: Can AHK PixelSearch look for multiple colors within a single variable?

11 Aug 2019, 06:05

It works for all of them when RGB is added to the PixelSearch options:
PixelSearch, X, Y, X-1, Y-1, X+1, Y+1, 0xED1B24, 10, RGB Fast

The format is BGR by default, and your search colors are in RGB. It found white and black because they are symmetrical.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: Can AHK PixelSearch look for multiple colors within a single variable?

11 Aug 2019, 06:16

Boiler is correct again. PipeDreams, looks like you are all set on this one. By the way, that BGR vs RGB thing bit me once too. So don't sweat it, lesson learned.
User avatar
PipeDreams
Posts: 165
Joined: 19 Dec 2015, 00:20

Re: Can AHK PixelSearch look for multiple colors within a single variable?

11 Aug 2019, 14:39

boiler wrote:
11 Aug 2019, 06:05
It works for all of them when RGB is added to the PixelSearch options:
PixelSearch, X, Y, X-1, Y-1, X+1, Y+1, 0xED1B24, 10, RGB Fast

The format is BGR by default, and your search colors are in RGB. It found white and black because they are symmetrical.
Oh, alright I see, I have been placing the RGB in the wrong spot :oops: lol, I feel so dumb for making that kind of mistake.

One more issue still plagues me; however, the script seems to cycle between true and false (i.e., doing the If AND Else instead of doing If OR Else) when placing anything but the first color in the search field (e.g., under the mouse pointer). Any ideas why that is the case, and how do I get it to stop doing If AND Else instead of doing If OR Else? The Break and Continue commands are not working as I expected them to.

Code: Select all

#SingleInstance Force
Process, Priority, , Above Normal
SetControlDelay, -1
SetBatchLines, -1
SetWinDelay, -1
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
CoordMode, ToolTip, Screen
MouseMove, (A_ScreenWidth/2), (A_ScreenHeight/2)
IfNotExist, %USERPROFILE%\Downloads\Testerpic2.jpg
{	UrlDownloadToFile, https://www.autohotkey.com/boards/download/file.php?id=7788, %USERPROFILE%\Downloads\Testerpic2.jpg
	SoundBeep, 300, 50
} SplashImage, %USERPROFILE%\Downloads\Testerpic2.jpg, B FS18, Teast Image
Loop, ;#4
{	Text1 = Pixel Found In Box `n%Color% Under Pointer
	Text2 = Searching In Box `n%Color% Under Pointer
	LFColors := "0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF"
	; 			(   Red	 | Green  |	 Blue  | Yellow |  Black | White  )	
	Loop, Parse, LFColors, `|
	{	PixelSearch, X, Y, X1, Y1, X2, Y2, A_LoopField, 0, Fast, RGB
		If (!ErrorLevel)
		{	FrameColor := "Lime"
			GoSub, Box
			PixelGetColor, Color, X, Y, RGB, Fast
			ToolTip, %Text1%, (X-52), (Y+57), 1
			Break
		} Else,
		{	FrameColor := "Red"
			GoSub, Box
			PixelGetColor, Color, X, Y, RGB, Fast
			ToolTip, %Text2%, (X-58), (Y+57), 1
			Continue
}	}	} Return
Box:
{	WinGetPos,,, WindowWidth, WindowHeight, %WinTitle%
	MouseGetPos, X, Y
	;	Uupper Left Corner		|	Lower Right Corner
	;	Left		   Top		|	Right		Bottom
	X1 := (X-50), Y1 := (Y-50), X2 := (X+50), Y2 := (Y+50) ;Adjust The Size of The Search Area/Box.
	FrameThickness := (1), FrameWidth := (X2-X1), FrameHeight := (Y2-Y1)
	Gui, 1: Margin, %FrameThickness%, %FrameThickness%
	Gui, 1: Color, %FrameColor%
	Gui, 1: Add, Text, W%FrameWidth% H%FrameHeight% 0x6
	Gui, 1: -Caption +AlwaysOnTop +ToolWindow +LastFound
	Gui, 1: Show, NoActivate, Gui1
	WinSet, TransColor, White
	WinMove, %WinTitle%,, (X-(WindowWidth/2)), (Y-(WindowHeight/2))
} Return
~Esc::ExitApp
Attachments
Boiler.gif
Boiler.gif (614.54 KiB) Viewed 9032 times
User avatar
boiler
Posts: 17075
Joined: 21 Dec 2014, 02:44

Re: Can AHK PixelSearch look for multiple colors within a single variable?

11 Aug 2019, 15:03

The reason is that you have a "Break" inside the If block which causes it to cycle to the next color and find that it's not there, and it continues until it cycles around and gets to the found color again. That's why it doesn't happen when the found color is red -- it's the first in the list, so when it's on red and you break to begin the cycle again, it's the first one it finds (it never gets to the point where it finds the not found colors). For the other colors, it has to cycle through red and sometimes some other colors before it gets to the one it found. As you'll notice, it takes longer to cycle through before the border turns green again for the colors at the end of the list (Black and White), while it cycles really fast for Green because it's the next one after Red.

Once you correct it from breaking outside the loop, you'll probably find that you can't rely on defining Text1 and Text2 in the outer loop since they use the value of Color which is found within the inner loop. Best practice is to define that text inside the inner loop after you've defined the value of Color.

Also, although it seems to work (probably because it's the last parameter), you're not supposed to have a comma between the parameter RGB and Fast in your PixelSearch and PixelGetColor commands. It's supposed to just be a space like RGB Fast.
User avatar
PipeDreams
Posts: 165
Joined: 19 Dec 2015, 00:20

Re: Can AHK PixelSearch look for multiple colors within a single variable?

11 Aug 2019, 17:13

boiler wrote:
11 Aug 2019, 15:03
The reason is that you have a "Break" inside the If block which causes it to cycle to the next color and find that it's not there, and it continues until it cycles around and gets to the found color again.
Ok, I understand that, but how do I get it to stop cycling true/false when colors 2-6 are found? If I do not use the break command at all the script will cycle like this for all of the colors, and when it is placed outside the if or else blocks only the first color is found while all the others fail but the cycling stops. Where would I put the break command if not in the if block?

PS thanks for the RGB & ToolTip info

Code: Select all

#SingleInstance Force
Process, Priority, , Above Normal
SetControlDelay, -1
SetBatchLines, -1
SetWinDelay, -1
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
CoordMode, ToolTip, Screen
MouseMove, (A_ScreenWidth/2), (A_ScreenHeight/2)
IfNotExist, %USERPROFILE%\Downloads\Testerpic2.jpg
{	UrlDownloadToFile, https://www.autohotkey.com/boards/download/file.php?id=7788, %USERPROFILE%\Downloads\Testerpic2.jpg
	SoundBeep, 300, 50
} SplashImage, %USERPROFILE%\Downloads\Testerpic2.jpg, B FS18, Teast Image
Loop, ;#1
{	LFColors := "0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF"
	; 			(   Red	 | Green  |	 Blue  | Yellow |  Black | White  )	
	Loop, Parse, LFColors, `|
	{	PixelSearch, X, Y, X1, Y1, X2, Y2, A_LoopField, 0, Fast RGB
		If (!ErrorLevel)
		{	FrameColor := "Lime"
			GoSub, Box
			PixelGetColor, Color, X, Y, RGB, Fast
			ToolTip, Pixel Found In Box `n%Color% Under Pointer, (X-52), (Y+57), 1
			;Break
		} Else,
		{	FrameColor := "Red"
			GoSub, Box
			PixelGetColor, Color, X, Y, RGB, Fast
			ToolTip, Searching In Box `n%Color% Under Pointer, (X-58), (Y+57), 1
			;Break
		} ;Break
	} 
} Return
Box:
{	WinGetPos,,, WindowWidth, WindowHeight, %WinTitle%
	MouseGetPos, X, Y
	;	Uupper Left Corner		|	Lower Right Corner
	;	Left		   Top		|	Right		Bottom
	X1 := (X-50), Y1 := (Y-50), X2 := (X+50), Y2 := (Y+50) ;Adjust The Size of The Search Area/Box.
	FrameThickness := (1), FrameWidth := (X2-X1), FrameHeight := (Y2-Y1)
	Gui, 1: Margin, %FrameThickness%, %FrameThickness%
	Gui, 1: Color, %FrameColor%
	Gui, 1: Add, Text, W%FrameWidth% H%FrameHeight% 0x6
	Gui, 1: -Caption +AlwaysOnTop +ToolWindow +LastFound
	Gui, 1: Show, NoActivate, Gui1
	WinSet, TransColor, White
	WinMove, %WinTitle%,, (X-(WindowWidth/2)), (Y-(WindowHeight/2))
} Return
~Esc::ExitApp
Essentially I want this loop

Code: Select all

Loop, ;#1
{	LFColors := "0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF"
	; 			(   Red	 | Green  |	 Blue  | Yellow |  Black | White  )	
	;GoSub, Box
	Loop, Parse, LFColors, `|
	{	PixelSearch, X, Y, X1, Y1, X2, Y2, A_LoopField, 0, Fast RGB
		If (!ErrorLevel)
		{	FrameColor := "Lime"
			GoSub, Box
			PixelGetColor, Color, X, Y, RGB, Fast
			ToolTip, Pixel Found In Box `n%Color% Under Pointer, (X-52), (Y+57), 1
			Break
		} Else,
		{	FrameColor := "Red"
			GoSub, Box
			PixelGetColor, Color, X, Y, RGB, Fast
			ToolTip, Searching In Box `n%Color% Under Pointer, (X-58), (Y+57), 1
			;Break
		} ;Break
	} 
} Return
to run like this loop

Code: Select all

Loop, ;#2
{	PixelSearch, X, Y, X1, Y1, X2, Y2, 0xED1B24, 0, Fast RGB
	If (!ErrorLevel)
	{	GoSub, Found
	} Else,
	{	PixelSearch, X, Y, X1, Y1, X2, Y2, 0x23B14D, 0, Fast RGB
		If (!ErrorLevel)
		{	GoSub, Found
		} Else,
		{	PixelSearch, X, Y, X1, Y1, X2, Y2, 0x3F47CC, 0, Fast RGB
			If (!ErrorLevel)
			{	GoSub, Found
			} Else,
			{	PixelSearch, X, Y, X1, Y1, X2, Y2, 0xFEF200, 0, Fast RGB
				If (!ErrorLevel)
				{	GoSub, Found
				} Else,
				{	PixelSearch, X, Y, X1, Y1, X2, Y2, 0x000000, 0, Fast RGB
					If (!ErrorLevel)
					{	GoSub, Found
					} Else,
					{	PixelSearch, X, Y, X1, Y1, X2, Y2, 0xFFFFFF, 0, Fast RGB
						If (!ErrorLevel)
						{	GoSub, Found
						} Else,
						{	GoSub, Searching						
}	}	}	}	}	}	} Return
Found:
{	FrameColor := "Lime"
	GoSub, Box
	PixelGetColor, Color, X, Y, RGB, Fast
	ToolTip, Pixel Found In Box `n%Color% Under Pointer, (X-52), (Y+57), 1
} Return
Searching:
{	FrameColor := "Red"
	GoSub, Box
	PixelGetColor, Color, X, Y, RGB, Fast
	ToolTip, Searching In Box `n%Color% Under Pointer, (X-58), (Y+57), 1
} Return
Last edited by PipeDreams on 11 Aug 2019, 18:49, edited 1 time in total.
User avatar
boiler
Posts: 17075
Joined: 21 Dec 2014, 02:44

Re: Can AHK PixelSearch look for multiple colors within a single variable?  Topic is solved

11 Aug 2019, 18:35

The issue is that you are acting as you cycle through the colors. You need to check all the colors, see if any of them are found, then act on whether any of them were found or not. The modification below does that. Note that as you move the cursor in an area with none of the search colors (especially downward), it may temporarily say that it has it found a color, which I believe is because it finds the black portion of the ToolTip before it has a chance to move out of the way. If you move the ToolTip to a stationary place out of the way, I think it should eliminate that issue. But it's not an issue once you stop moving the mouse. It correctly identifies whether or not one of the colors is found within the box. I changed the box color to Fuchsia rather than Red for the same reason. It would find the red box within the search area as it's moving, which matches one of the colors it searches for.

Code: Select all

#SingleInstance Force
Process, Priority, , Above Normal
SetControlDelay, -1
SetBatchLines, -1
SetWinDelay, -1
CoordMode, Pixel, Screen
CoordMode, Mouse, Screen
CoordMode, ToolTip, Screen
MouseMove, (A_ScreenWidth/2), (A_ScreenHeight/2)
IfNotExist, %USERPROFILE%\Downloads\Testerpic2.jpg
{	UrlDownloadToFile, https://www.autohotkey.com/boards/download/file.php?id=7788, %USERPROFILE%\Downloads\Testerpic2.jpg
	SoundBeep, 300, 50
} SplashImage, %USERPROFILE%\Downloads\Testerpic2.jpg, B FS18, Teast Image
LFColors := "0xED1B24|0x23B14D|0x3F47CC|0xFEF200|0x000000|0xFFFFFF"
; 			(   Red	 | Green  |	 Blue  | Yellow |  Black | White  )	

loop
{
	FoundColor := 0
	loop, Parse, LFColors, |
	{
		MouseGetPos, X, Y
		X1 := (X-50), Y1 := (Y-50), X2 := (X+50), Y2 := (Y+50)
		PixelSearch, X, Y, X1, Y1, X2, Y2, A_LoopField, 0, Fast RGB
		if (!ErrorLevel)
			FoundColor := 1
	}
	if (FoundColor)
	{
		FrameColor := "Lime"
		Gosub, Box
		PixelGetColor, Color, X, Y, RGB
		ToolTip, Pixel Found In Box `n%Color% Under Pointer, (X-52), (Y+57), 1
	}
	else
	{
		FrameColor := "Fuchsia"
		Gosub, Box
		PixelGetColor, Color, X, Y, RGB
		ToolTip, Searching In Box `n%Color% Under Pointer, (X-58), (Y+57), 1
	}
}
return

Box:
{
	WinGetPos,,, WindowWidth, WindowHeight, %WinTitle%
	MouseGetPos, X, Y
	;	Uupper Left Corner		|	Lower Right Corner
	;	Left		   Top		|	Right		Bottom
	X1 := (X-50), Y1 := (Y-50), X2 := (X+50), Y2 := (Y+50) ;Adjust The Size of The Search Area/Box.
	FrameThickness := (1), FrameWidth := (X2-X1), FrameHeight := (Y2-Y1)
	Gui, 1: Margin, %FrameThickness%, %FrameThickness%
	Gui, 1: Color, %FrameColor%
	Gui, 1: Add, Text, W%FrameWidth% H%FrameHeight% 0x6
	Gui, 1: -Caption +AlwaysOnTop +ToolWindow +LastFound
	Gui, 1: Show, NoActivate, Gui1
	WinSet, TransColor, White
	WinMove, %WinTitle%,, (X-(WindowWidth/2)), (Y-(WindowHeight/2))
}

~Esc::ExitApp
User avatar
PipeDreams
Posts: 165
Joined: 19 Dec 2015, 00:20

Re: Can AHK PixelSearch look for multiple colors within a single variable?

11 Aug 2019, 21:32

boiler wrote:
11 Aug 2019, 18:35
The issue is that you are acting as you cycle through the colors. You need to check all the colors, see if any of them are found, then act on whether any of them were found or not. The modification below does that. Note that as you move the cursor in an area with none of the search colors (especially downward), it may temporarily say that it has it found a color, which I believe is because it finds the black portion of the ToolTip before it has a chance to move out of the way. If you move the ToolTip to a stationary place out of the way, I think it should eliminate that issue. But it's not an issue once you stop moving the mouse. It correctly identifies whether or not one of the colors is found within the box. I changed the box color to Fuchsia rather than Red for the same reason. It would find the red box within the search area as it's moving, which matches one of the colors it searches for.
Ooooh I see, I thought that ErrorLevel was enough, but I needed a If block outside of the Loop, Parse, and a switch. :facepalm: And yes you are correct about the tool tip triggering the script when moving the mouse down. I think this is because it is running a bit laggy due to the GUI and ToolTip being out of sync. But I got that fixed now, and the ToolTip was more for just testing the scrip so I can do without it. This loop is exactly what I was looking for.

Code: Select all

Loop,
{	Loop, Parse, LFColors, |
	{	GoSub, Box
		PixelSearch, X, Y, X1, Y1, X2, Y2, A_LoopField, 0, RGB Fast
		If (!ErrorLevel)
		{	If (!FoundColor)
			(FoundColor := !FoundColor)
	}	} If (FoundColor)
	{	(FoundColor := !FoundColor)
		FrameColor := "Lime"
	} Else,
	{	FrameColor := "Red"
}	} Return
Thank you very much for affording me every opportunity to figure it out on my own, but I am glad you showed me the way.
And thank you too SOTE for giving me the inspiration to pursue this little project.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot] and 158 guests