Difference between revisions of "Module:ComplForColorModules"
Jump to navigation
Jump to search
blackwiki>Jmarchn |
blackwiki>Jmarchn m |
||
| Line 2: | Line 2: | ||
local SA = require "Module:SimpleArgs" | local SA = require "Module:SimpleArgs" | ||
| + | local MC = require "Module:Multicol" | ||
local TNTT = require "Module:TNTTools" | local TNTT = require "Module:TNTTools" | ||
| Line 12: | Line 13: | ||
local function I18nStr (S, ...) | local function I18nStr (S, ...) | ||
| − | return TNTT. | + | return TNTT.GetMsgP (I18n, S, {...}) |
end | end | ||
| Line 80: | Line 81: | ||
WriteColor = Color..'ff' --Alpha channel | WriteColor = Color..'ff' --Alpha channel | ||
end | end | ||
| − | local TheBox = '<span style="background-color:'..Color..'; border:1px solid gray;" title="'..TNTT. | + | local TheBox = '<span style="background-color:'..Color..'; border:1px solid gray;" title="'..TNTT.GetStrP(I18n,'Color')..': '..SColor(Color)..'"> </span>' |
if WriteColor ~='' then | if WriteColor ~='' then | ||
TheBox = TheBox..' '..WriteColor..' ' | TheBox = TheBox..' '..WriteColor..' ' | ||
| Line 107: | Line 108: | ||
SOutlineColor = '|outline='..OutlineColor | SOutlineColor = '|outline='..OutlineColor | ||
end | end | ||
| − | return '{{'..TNTT. | + | return '{{'..TNTT.GetStrP(I18n,'Legend')..'|'..Color..'|'..Text..SOutlineColor..'}}' |
end | end | ||
| − | function p.LegendText (AColors, Labels, NLabels, | + | function p.LegendText (AColors, Labels, NLabels, NCols, IsTemplate, OutlineColor) |
| − | |||
| − | |||
| − | |||
| − | |||
function NoDataFound (i) | function NoDataFound (i) | ||
if string.sub(Labels[i],1,2) == '--' then | if string.sub(Labels[i],1,2) == '--' then | ||
| Line 142: | Line 139: | ||
table.insert(Show, p.LegendColor(AColors[i],Text,'')) | table.insert(Show, p.LegendColor(AColors[i],Text,'')) | ||
end | end | ||
| − | SShow = | + | local SShow = MC.MultiCol (NCols, Show, table.getn(AColors), 1, IsTemplate) |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
for i=1, table.getn(AColors) do | for i=1, table.getn(AColors) do | ||
if WithLabels then Text = Labels[i] end | if WithLabels then Text = Labels[i] end | ||
table.insert(Codes, ' '..LegendCode(AColors[i],Text,OutlineColor)) | table.insert(Codes, ' '..LegendCode(AColors[i],Text,OutlineColor)) | ||
end | end | ||
| − | local SCodes = | + | local SCodes = MC.MultiCol (NCols, Codes, table.getn(AColors), 2, IsTemplate) |
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
return SShow..'\n'..SCodes | return SShow..'\n'..SCodes | ||
end | end | ||
return p | return p | ||
Revision as of 12:05, 12 November 2019
This module is used by Module:BrewerColors and Module:ChartColors.
To translate or review the translation of the module to your language, edit carefully Data:I18n/ComplForColorModules.tab.
local p = {}
local SA = require "Module:SimpleArgs"
local MC = require "Module:Multicol"
local TNTT = require "Module:TNTTools"
local I18n = 'ComplForColorModules'
--local MoreOneNoData = "Found 2 legends of \"Value not assigned\" or \"Data not available\""
--local FoundNLegendsExpectedN = "Found $1 legends, expected $2"
--local Color = "Color"
--local Legend = "legend"
local function I18nStr (S, ...)
return TNTT.GetMsgP (I18n, 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
function p.ColorNameInv (args)
local S = SA.RStr_Par (args, 1)
local ColorName, IsInv, Params = p.ColorNameInvFromS0 (S)
return ColorName, IsInv
end
function p.GetLabels(Args, NArgs, N, Pos)
local Labels, NLabels = SA.StrTab_NPar (Args, NArgs, Pos)
local OutlineColor = Args['outline'] or ''
if (NLabels ~= 0) and (NLabels ~= N) then
function NoDataFound (i)
if string.sub(Labels[i],1,2) == '--' then
return 1
else
return 0
end
end
local NoDataFoundCount = NoDataFound (1)
NoDataFoundCount = NoDataFoundCount + NoDataFound (NLabels)
if NoDataFoundCount == 2 then
error (I18nStr ('MoreOneNoData'))
else
N = N + NoDataFoundCount
end
if (NLabels ~= 0) and (NLabels ~= N) then
error (I18nStr ('FoundNLegendsExpectedN', tostring(NLabels), tostring(N)))
else
return Labels, NLabels, OutlineColor
end
else
return Labels, NLabels, OutlineColor
end
end
function SColor (Color)
if string.sub(Color,1,1) == '#' then --the usual
return string.sub(Color,2,100)
else
return Color
end
end
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="'..TNTT.GetStrP(I18n,'Color')..': '..SColor(Color)..'"> </span>'
if WriteColor ~='' then
TheBox = TheBox..' '..WriteColor..' '
end
return TheBox
end
function p.TextWithTooltip (Text, Tooltip)
if Tooltip ~= '' then
return '<span title="'..Tooltip..'">'..Text..'</span>'
else
return Text
end
end
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
function LegendCode(Color, Text, OutlineColor)
local SOutlineColor = ''
if OutlineColor ~= '' then
SOutlineColor = '|outline='..OutlineColor
end
return '{{'..TNTT.GetStrP(I18n,'Legend')..'|'..Color..'|'..Text..SOutlineColor..'}}'
end
function p.LegendText (AColors, Labels, NLabels, NCols, IsTemplate, OutlineColor)
function NoDataFound (i)
if string.sub(Labels[i],1,2) == '--' then
return true
else
return false
end
end
local Show, Codes = {}, {}
local Text = ''
local WithLabels = NLabels > 0
if WithLabels then
function DelBegin (i)
Labels[i] = string.sub(Labels[i], 3, 1000)
end
local Gray = '#b3b3b3' -- A 40% gray
if NoDataFound(1) then
table.insert(AColors, 1, Gray)
DelBegin(1)
elseif NoDataFound(NLabels) then
table.insert(AColors, Gray)
DelBegin(NLabels)
end
end
for i=1, table.getn(AColors) do
if WithLabels then Text = Labels[i] end
table.insert(Show, p.LegendColor(AColors[i],Text,''))
end
local SShow = MC.MultiCol (NCols, Show, table.getn(AColors), 1, IsTemplate)
for i=1, table.getn(AColors) do
if WithLabels then Text = Labels[i] end
table.insert(Codes, ' '..LegendCode(AColors[i],Text,OutlineColor))
end
local SCodes = MC.MultiCol (NCols, Codes, table.getn(AColors), 2, IsTemplate)
return SShow..'\n'..SCodes
end
return p