working with color and image

Ask gaming related questions (AHK v1.1 and older)
Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

working with color and image

13 Jun 2021, 16:11

I'm trying to create a script that looks first for a color at a certain coordinate.


first coordinate:
PixelGetColor, LifeBar, 1289, 143, RGB
firstcolor = 0xDB4F4F

if the color of the first specified coordinate is not found it will automatically press the key (insert).


however if the color is found at the specified coordinate then it will go to the next line without taking any action,
and will look for the second color at a given coordinate and also an image.




second coordinate:
PixelSearch,,, 680, 25, 1000, 40, 0x8F00B7, RGB, ; blue, green, red
ImageSearch,,, 530, 578, 580, 600, C:\Users\Hernest\Desktop\name images\Manaimage.png ; Provide path to image

if the script doesn't find the second color but it finds the image
will automatically press the key (end) will wait 2 milliseconds and press the key (insert).
if the script does not find the second color and also does not find the image, it will only press the key (insert).


Can someone help me ?

this is the image link:
https://imgur.com/a/aso8O4V
User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: working with color and image

13 Jun 2021, 17:26

Code: Select all

PixelGetColor, LifeBar, 1289, 143, RGB
If (LifeBar = 0xDB4F4F) {
 PixelSearch,,, 680, 25, 1000, 40, 0x8F00B7,, Fast RGB
 If !ErrorLevel
  Return
 ImageSearch,,, 530, 578, 580, 600, %A_Desktop%\name images\Manaimage.png
 If !ErrorLevel {
  Send {End}
  SetBatchLines, -1
  DllCall("Winmm\timeBeginPeriod", "UInt", 3)
  Loop, 50
   DllCall("Sleep", "UInt", 1)
  DllCall("Winmm\timeEndPeriod", "UInt", 3)
 }
}
Send {Ins}
Return
Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: working with color and image

13 Jun 2021, 17:56

I forgot to mention something, would the verification of this script happen every 1 millisecond?
in a specific window called "Pokemon Red"
User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: working with color and image

13 Jun 2021, 18:02

I do not know what that means, but if you are using coordinates for that window, you can WinActivate the window first.
Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: working with color and image

13 Jun 2021, 18:08

I did the test and the first time he pressed the insert key, in the LifeBar verification.
then it skipped the check from both LifeBar and the others, and didn't execute the "end" or "insert" key again
I checked the coordinates and corrected them and I still had no result,
I use screen coordinates


I would like to know how I put a loop, so the script verification in the determined window can happen anytime



this color --> 0x8F00B7 should be found at the bottom of the bar that is in the image below, but I don't know if the color I found is totally correct
I'm looking for a shade of purple

image below:
https://imgur.com/a/k5ignpQ



when i position my mouse in the middle of the purple bar i got this color 0x8F00B7 as shown in the image below

image below:
https://imgur.com/a/Brc7DxD




when it finds the image, the script first presses "END" and 2 milliseconds later presses "insert"
because on the END key, I selected an item that removes the cooldrawn from the spell that is on the "INSERT" key automatically
the image the script looks for and a check to see if the spell is in cooldraw


image that demonstrates that magic is in cooldraw:
https://imgur.com/a/aso8O4V
User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: working with color and image

13 Jun 2021, 18:43

Debugging is straightforward, because you can add MsgBox lines to display the color that is found, as well as ErrorLevels for the searches; and MouseMove to the coordinates that are provided.

To add a hotkey, you can simply insert something like F3:: as the first line. Alternatively, you can enclose the script inside a Loop command with braces (no Return). If you use a loop, then insert a short Sleep at the start or end of the cycle.

To use screen coordinates, use CoordMode, Pixel at the top. See: CoordMode.
Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: working with color and image

13 Jun 2021, 18:57

I will make a simpler explanation and with images


full life bar
https://imgur.com/a/SS98U2R

half life bar
https://imgur.com/a/ijfZPDF

magic bar
https://imgur.com/a/k5ignpQ

cooldraw image of magic
https://imgur.com/a/aso8O4V


when the life bar is halfway, press the "insert" key

if the health bar is halfway, check if the cooldraw image of the spell is located at coordinates 530, 606, 560, 630,
press "END" first after 2 milliseconds press "INSERT"

the "end" key removes the cooldraw from the spell that is on the "insert" key
so it can be used again

if the magic bar is found between coordinates 935, 31, 996, 36,
will proceed to start of checks
User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: working with color and image

13 Jun 2021, 19:06

My script meets your first description, but if it does not work in your program, you can use the debugging approaches that I have described. Your testing & debugging may require a fair amount of time. You have the tools here with the commands for PixelGetColor, PixelSearch, and ImageSearch. Since your script does not work, test one search at a time, and get it working before moving to the next one. The colored bars with gradients can be tricky because they are not solid blocks-- instead contain multiple colors-- so you might need to focus on a small area.
Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: working with color and image

