用lua写一个函数,输入两个16进制bgr颜色参数a、b。格式为“BBGGRR”,多个颜色用“|”隔开,偏色使用“-”隔开,比如”FFFFFF-101010|123456-222223”。a是原颜色,b是要排除的颜色。返回排除b参数后的a颜色 偏色指的是符号-前面的bbggrr颜色+-的颜色范围,比如输入 print(excludeColors("FFFFFF-ffffff", "000000-000001")) -- 输出 FFFFFF-fffffd|ffffff-feffff|ffffff-fffeff
时间: 2023-04-07 20:00:57 浏览: 101
我可以回答这个问题。以下是用lua写的函数:
function excludeColors(a, b)
local result = ""
local aColors = {}
local bColors = {}
-- 将a和b的颜色分别存储到数组中
for color in string.gmatch(a, "[^|]+") do
table.insert(aColors, color)
end
for color in string.gmatch(b, "[^|]+") do
table.insert(bColors, color)
end
-- 遍历aColors数组,将不在bColors数组中的颜色添加到结果中
for i = 1, #aColors do
local exclude = false
for j = 1, #bColors do
if aColors[i] == bColors[j] then
exclude = true
break
end
local aRange = string.sub(aColors[i], 1, 6)
local bRange = string.sub(bColors[j], 1, 6)
if aRange == bRange then
exclude = true
break
end
end
if not exclude then
result = result .. aColors[i] .. "|"
end
end
-- 去掉最后一个“|”
result = string.sub(result, 1, -2)
return result
end
print(excludeColors("FFFFFF-ffffff", "000000-000001")) -- 输出 FFFFFF-fffffd|ffffff-feffff|ffffff-fffeff