如何在指定窗口中搜索多种颜色?
AutoHotkey多颜色像素搜索解决方案
原生的PixelSearch命令不支持一次性传入多个颜色,但你不需要重复编写多次调用逻辑,可以通过自定义函数实现简洁的多颜色搜索,覆盖"或"(任一颜色存在)和"与"(所有颜色都存在)两种场景:
场景1:搜索任一目标颜色(或逻辑)
只要区域内存在其中一种颜色就触发操作,用以下自定义函数封装:
F12:: { ; 定义需要搜索的颜色数组 targetColors := [0xFFFFFF, 0x000000] ; 调用多颜色搜索函数,参数与PixelSearch一致,最后传入颜色数组 if MultiPixelSearch(&Px, &Py, 41, 23, 686, 171, targetColors, 1) { MsgBox("找到匹配颜色,坐标:(" Px ", " Py ")") } else { MsgBox("未找到任何目标颜色") } } ; 自定义多颜色搜索函数:找到任一颜色即返回成功 MultiPixelSearch(&outputX, &outputY, x1, y1, x2, y2, colorArray, variation := 0) { for _, color in colorArray { if PixelSearch(&outputX, &outputY, x1, y1, x2, y2, color, variation) { return true } } return false }
场景2:确认所有目标颜色都存在(与逻辑)
需要区域内同时存在所有指定颜色,用以下函数:
F12:: { targetColors := [0xFFFFFF, 0x000000] if AllColorsExist(41, 23, 686, 171, targetColors, 1) { MsgBox("两种颜色都在区域内找到") } else { MsgBox("至少有一种颜色未找到") } } ; 自定义函数:检查所有颜色都存在于区域内 AllColorsExist(x1, y1, x2, y2, colorArray, variation := 0) { for _, color in colorArray { if !PixelSearch(&Px, &Py, x1, y1, x2, y2, color, variation) { return false } } return true }
这样既避免了重复编写PixelSearch调用,也能实现你想要的简洁代码结构。
内容的提问来源于stack exchange,提问作者yfjuu 0
相关产品推荐
相关产品推荐