17 Jun 2021, 23:50

@mikeyww
I did as you said and tested it line by line, and now it seems to work correctly I just have a doubt.

when the action is executed the script automatically closes i tried to find out the reason for this action to happen but i was not successful do you know tell me the reason it closes? I would like a scan to run all the time and it never closes automatically



I edited the code a little and it looked like this:
when opening this code it executes the key and the script is terminated soon after

Code: Select all

#SingleInstance, Force
CoordMode, Pixel, Screen ; need this since you are using screen coordinates
SetKeyDelay, -1, -1
SetWinDelay, -1
loop
{
If WinActive("Pokemon Red")
{
PixelGetColor, LifeBar, 1270, 143, RGB
If (LifeBar != 0xDB4F4F) {
PixelSearch,,,, 1040, 40, 1130, 43, 0x8200B0,, Fast RGB
        If !ErrorLevel
Send, {f4}
        Return
 ImageSearch,,, 541, 614, 548, 620, %A_Desktop%\name images\Manaimage.png
 If !ErrorLevel {
  Send {numpadmult}
  SetBatchLines, -1
  DllCall("Winmm\timeBeginPeriod", "UInt", 3)
  Loop, 50
   DllCall("Sleep", "UInt", 1)
  DllCall("Winmm\timeEndPeriod", "UInt", 3)
 }
}
Send {f4}
Return
}
Sleep, 210
}
Return

this one below is the formula you gave me I just changed the coordinates. but when I click to open this script it doesn't even appear next to the clock, closing in a fraction of a second, I made a point of analyzing images and coordinates but I don't understand what the problem is

Code: Select all

PixelGetColor, LifeBar, 1289, 143, RGB
If (LifeBar = 0xDB4F4F) {
 PixelSearch,,,, 1040, 40, 1130, 43, 0x8200B0,, Fast RGB
 If !ErrorLevel
  Return
 ImageSearch,,, 541, 614, 548, 620, %A_Desktop%\name images\Manaimage.png
 If !ErrorLevel {
  Send {End}
  SetBatchLines, -1
  DllCall("Winmm\timeBeginPeriod", "UInt", 3)
  Loop, 50
   DllCall("Sleep", "UInt", 1)
  DllCall("Winmm\timeEndPeriod", "UInt", 3)
 }
}
Send {Ins}
Return
I saw a lot of tutorials on how to work with images and I took great care when cutting them and also putting them in an easy-to-find path

I even found a code where it gives the coordinates of the place where the image is found and so I'm sure the image was found in those coordinates, I took the same care when dealing with the colors.

I will put below the code I used to search for the image coordinates:

Code: Select all

1::
    imagesearch, x, y, 0, 0, 1366, 768, C:\Users\Hernest\Desktop\image search\yy\Manaimage.png
    msgbox, %x% - %y%
return

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: working with color and image

18 Jun 2021, 02:25

This line has too many blank parameters at the beginning:

Code: Select all

PixelSearch,,,, 1040, 40, 1130, 43, 0x8200B0,, Fast RGB
Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: working with color and image

18 Jun 2021, 06:03

@boiler


alright, i'm trying to use the code below but when the keys are executed the script automatically closes.
I don't know why this happens can you tell me what's wrong?
I would like him to test himself all the time


I had help from. @mikeyww for code creation so I don't know where the problem is coming from



here is the explanation of how I would like the script to work:

I'm trying to create a script that looks first for a color at a certain coordinate.

first coordinate:
PixelGetColor, LifeBar, 1289, 143, RGB
firstcolor = 0xDB4F4F

if the color of the first specified coordinate is not found it will automatically press the key (insert).

however if the color is found at the specified coordinate then it will go to the next line without taking any action,
and will look for the second color at a given coordinate and also an image.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
second coordinate:
PixelSearch,,, 680, 25, 1000, 40, 0x8F00B7, RGB, ; blue, green, red
ImageSearch,,, 530, 578, 580, 600, C:\Users\Hernest\Desktop\name images\Manaimage.png ; Provide path to image

if the script doesn't find the second color but it finds the image
will automatically press the key (end) will wait 2 milliseconds and press the key (insert).
if the script does not find the second color and also does not find the image, it will only press the key (insert).


however the script below checks only once and when it finds and presses a key it automatically closes.

Code: Select all

