This Module is used by the Template:Color and produces color-boxes of the given names.
The available color names can be seen and expanded at Module:Color/codes, the according ids at Module:Color/ids.
local p = {}
function p.colors( f )
local args = f:getParent().args
local colorCodes = mw.loadData( 'Module:Color/codes' )
local colorIds = mw.loadData( 'Module:Color/ids' )
local colorBlocks = {}
local size = 20
local colorNameLower = ''
local colorId = ''
--This slices one parameter into a variable number of parameters with a comma as a delimiter.
--This allows the list to be called into another Template as its own parameter.
local M_SLICES = {}
for part in string.gmatch((args[1])..',', "([^,]*),") do
table.insert(M_SLICES,part:match "^%s*(.-)%s*$")
end
local colorIdsOrdered={}
for _, colorNameInput in ipairs(M_SLICES) do
colorNameLower = colorNameInput:gsub('%W',''):lower()
colorId = 'No ID'
if colorIds[colorNameLower] ~= nil then
colorId = colorIds[colorNameLower]
end
if not p.isIdInTable(colorIdsOrdered, colorId) and colorCodes[colorNameLower] ~= nil then
table.insert(colorIdsOrdered, {colorId, colorNameLower, colorNameInput})
end
end
table.sort(colorIdsOrdered, function(a,b) return tonumber(a[1]) ~= nil and tonumber(a[1]) < tonumber(b[1]) end)
-- colorInfo[1]: color Id
-- colorInfo[2]: color name lower case, as used in the lookup tables
-- colorInfo[3]: color name as entered in the template call
for _, colorInfo in ipairs(colorIdsOrdered) do
table.insert(colorBlocks,'<div class="color-square" style="height:'..size..'px; width:'..size..'px; background:'..colorCodes[colorInfo[2]]..';" title="'..colorInfo[3]..' ('..colorInfo[1]..')"><span>'..colorInfo[1]..'</span></div>')
end
return '<div class="color-container">'..table.concat(colorBlocks)..'</div>'
end
function p.isIdInTable(tab, id)
for _, colorInfo in ipairs(tab) do
if colorInfo[1] == id then return true end
end
return false
end
return p