Fandom Developers Wiki

See Global Lua Modules/PortableNavbox.


-- <nowiki>
local p = {}

local yesno = require('Dev:Yesno')
local title = mw.title.getCurrentTitle()

local function tohex(color)
	color = string.lower(color)
	color = string.gsub(color, '^%#', '')
	local hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}
	local red, green, blue
	if string.find(color, 'rgb') then
		red, green, blue = string.match(color, '%(%s*(%d+)%s*%,%s*(%d+)%s*%,%s*(%d+)')
	elseif string.find(color, 'hsl') then -- https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB
		local hue, sat, light = string.match(color, '%(%s*(%d+)[%s%,]+(%d+)%%[%s%,]+(%d+)%%')
		hue = hue/60
		sat = sat/100
		light = light/100
		local chroma = (1 - math.abs(2*light - 1))*sat
		local x = chroma*(1 - math.abs(math.fmod(hue,2) - 1))
		local r1, g1, b1
		if hue <1 then
			r1 = chroma
			g1 = x
			b1 = 0
		elseif hue <2 then
			r1 = x
			g1 = chroma
			b1 = 0
		elseif hue <3 then
			r1 = 0
			g1 = chroma
			b1 = x
		elseif hue <4 then
			r1 = 0
			g1 = x
			b1 = chroma
		elseif hue <5 then
			r1 = x
			g1 = 0
			b1 = chroma
		else
			r1 = chroma
			g1 = 0
			b1 = x
		end
		local m = light - chroma/2
		red = math.floor((r1 + m)*255 +0.5)
		green = math.floor((g1 + m)*255 +0.5)
		blue = math.floor((b1 + m)*255 +0.5) 
	end
	if red then
		red = red/16
		green = green/16
		blue = blue/16
		return hex[math.floor(red) +1] .. hex[math.floor(math.fmod(red,1)*16) +1] .. hex[math.floor(green) +1] .. hex[math.floor(math.fmod(green,1)*16) +1] .. hex[math.floor(blue) +1] .. hex[math.floor(math.fmod(blue,1)*16) +1]
	end
	if string.len(color) == 4 then
		color = string.sub(color,1,3)
	elseif string.len(color) == 8 then
		color = string.sub(color,1,6)
	end
	return color
end

function p.main(frame)
    local args = frame:getParent().args
    return p._main(args)
end

function p._main(args)
    local infobox = mw.html.create('infobox')
    infobox
    	:attr('theme', args.theme or 'navbox')
    if args.type then
    	infobox
    		:attr('type', args.type)
    end
    if args['header-background'] then
    	infobox
    		:attr('accent-color-default', tohex(args['header-background']))
    end
    if args['header-text'] then
    	infobox
    		:attr('accent-color-text-default', tohex(args['header-text']))
    end
    if yesno(args.stacked, false) then
    	infobox
    		:attr('layout', 'stacked')
    end
    local group = infobox:tag('group')
    if args.collapse ~= nil then
		if yesno(args.collapse, true) then --Collapse true: close
			group
				:attr('collapse', 'closed')
    	else --Collapse false: open
        	group
				:attr('collapse', 'open')
        end
    end
    if args[1] then
    	local header = group:tag('header')
    	header
    		:wikitext(mw.text.trim(args[1]))
    end
    local i = 2 -- i is where the parameters start
    while args[i] and args[i+1] do
    	local data = group:tag('data')
    	local label = data:tag('label')
    	label
    		:wikitext(mw.text.trim(args[i]))
    	local r = mw.ustring.gsub(mw.text.trim(args[i+1]), table.concat({' ', args.separator or '!', ' '}), '&#32;•&#160;')
    	local default = data:tag('default')
    	default
    		:wikitext(mw.text.trim(r))
    	i = i + 2
    end
    if
        title.namespace ~= 829 or
        title.subpageText ~= 'testcases'
    then
        infobox = mw.getCurrentFrame():preprocess(tostring(infobox))
    end
    return infobox
end

return p
-- </nowiki>