Detect colors similar to another color?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
densch
Posts: 120
Joined: 29 May 2018, 15:10

Detect colors similar to another color?

17 Jun 2019, 12:14

Hello,
I am a lazy person. so I am currently playing some idle clicker kind of game and dont feel like excessively clicking.
the repeated clicking part is solved, also made it so that pressing z will toggle the auto click on and off.
but I still have to move the mouse.
i want to automate that part as well.
so as it is each enemy has a red health bar above it's head.
the sprites are pretty big.

so I wanna go the easy way, search for a red rectangle (they all look the same, for the most part)
and click a fixed distance below it.
if the enemy dies, the rectangle disappears too.
so it shoudl autoskip to the next enemy.

now just finding the rectangle is the thing.

just used made a screenshot of the game and used the script:
"
z:: ; Z hotkey.
MouseGetPos, MouseX, MouseY
PixelGetColor, color, %MouseX%, %MouseY%, Alt
MsgBox The color at the current cursor position is %color%.
return"

to look at the red rectangle.

however depending on where I click in that rectangle (on the screenshot) the color is different.

so something like "if color=0x1d5f8S"
wont do it since the rectangle consists of different shades of that light red.

so do you have any idea what I could do with pixelgetcolor to get that certain range of red pixels?
or any other good idea how to pinpoin that red rectangle?

givent that i am doing the clicking every 70 milisecond, it would be good if it was some fast algorithm or something.

Thank you in advance :-)
colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Detect colors similar to another color?

17 Jun 2019, 14:03

Split the pixel color into rgb integers. Split your check color into rgb integers. Then calculate a 3d distance using rgb as 3D coordinates. Then specify a tolerance. If the distance is less than tolerance then the pixel color is close enough to your target color.

Code: Select all

z:: ; Z hotkey.
targetColor=0x1d5f85
tolerance := 20
MouseGetPos, MouseX, MouseY
PixelGetColor, color, %MouseX%, %MouseY%, Alt RGB
MsgBox The color at the current cursor position is %color%.

;split target color into rgb
tr := format("{:d}","0x" . substr(targetColor,3,2))
tg := format("{:d}","0x" . substr(targetColor,5,2))
tb := format("{:d}","0x" . substr(targetColor,7,2))

;split pixel into rgb
pr := format("{:d}","0x" . substr(color,3,2))
pg := format("{:d}","0x" . substr(color,5,2))
pb := format("{:d}","0x" . substr(color,7,2))

;check distance
distance := sqrt((tr-pr)**2+(tg-pg)**2+(pb-tb)**2)
if(distance<tolerance)
{
	msgbox %distance%
}
return
densch
Posts: 120
Joined: 29 May 2018, 15:10

Re: Detect colors similar to another color?

17 Jun 2019, 14:55

Wow, that's really cool! Thanks!

Can you please explain to me what the different parts of

tr := format("{:d}","0x" . substr(targetColor,3,2))
actually do?
colt
Posts: 291
Joined: 04 Aug 2014, 23:12
Location: Portland Oregon

Re: Detect colors similar to another color?

17 Jun 2019, 15:19

targetColor is stored as a hex value of 0x1d5f85 or 0xRRGGBB where RR can be interpreted as an integer between 0-255
substr(targetColor,3,2) will return the red value of your rgb color that is stored in hex form. In your case it will return 1d.
The format command takes that hex value and converts it to decimal. I had to prepend the substr function with 0x to specify the input a hex value. Conversion from hex to decimal is probably unnecessary, but I find it much easier to debug when it is in decimal form.
densch
Posts: 120
Joined: 29 May 2018, 15:10

Re: Detect colors similar to another color?

20 Jun 2019, 02:46

hey, I am a bit confused right now.
I wanted to change it from a command to a function, so it can be used as needed.
I wanted it to get some color and give back the distance between the current cursor position color and the given color.
but for whatever reason ahk just wont recognize it as a function at all, with the return 5 being an "return with a parameter can only be used inside a function".
Well guess what exactls this was supposed to be , ahk?

Here's the code:
Spoiler
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Detect colors similar to another color?

20 Jun 2019, 03:37

I'm not sure if this will be of any help to you, but here is a pre-made Pixelsearch script.

https://www.autohotkey.com/boards/viewtopic.php?f=6&t=60949


I will always advocate for learning how to do it yourself, but sometimes you just want to get to the goal and not deal with doing everything in between.
densch
Posts: 120
Joined: 29 May 2018, 15:10

Re: Detect colors similar to another color?

20 Jun 2019, 05:06

wow, that is a lot more complex then I will ever need it.

