函数:image.getColorRGB 获取图片对象某点 RGB 颜色

函数名称:获取图片对象指定坐标点的颜色

函数功能: 获取图片对象指定坐标点的颜色

函数方法

r,g,b = image.getColor(img,x,y)

参数 类型 必填 说明
img userdata 需要操作的图片对象
x number 将获取颜色值的屏幕横坐标
y number 将获取颜色值的屏幕纵坐标
返回值 类型 说明
r,g,b number 该点颜色的 R,G,B 值,十进制颜色值

函数用例

--当前界面截图并获取 20,20 的颜色值
img = image.snapshot()
if img  then
    r,g,b = image.getColorRGB(img,20,20)
    --转成十六进制
    toast(string.format("%#x",r)..string.format("%x",g)..string.format("%x",b),5000)
else
    dialog("截屏失败,请重新给截屏权限",5000)
end

封装一个单点模糊比色函数

function isColor(x,y,c,s)
    img = image.snapshot()
    if img  then
        local fl,abs = math.floor,math.abs
        s = fl(0xff*(100-s)*0.01)
        local r,g,b = fl(c/0x10000),fl(c%0x10000/0x100),fl(c%0x100)
        local rr,gg,bb = image.getColorRGB(img,x,y)
        if abs(r-rr)<s and abs(g-gg)<s and abs(b-bb)<s then
            return true
        end
     else
        toast("截屏失败",5000)
    end

end
if isColor(986, 179, 0xfefdff,90) then --90 为模糊值,值越大要求的精确度越高
    dialog("找到颜色",5000)
else
    dialog("未找到颜色",5000)
end

多点模糊比色 在实际游戏脚本制作中,很多界面单靠 1 个点不容易进行准确的判断,这里封装一个配合 TABLE 使用的多点模糊比色函数来实现精确判断:

function multiColor(array,s)
    img = image.snapshot()
    if img  then
        s = math.floor(0xff*(100-s)*0.01)
        for var = 1, #array do
            local lr,lg,lb = image.getColorRGB(img,array[var][1],array[var][2])
            local r = math.floor(array[var][3]/0x10000)
            local g = math.floor(array[var][3]%0x10000/0x100)
            local b = math.floor(array[var][3]%0x100)
            if math.abs(lr-r) > s or math.abs(lg-g) > s or math.abs(lb-b) > s then
                return false
            end
        end

        return true
    else
        toast("截屏失败",5000)
    end
end
g_t_Table = {{986, 179, 0xfefdff},{933, 184, 0x3835de},{719, 184, 0x5751e7},{933, 118, 0x4139e6}}
if multiColor(g_t_Table,95) then
    toast("找到颜色",5000)
else
    dialog("未找到颜色",5000)
end

注意事项

  • 目前积木编程函数和触动精灵函数不通用,请仔细查看本手册,此手册中函数仅支持积木编程,不支持触动精灵,同理请勿将触动精灵函数在积木编程运行。

  • 使用 image.snapshot 函数需要给积木编程截图权限。

  • 显示截屏失败详见 https://www.touchsprite.com/docs/9499。

Copyright 北京帮你玩科技有限公司 2022 all right reserved,powered by Gitbook该文章修订时间: 2023-08-22 17:22:19

results matching ""

    No results matching ""