模块:DifficultyColor
刷
历
编
跳到导航
跳到搜索
此模块的文档可以在模块:DifficultyColor/doc创建
local p = {}
function p.style(frame)
local difficulty = frame.args[1] or ''
local colors = {
['极限'] = '#8b0000',
['疯狂'] = '#dc3545',
['噩梦'] = '#6f42c1',
['困难'] = '#007bff',
['普通'] = '#28a745'
}
-- 方法1:尝试无分隔符匹配
local diffs = {}
-- 先尝试直接匹配完整字符串
if colors[difficulty] then
-- 单难度
table.insert(diffs, difficulty)
else
-- 尝试分割:匹配所有已知难度词
local remaining = difficulty
while #remaining > 0 do
local matched = false
-- 检查每个难度词
for diff, _ in pairs(colors) do
if string.sub(remaining, 1, #diff) == diff then
table.insert(diffs, diff)
remaining = string.sub(remaining, #diff + 1)
matched = true
break
end
end
-- 如果没有匹配到任何难度词,退出循环
if not matched then
break
end
end
end
if #diffs == 0 then
return 'background-color: #f8f9fa; color: #000;'
elseif #diffs == 1 then
return 'background-color: ' .. colors[diffs[1]] .. '; color: white;'
else
-- 生成渐变色
local gradient = 'linear-gradient(90deg'
local step = 100 / #diffs
for i, diff in ipairs(diffs) do
local startPercent = (i-1) * step
local endPercent = i * step
gradient = gradient .. ', ' .. colors[diff] .. ' ' .. startPercent .. '%, ' .. colors[diff] .. ' ' .. endPercent .. '%'
end
gradient = gradient .. ')'
return 'background: ' .. gradient .. '; color: white;'
end
end
return p

沪公网安备 31011002002714 号