I literally only want to find a red rectangles or parts of it (there's nothing else red on the screen, so searching for a red pixel (or a color very similar to it) should do the trick.
the area in which to search can be defined also quite well.

I currently just dont get why my function wont be recognized as a function. :-/
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Detect colors similar to another color?

20 Jun 2019, 05:45

Code: Select all

//check if current color is similar enough to given color
// isnt how u begin line comments in ahk
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Detect colors similar to another color?

20 Jun 2019, 05:49

You have a few errors in your code.

Code: Select all

similarcolor(color){
//check if current color is similar enough to given color    ;<--------------Not a valid comment  Use " ; "
targetColor=color    ;<----------------------------  Here you set the target color to be the word "color" when you want it to be the value in the variable color  ;  Use " := "   targetColor :=  color
tolerance := 20
MouseGetPos, MouseX, MouseY
PixelGetColor, color, %MouseX%, %MouseY%, Alt RGB
Tooltip, The color at the current cursor position is %color%.

Sleep, 200

;split target color into rgb
tr := format("{:d}","0x" . substr(targetColor,3,2))
tg := format("{:d}","0x" . substr(targetColor,5,2))
tb := format("{:d}","0x" . substr(targetColor,7,2))

;split pixel into rgb
pr := format("{:d}","0x" . substr(color,3,2))
pg := format("{:d}","0x" . substr(color,5,2))
pb := format("{:d}","0x" . substr(color,7,2))

;check distance
distance := sqrt((tr-pr)**2+(tg-pg)**2+(pb-tb)**2)
if(distance<tolerance)
{
Tooltip, %distance%
}
return 5
}
densch
Posts: 120
Joined: 29 May 2018, 15:10

Re: Detect colors similar to another color?

20 Jun 2019, 05:54

oh, okay. I was somehow under the impression since
/*
*/
does multiline comments, that // should do single line ones.


tried it, at least it does it's usual stuff again and not give me error.
now I'll be on my quest until the next issue arises :-)
densch
Posts: 120
Joined: 29 May 2018, 15:10

Re: Detect colors similar to another color?

20 Jun 2019, 06:34

okay, color detection works for now.
despite the pixelgetcolor documentation specifing bgr, it actually uses rgb.

had to use some color hex display site to verify which order of the 3 parts was the right one.
finally got the color values of the needed color too.

with tolerance=25, I really only get stuff inside the wanted red bar.

even though there is a bit of dark red on some enemy (didnt realize that until now) it wont get triggered.

so it's perfect.

now only need the pixel area search thingie working and I'm done :-)
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Detect colors similar to another color?

20 Jun 2019, 06:51

PixelGetColor, color, %MouseX%, %MouseY%, Alt RGB
:roll:
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: Detect colors similar to another color?

20 Jun 2019, 17:16

densch wrote:
17 Jun 2019, 12:14
however depending on where I click in that rectangle (on the screenshot) the color is different.

so something like "if color=0x1d5f8S"
wont do it since the rectangle consists of different shades of that light red.
hello,
Here is a ready to use function (by skrommel) that can solve this problem :superhappy: :ugeek:

Code: Select all

;by skrommel
COMPARE(color1,color2)  ; colors in hex format  "0xff8728"
{
  Loop,2
  {
    param:=A_Index
    StringTrimLeft,color%param%,color%param%,2
    Loop,3
    {
      StringLeft,c%param%%A_Index%,color%param%,2
      value:=c%param%%A_Index%
      c%param%%A_Index%=0x%value%
      StringTrimLeft,color%param%,color%param%,2
    }
  } 
  difference:=(Abs(c11-c21)+Abs(c12-c22)+Abs(c13-c23))/3
  Return difference
}
:arrow:
Here is a gui to test the function :geek:

Code: Select all

;COMPARE() colors by Skrommel
;gui test by Speedmaster

#SingleInstance force

color1:=0xFE0000
color2:=0xFF0000

gui, font, s12

gui, +resize
gui, add, text,, press T ( target color )
gui, add, listview, x10 y+10 w20 h20 border vt_lv -E0X200 0x4000 +Background%color1%
gui, add, text, yp x+10 w100 hp 0x201 border vcolorhex1, % color1
gui, add, text, x10, press S ( selected color )
gui, add, listview, x10 y+5 w20 h20 border vs_lv -E0X200 0x4000 +Background%color2%
gui, add, text, yp x+10 w100 hp 0x201 border vcolorhex2, % color2
gui, add, button, x10 gok, Compare
gui, add, text, x+10 w100 hp 0x201 border vresult, 
gui, add, text, x10 w210 h100 -border vcomment,  the closer the value is to zero, the more similar the colors are
gui, show, w300, compare colors
return

ok:
guicontrol,, result, % compare(color1, color2) 
return

~t::
MouseGetPos, MouseX, MouseY
PixelGetColor, color1, %MouseX%, %MouseY%, Alt RGB
GuiControl ,+Background%color1%, t_lv
GuiControl ,, colorhex1, % color1
guicontrol,, result,

return

~s::
MouseGetPos, MouseX, MouseY
PixelGetColor, color2, %MouseX%, %MouseY%, Alt RGB
GuiControl ,+Background%color2%, s_lv
GuiControl ,, colorhex2, % color2
guicontrol,, result,
return

guiclose: 
esc:: 
exitapp 
return



;by skrommel
COMPARE(color1,color2)  ; colors in hex format  "0xff8728"
{
  Loop,2
  {
    param:=A_Index
    StringTrimLeft,color%param%,color%param%,2
    Loop,3
    {
      StringLeft,c%param%%A_Index%,color%param%,2
      value:=c%param%%A_Index%
      c%param%%A_Index%=0x%value%
      StringTrimLeft,color%param%,color%param%,2
    }
  } 
  difference:=(Abs(c11-c21)+Abs(c12-c22)+Abs(c13-c23))/3
  Return difference
}
Cheers,

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Joey5 and 205 guests