Difference between revisions of "Module:HalidomMap"

From Dragalia Lost Wiki
Jump to navigation Jump to search
>Mornsta-gpuser
(Undo revision 37119 by Mornsta (talk))
>Mornsta-gpuser
Line 66: Line 66:
 
     ["8"] = "purple",
 
     ["8"] = "purple",
 
     ["9"] = "brown",
 
     ["9"] = "brown",
     ["10"] = "#808080",
+
     ["10"] = "dark orange",
     ["11"] = "#808080",
+
     ["11"] = "dark yellow",
 
     ["101"] = "lightpink",
 
     ["101"] = "lightpink",
 
     ["102"] = "#ffd29b",
 
     ["102"] = "#ffd29b",

Revision as of 00:23, 22 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"] = "dark orange",
    ["11"] = "dark yellow",
    ["101"] = "lightpink",
    ["102"] = "#ffd29b",
    ["103"] = "#ffff8e",
    ["104"] = "#e0ffb3",
    ["105"] = "lightgreen",
    ["106"] = "#ccfeff",
    ["107"] = "lightblue",
    ["108"] = "#ffb3ff",
    ["109"] = "burlywood",
    ["110"] = "#d3d3d3",
    ["111"] = "#d3d3d3"
  }
  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