Mòdul:ComplForColorModules

De la Viquipèdia, l'enciclopèdia lliure
Icona de documentació de mòdul Documentació del mòdul [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]

Mòdul ComplForColorModules (codi · ús · discussió · proves · tests · casos prova | subpàgines · enllaços)

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Aquest mòdul l'utilitza Module:BrewerColors i Module:ChartColors.

local p = {}

local SA = require "Module:SimpleArgs"
local MC = require "Module:XCols"

local i18n = {
	["Arg"] = {
		["Palette"] =	"palette",
		["N_Colors"] =	"color_n",
	},
	["Label"] = {
		["Color"] =		"Color",
		["Legend"] =	"Legend",
	},
	["Error"] = {
		["MoreOneNoData"] =			"Found 2 legends of \"Value not assigned\" or \"Data not available\"",
		["FoundNLegendsExpectedN"] = "Found $1 legends, expected $2",
	},
}
i18n = SA.loadI18n ('ComplForColorModules', i18n)

local function I18nArgLabel (S)
	return i18n["Arg"][S]
end

local function I18nLabel (S)
	return i18n["Label"][S]
end

local function I18nError (S, ...)
	return SA.I18nErrorParams (i18n["Error"][S], ...)
end

function p.ColorNameInvFromS0 (S)
	local IsInv = false
	local ColorName = ''
	local Params = {}
	if S ~= '' then
		for w in S:gmatch("([^_]+)") do 
	    	table.insert(Params, w)
		end
		ColorName = Params[1]
	end	
	local PN = table.getn(Params)
	if (PN == 2) and (Params[2]=='i') then
		IsInv = true
	end	
	return ColorName, IsInv, Params
end --ColorNameInvFromS0

function p.ColorNameInv0 (args, WithName)
	local label
	if WithName then
		label = I18nArgLabel('Palette')
	else
		label = 1
	end	
	return SA.RStr_Par (args, label)
end --ColorNameInv0

function p.ColorNameInv (args, WithName)
	local S = p.ColorNameInv0 (args, WithName)
	local ColorName, IsInv, Params = p.ColorNameInvFromS0 (S)
	return ColorName, IsInv			    	
end	--ColorNameInv

function SColor (Color)
	if string.sub(Color,1,1) == '#' then --the usual
		return string.sub(Color,2,100)
	else
		return Color
	end	
end	--SColor

function p.Box(Color,WriteColor)
	if WriteColor == '-' then 
		WriteColor = Color
	elseif WriteColor == 'a' then 	
		WriteColor = Color..'ff' --Alpha channel
	end	
	local TheBox = '<span style="background-color:'..Color..'; border:1px solid gray;" title="'..I18nLabel('Color')..': '..SColor(Color)..'">&nbsp;&nbsp;&nbsp;</span>'	
	if WriteColor ~='' then 
		TheBox = TheBox..'&nbsp;'..WriteColor..' &nbsp;'
	end	
	return TheBox
end	--Box

function p.TextWithTooltip (Text, Tooltip)
	if Tooltip ~= '' then
		return '<span title="'..Tooltip..'">'..Text..'</span>'
	else
		return Text
	end	
end	--TextWithTooltip

function p.LegendColor(Color, Text, Tooltip)
	if Text == '' then
		Text = SColor(Color)
	end	
	return '<li style="list-style-type: none; list-style-image: none;"><span style="border:1px solid gray; background:'..Color..'; margin-right:0.3em; vertical-align: middle; webkit-print-color-adjust:exact; color-adjust:exact; display:inline-block; width:1.05em; height:1em;"></span>'..p.TextWithTooltip(Text,Tooltip)..'</li>'
end	--LegendColor

function LegendCode(Color, Text, OutlineColor)
	local SOutlineColor = ''
	if OutlineColor ~= '' then
		SOutlineColor = '|outline='..OutlineColor
	end	
	return '{{'..I18nLabel('Legend')..'|'..Color..'|'..Text..SOutlineColor..'}}'
end	--LegendCode

function p.N_Colors (args)
	return SA.RPosInt_Par (args, I18nArgLabel('N_Colors'))
end	

function p.LegendText (args, AColors, n_colors)
	local Show, Codes = {}, {}
	local Text = ''
	local splited, NLines, Lines, NCols, width, same_width, sep_cols, align, vertical_align, Header, Footer = MC.MainVals (args, false)
    local OutlineColor = args['outline'] or ''
    SA.CheckParams (args, SA.JoinI18nTables ({MC.i18n["Arg"],i18n["Arg"]}))
	function NoDataFound (i)
		if string.sub(Lines[i],1,2) == '--' then
			return 1
		else
			return 0
		end	
	end	
    if (NLines ~= 0) and (NLines ~= n_colors) then
    	local NoDataFoundCount = NoDataFound (1)
    	NoDataFoundCount = NoDataFoundCount + NoDataFound (NLines)
    	if NoDataFoundCount == 2 then
	    	error (I18nError ('MoreOneNoData'))
    	else
    		n_colors = n_colors + NoDataFoundCount
    	end	
		if (NLines ~= 0) and (NLines ~= n_colors) then
	    	error (I18nError ('FoundNLegendsExpectedN', tostring(NLines), tostring(n_colors)))
	    end	
    end	
	local WithLabels = NLines > 0
	if WithLabels then
		function DelBegin (i)
			Lines[i] = string.sub(Lines[i], 3, 1000)
		end	
		local Gray = '#b3b3b3' -- A 40% gray
		if NoDataFound(1) == 1 then
			table.insert(AColors, 1, Gray) 
			DelBegin(1)
		elseif NoDataFound(NLines )== 1 then
			table.insert(AColors, Gray) 
			DelBegin(NLines)
		end
	end
	for i=1, table.getn(AColors) do
		if WithLabels then 
			Text = Lines[i] 
		end
		table.insert(Show, p.LegendColor(AColors[i],Text,''))
	end	
	return MC.MultiColX (false, width, same_width, sep_cols, NCols, Show, align, vertical_align, Header, Footer)
end	--LegendText

return p