更多操作
local p = {}
-- 计算亮度:返回 true 表示背景较亮,用黑字;false 表示背景暗,用白字 local function isLightColor(color)
local r, g, b
-- 处理 rgb(r, g, b) r, g, b = color:match("rgb%((%d+),%s*(%d+),%s*(%d+)%)") if r and g and b then r, g, b = tonumber(r), tonumber(g), tonumber(b) else -- 处理十六进制 #rrggbb local hex = color:match("#(%x%x%x%x%x%x)") if hex then r = tonumber(hex:sub(1, 2), 16) g = tonumber(hex:sub(3, 4), 16) b = tonumber(hex:sub(5, 6), 16) end end
if r and g and b then local luminance = 0.299 * r + 0.587 * g + 0.114 * b return luminance > 186 else return true -- 无法判断时默认使用黑字 end
end
function p.render(frame)
local args = frame:getParent().args local output = {}
local title = args['标题'] or '未命名条目' local color = args['颜色'] or '#e8e8e8' local useBlackText = isLightColor(color) local fontColor = useBlackText and '#000' or '#fff'
table.insert(output, '{| class="infobox" style="width:280px; font-size:90%; text-align:left; background:#f9f9f9; border:1px solid #aaa; border-collapse:collapse;"')
table.insert(output, string.format('|-\n! colspan="2" style="text-align:center; font-size:130%%; font-weight:bold; background:%s; color:%s; padding:8px;" | %s', color, fontColor, title))
if args['图片'] and args['图片'] ~= then table.insert(output, '|-\n| colspan="2" style="text-align:center;" | [[File:' .. args['图片'] .. '|center|200px]]') end
for k, v in pairs(args) do if k ~= '标题' and k ~= '图片' and k ~= '颜色' and v ~= then table.insert(output, '|-\n| style="width:40%; font-weight:bold;" | ' .. k .. ' || ' .. v) end end
table.insert(output, '|}') return table.concat(output, '\n')
end
return p