(order colors by id and display id) |
(fix?) |
||
Line 17: | Line 17: | ||
local colorIdsOrdered={} |
local colorIdsOrdered={} |
||
− | for colorNameInput in |
+ | for _, colorNameInput in ipairs(M_SLICES) do |
colorNameLower = colorNameInput:gsub('%W',''):lower() |
colorNameLower = colorNameInput:gsub('%W',''):lower() |
||
Revision as of 21:50, 20 October 2021
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 colorCodes[colorNameLower] ~= nil then
table.insert(colorIdsOrdered, {colorId, colorNameLower, colorNameInput})
end
end
table.sort(colorIdsOrdered)
-- colorInfo[0]: color Id
-- colorInfo[1]: color name lower case, as used in the lookup tables
-- colorInfo[2]: 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]..')">'..colorInfo[1]..'</div>')
end
return '<div class="color-container">'..table.concat(colorBlocks)..'</div>'
end
return p