Module:HalidomMap2

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', '6px') :css('height', '6px') :css('border', '1px solid #009cff') :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:HalidomMap2'})) end

return p