打开/关闭菜单
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
登录后可编辑和发表评论。

Module:ColorBlock

来自Vocawiki

此模块的文档可以在Module:ColorBlock/doc创建

local p = {}

local var = require('Module:Variables')
local split = require('Module:String').split
local vocalistColors = require("Module:Vocalist Colors")
local Color = require('Module:Color')
local ustring = mw.ustring 

local function singer2Color(singerstr, colors)
    if not singerstr or singerstr == "" then
        return
    end

    singerstr = ustring.gsub(singerstr, '[,・,×&/]', '、') 
    local singers = split(singerstr, '、')
    for _, singer in ipairs(singers) do
        local name = ustring.match(singer, "^%s*(.-)%s*$")
        if name and name ~= "" then
            local color = vocalistColors.main({args = {name}})
            if color and color ~= "#333333" and color ~= "未知歌手" then
                colors[color] = true
            end
        end
    end
end

function p.content2ColorList(content, noprefix)
	local colors = {}
	local pattern = (noprefix and "" or "olor%s*=%s*") .. "(#%x%x%x%x%x%x)"
    for color in ustring.gmatch(content, pattern) do 
        if color then
            color = color:upper()
            if color ~= "#777777" and color ~= "#333333" then
                colors[color] = true
            end
        end
    end

    local pattern2 = "歌手%s*=%s*([^|\n]+)"
    for singerstr in ustring.gmatch(content, pattern2) do
        singerstr = ustring.match(singerstr, "^%s*(.-)%s*$")
        singer2Color(singerstr, colors)
    end
    
    local pattern3 = "#777777%s*<!%-%-(.-)%-%->"
    for singerstr in ustring.gmatch(content, pattern3) do
        singer2Color(singerstr, colors)
    end

    local colorList = {}
    for color in pairs(colors) do
        table.insert(colorList, color)
    end

    return colorList
end

function p.colorList2Text(colorList, frame)
    table.sort(colorList, function(a, b)
        local color_a = Color.create(a)
        local color_b = Color.create(b)

        if not color_a or not color_b then
            return false
        end

        local hsl_a = color_a:hsl().value 
        local hsl_b = color_b:hsl().value

        if hsl_a[1] ~= hsl_b[1] then
            return hsl_a[1] < hsl_b[1]
        end
        if hsl_a[2] ~= hsl_b[2] then 
            return hsl_a[2] > hsl_b[2]
        end
        if hsl_a[3] ~= hsl_b[3] then 
            return hsl_a[3] > hsl_b[3]
        end
        
        return false
    end)

    local colorMap = {}
    
    for _, color in ipairs(colorList) do
        local testFrame = { args = {[1] = color} }
        local singer = vocalistColors.main(testFrame)
        if singer and singer ~= "#333333" and singer ~= "未知歌手" then
            colorMap[color] = singer
        end
    end

    local mappedColors = {}
    
    local result = ""
   
    for _, color in ipairs(colorList) do
        local singer = colorMap[color]
        if singer then
            table.insert(mappedColors, "{{color_block/wl|" .. color .. "}} [[" .. singer .. "]]")
        end
    end
    
    if #mappedColors > 0 then
        result = result .. table.concat(mappedColors, " ")
    	result = result .. "\n*{{color_block/wl|#777777}} 复数/其他"
    end
    return result
end

function p.main(frame)
    local achievement = frame.args[1] or ""
    local title = mw.title.getCurrentTitle()
    local content = title and title:getContent() or ""

    local result = frame:preprocess("*部分角色官方代表色不明,为暂定颜色。<ref>注:有部分角色代表颜色取的是近似值。</ref>\n*")

    local colorList = p.content2ColorList(content)
	local colors = var.getPlain("temple-colors")..table.concat(colorList)
	colorList = p.content2ColorList(colors, true)
	var.set("temple-colors", table.concat(colorList))
	var.set("temple-colors-text", p.colorList2Text(colorList, frame))
	result = result .. var.final("temple-colors-text")

    if achievement ~= "" then
        result = result .. "\n*已达成" .. achievement .. "的歌曲以<span style=\"background: #FFFF99;\">此颜色背景</span>标出。"
    end
    result = result .. "\n*提示:悬停于代表色上可以显示演唱者。"

	return result
end

return p