Difference between revisions of "Module:HalidomMap"
Jump to navigation
Jump to search
imported>Elaeagnifolia |
>Mornsta-gpuser |
||
| (106 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
| − | function split( | + | --explode-like split function taken from LUA documentation |
| − | local | + | function split(d,p) |
| − | + | local t, ll | |
| − | return | + | t={} |
| + | ll=0 | ||
| + | if(#p == 1) then | ||
| + | return {p} | ||
| + | end | ||
| + | while true do | ||
| + | l = string.find(p, d, ll, true) -- find the next d in the string | ||
| + | if l ~= nil then -- if "not not" found then.. | ||
| + | table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array. | ||
| + | ll = l + 1 -- save just after where we found it for searching next time. | ||
| + | else | ||
| + | table.insert(t, string.sub(p,ll)) -- Save what's left in our array. | ||
| + | break -- Break at end, as it should be, according to the lua manual. | ||
| + | end | ||
| + | end | ||
| + | return t | ||
end | end | ||
| + | --renders the table of different cell colors based on given arguments | ||
p._halidomMap = function (args) | p._halidomMap = function (args) | ||
| − | local tbl = mw.html.create('table'):css('text-align', 'center') | + | -- Create the table |
| + | local tbl = mw.html.create('table'):css('text-align', 'center'):css('table-layout','fixed'):css('border-collapse', 'collapse') | ||
| + | -- Loop through all the template arguments ("rows of the halidom map") | ||
for idx, row in ipairs(args) do | for idx, row in ipairs(args) do | ||
| + | -- Create the table row | ||
local tr = tbl:tag('tr') | local tr = tbl:tag('tr') | ||
| − | local cellValues = split( | + | |
| + | -- Split the cell values | ||
| + | local cellValues = split(",", row) | ||
| + | |||
| + | -- Loop through all cell values in the row | ||
for idx2, cellValue in ipairs(cellValues) do | for idx2, cellValue in ipairs(cellValues) do | ||
| + | -- Create the table cell with the background color based on the cell value | ||
local td = tr:tag('td') | local td = tr:tag('td') | ||
| − | :css('width', ' | + | :css('width', '7px') |
| − | :css('height', ' | + | :css('height', '7px') |
| + | --:css('border', '1px solid #c0c0c0') | ||
:css('background-color', getCellColor(cellValue)) | :css('background-color', getCellColor(cellValue)) | ||
end | end | ||
| Line 25: | Line 50: | ||
end | end | ||
| − | --- Function that converts | + | --- Function that converts a given value to a color |
| − | -- @param | + | -- @param value |
| − | -- @return | + | -- @return Hex color |
function getCellColor(value) | function getCellColor(value) | ||
local halidomValueToColor = { | local halidomValueToColor = { | ||
| − | ["255"] = "# | + | ["255"] = "#466999", |
["0"] = "#000", | ["0"] = "#000", | ||
["1"] = "red", | ["1"] = "red", | ||
| − | ["2"] = " | + | ["2"] = "#f38500", |
| − | ["3"] = " | + | ["3"] = "#e6e600", |
| − | ["4"] = " | + | ["4"] = "lime", |
["5"] = "green", | ["5"] = "green", | ||
["6"] = "turquoise", | ["6"] = "turquoise", | ||
| Line 42: | Line 67: | ||
["9"] = "brown", | ["9"] = "brown", | ||
["10"] = "#808080", | ["10"] = "#808080", | ||
| − | ["11"] = "# | + | ["11"] = "#696969", |
["101"] = "lightpink", | ["101"] = "lightpink", | ||
| − | ["102"] = " | + | ["102"] = "#ffd29b", |
| − | ["103"] = " | + | ["103"] = "#ffff8e", |
["104"] = "#e0ffb3", | ["104"] = "#e0ffb3", | ||
["105"] = "lightgreen", | ["105"] = "lightgreen", | ||
| Line 53: | Line 78: | ||
["109"] = "burlywood", | ["109"] = "burlywood", | ||
["110"] = "#d3d3d3", | ["110"] = "#d3d3d3", | ||
| − | ["111"] = "# | + | ["111"] = "#C0C0C0" |
} | } | ||
if halidomValueToColor[value] ~= nil then | if halidomValueToColor[value] ~= nil then | ||
Latest revision as of 14:00, 24 January 2019
Documentation for this module may be created at Module:HalidomMap/doc
local p = {}
--explode-like split function taken from LUA documentation
function split(d,p)
local t, ll
t={}
ll=0
if(#p == 1) then
return {p}
end
while true do
l = string.find(p, d, ll, true) -- find the next d in the string
if l ~= nil then -- if "not not" found then..
table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
ll = l + 1 -- save just after where we found it for searching next time.
else
table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
break -- Break at end, as it should be, according to the lua manual.
end
end
return t
end
--renders the table of different cell colors based on given arguments
p._halidomMap = function (args)
-- Create the table
local tbl = mw.html.create('table'):css('text-align', 'center'):css('table-layout','fixed'):css('border-collapse', 'collapse')
-- Loop through all the template arguments ("rows of the halidom map")
for idx, row in ipairs(args) do
-- Create the table row
local tr = tbl:tag('tr')
-- Split the cell values
local cellValues = split(",", row)
-- Loop through all cell values in the row
for idx2, cellValue in ipairs(cellValues) do
-- Create the table cell with the background color based on the cell value
local td = tr:tag('td')
:css('width', '7px')
:css('height', '7px')
--:css('border', '1px solid #c0c0c0')
:css('background-color', getCellColor(cellValue))
end
--local td = tr:tag('td'):wikitext(row)
end
return tostring(tbl)
end
--- Function that converts a given value to a color
-- @param value
-- @return Hex color
function getCellColor(value)
local halidomValueToColor = {
["255"] = "#466999",
["0"] = "#000",
["1"] = "red",
["2"] = "#f38500",
["3"] = "#e6e600",
["4"] = "lime",
["5"] = "green",
["6"] = "turquoise",
["7"] = "blue",
["8"] = "purple",
["9"] = "brown",
["10"] = "#808080",
["11"] = "#696969",
["101"] = "lightpink",
["102"] = "#ffd29b",
["103"] = "#ffff8e",
["104"] = "#e0ffb3",
["105"] = "lightgreen",
["106"] = "#ccfeff",
["107"] = "lightblue",
["108"] = "#ffb3ff",
["109"] = "burlywood",
["110"] = "#d3d3d3",
["111"] = "#C0C0C0"
}
if halidomValueToColor[value] ~= nil then
return halidomValueToColor[value]
end
return value
end
p.halidomMap = function (frame)
local getArgs = require 'Module:Arguments'.getArgs
return p._halidomMap(getArgs(frame, {wrappers = 'Template:HalidomMap'}))
end
return p