Тема: AHK: Параметры команды ImageSearch
Немного загнался по мотивам этой темы, ибо было непонятно. Там требовался параметр variation:
*n (variation): Specify for n a number between 0 and 255 (inclusive) to indicate the allowed number of shades of variation in either direction for the intensity of the red, green, and blue components of each pixel's color. For example, *2 would allow two shades of variation. This parameter is helpful if the coloring of the image varies slightly or if ImageFile uses a format such as GIF or JPG that does not accurately represent an image on the screen. If you specify 255 shades of variation, all colors will match. The default is 0 shades.
Так как совсем от балды брать этот параметр наверное не правильно, а также утомительно, решил (для себя) сделать код его находящий:
Image = Безымянный.JPG
Search = Search.png
#SingleInstance Force
#NoEnv
ListLines Off
SetBatchLines -1
SetWorkingDir %A_ScriptDir%
Image = Безымянный.JPG
Search = Search.png
Gui -DPIScale -Caption
Gui Margin, 0, 0
Gui Add, Picture, vPicture, % Image
Gui Show, x0 y0
1::
n := GetVariation(0, 0, 1152, 748, 14, Search, 22, 13, "27429F")
Sleep 500
For k, v in SeveralSearch(0, 0, 1152, 748, "*" n " " Search)
{
Click % v[1]+11 " " v[2]+6
Sleep 100
}
Return
GetVariation(x1, y1, x2, y2, Overlap, IsSearch, sW, sH, Bkg, n := 0) {
Found := 0
Gui 2: -Caption +AlwaysOnTop +E0x08000000 +E0x20 +Lastfound -DPIScale
DllCall("Dwmapi\DwmIsCompositionEnabled", "UInt*", isEnabled)
If isEnabled
{
Gui 2: Color, 000000
WinSet, TransColor, 000000 250
}
Gui 2: Show, NA x0 y0 w%A_ScreenWidth% h%A_ScreenHeight%
Loop
{
ToolTip % n "`n" Found,1,1
ImageSearch, mX, mY, x1, y1, x2, y2, *%n% %IsSearch%
If (n = 255)
Break
If (ErrorLevel && ++n)
Continue
Gui 2: Add, Progress, % "x" mX " y" mY " w" sW " h" sH " BackgroundD" Bkg
nRes := n, ++Found
ToolTip % n "`n" Found,1,1
If (Found = Overlap)
Break
Sleep -1
}
MsgBox, 262144, , % "Variation: " nRes
Gui 2: Destroy
ToolTip
Return nRes
}
2::
For k, v in SeveralSearch(0, 0, 1152, 748, "*49 " Search)
{
Click % v[1]+11 " " v[2]+6
Sleep 100
}
Return
SeveralSearch(x1, y1, x2, y2, IsSearch) {
StartTime := A_TickCount
xS := x1, prY := y2, ErrorCount := i := 0, coords := []
Loop
{
ImageSearch, mX, mY, xS, y1, x2, y2, %IsSearch%
If (ErrorLevel && ++ErrorCount > 1)
Break
If (mY = "" || mY > prY)
xS := x1, y1 := prY+1, prY := y2
Else
xS := mX+1, ErrorCount := 0, prY := mY, coords[++i] := [mX, mY]
}
MsgBox % "Найдено: " coords.MaxIndex()
. "`nЗа: " A_TickCount - StartTime "msec"
Return coords
}
Esc::
GuiEscape:
GuiClose:
ExitApp
Есть конечно простейшее решение, но у меня тут вся заморочка с тем, что на одной картинке нужно найти сразу несколько совпадений, что на первый взгляд мне показалось очень просто.
Кстати, так как вопрос нахождения нескольких совпадений поднимался как минимум не раз, то это вроде как решение.
Вспомнив также недавнюю тему, понял, что совет использовать там параметр variation, не совсем верный. Вырезать кусок из иконки где нет прозрачных областей, также не выдерживает критики. Есть мнение, что как раз для иконок предназначен TransN:
*TransN: This option makes it easier to find a match by specifying one color within the image that will match any color on the screen. It is most commonly used to find PNG, GIF, and TIF files that have some transparent areas (however, icons do not need this option because their transparency is automatically supported). For GIF files, *TransWhite might be most likely to work. For PNG and TIF files, *TransBlack might be best. Otherwise, specify for N some other color name or RGB value (see the color chart for guidance, or use PixelGetColor in its RGB mode). Examples: *TransBlack, *TransFFFFAA, *Trans0xFFFFAA.
И возник вопрос, а как извлечь из иконки цвет, который задан в ней как прозрачный?
Погуглив, понял, что есть такое дело, но решения ненашёл\непонял.
Win10x64 AhkSpy, Hotkey, ClockGui