#SingleInstance, Force
CoordMode, Pixel, Screen ; need this since you are using screen coordinates
SetKeyDelay, -1, -1
SetWinDelay, -1
Loop
{
	If WinActive("Pokemon Red")
	{
		PixelGetColor, LifeBar, 1270, 143, RGB
		If (LifeBar != 0xDB4F4F) {
		PixelSearch,,, 1040, 40, 1130, 43, 0x8200B0,, Fast RGB
        If !ErrorLevel
		Send, {f4}
        Return
 ImageSearch,,, 541, 614, 548, 620, C:\Users\Hernest\Desktop\name images\Manaimage.png
 If !ErrorLevel {
  Send {numpadmult}
  SetBatchLines, -1
  DllCall("Winmm\timeBeginPeriod", "UInt", 3)
  Loop, 50
   DllCall("Sleep", "UInt", 1)
  DllCall("Winmm\timeEndPeriod", "UInt", 3)
 }
}
Send {f4}
Return
	}
	Sleep, 210
}
Return
User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: working with color and image

18 Jun 2021, 06:42

You are executing your code at the top level of the script. Therefore, any instance of Return will cause the script to exit.
If there is no caller to which to return, Return will do an Exit instead.
In addition, after the last line of the script (auto-execute section) has completed, the script will exit unless it is persistent.
A script is persistent if any of the following conditions are true:
At least one hotkey or hotstring has been defined in the script or created by the Hotkey command or Hotstring function, even if it is not enabled.
The keyboard hook or mouse hook is installed.
The script contains any use of Gui, even if it has not been called.
The script contains any use of OnMessage, or has called it dynamically or retrieved a reference with Func.
The Input command has been called.
The #Persistent directive is present anywhere in the script.
Although not stated here, if you have an infinite loop, then the script never finishes and so will not exit, but if a Return is reached inside the loop, then this sort of script will exit if at the top level of the script.

To skip the remaining commands in the loop but continue to the next iteration of the loop, use Continue instead of Return.
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: working with color and image

18 Jun 2021, 07:32

Another issue is that the ImageSearch line that appears immediately after the Return will never execute even if you replace the Return with a Continue. Perhaps you were expecting the Return to be conditional based on the If statement a couple lines before it, but only the Send command is conditional since you did not identify a code block using a pair of braces. So when the code reaches that If statement, it will always execute the Return no matter what the result of the condition is.
Green Astronaut
Posts: 84
Joined: 13 Dec 2020, 08:35

Re: working with color and image

18 Jun 2021, 12:15

@boiler
Could you show me how I can create something like the explanation?
or edit this in a functional way?
User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: working with color and image

18 Jun 2021, 21:25

I don't know how to reconcile your written description of what's supposed to happen and the code that follows it because there are many basic discrepancies between the two (not even worrying about the logic of the code yet, just things like coordinates and keys). You say in the description to check 1289, 143, but then the first location your script checks is 1270, 143. Then you say if that color isn't found, then it's supposed to "press the key (insert)." But the script doesn't show the Insert key anywhere. Where you talk about the "second coordinate," the search ranges in both the PixelSearch and ImageSearch in your instructions don't match anything in your script. One of them isn't even close to anything in the script.

As mikeyww suggested, try breaking it down and getting one step to work at a time. That approach will make it easier for others to provide help if you need it.

Regarding the script you did post: Pay attention to code blocks because there is more than one place where loops or if statements are apparently meant to apply to multiple lines below them, but they are not grouped by braces. Below is not an attempt to follow your description of your desired operation, but just an attempt to add code blocks where it seems you would have meant to have them, changing Return to Continue (in one place) as mentioned by mikeyww, and implementing indentation that follows the logic of the code as written. You should always indent your script so that it visually represents the logic as presented by the script. That will help your own ability to debug it should help you get someone to take the time to review it and give feedback.

Code: Select all

#SingleInstance, Force
CoordMode, Pixel, Screen ; need this since you are using screen coordinates
SetKeyDelay, -1, -1
SetWinDelay, -1
Loop {
	If WinActive("Pokemon Red") {
		PixelGetColor, LifeBar, 1270, 143, RGB
		If (LifeBar != 0xDB4F4F) {
			PixelSearch,,, 1040, 40, 1130, 43, 0x8200B0,, Fast RGB
			If !ErrorLevel {
				Send, {f4}
				Continue
			}
			ImageSearch,,, 541, 614, 548, 620, C:\Users\Hernest\Desktop\name images\Manaimage.png
			If !ErrorLevel {
				Send {numpadmult}
				SetBatchLines, -1
				DllCall("Winmm\timeBeginPeriod", "UInt", 3)
				Loop, 50 {
					DllCall("Sleep", "UInt", 1)
					DllCall("Winmm\timeEndPeriod", "UInt", 3)
				}
			}
		}
		Send {f4}
		Return
	}
	Sleep, 210
}
Return

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 57 guests