FANDOM


-- <nowiki>
local p = {}
local getArgs = require("Dev:Arguments").getArgs
local userError = require("Dev:User error")
 
-- @TODO: replace this with a real color module some day
local function isValidRgbColor(str)
    local channels = mw.text.split(str, "%s*,%s*")
 
    if #channels ~= 3 then
        return false
    end
 
    for i = 1, #channels do
        local channel = tonumber(channels[i])
 
        if not channel or (channel % 1 ~= 0)
        or (channel < 0) or (channel > 255) then
            return false
        end
    end
 
    return true
end
 
--% Describe the following function here.
--@ args (table)
--@ args.expression (string)
--@ [args.bgColor] (string) defaults to "255,255,255"
--@ [args.textColor] (string) defaults to "0,0,0"
--: (str)
function p.render(args)
    local expression = args.expression
    local bgColor = args.bgColor or "255,255,255"
    local textColor = args.textColor or "0,0,0"
    local badColor = "invalid %s color (must be in the format " ..
                     "<code>[red],[green],[blue]</code>, where each is a " ..
                     "whole number from 0 through 255)"
 
    if not expression then
        error("missing expression", 0)
    end
 
    if not isValidRgbColor(bgColor) then
        error(badColor:format("background"), 0)
    end
 
    if not isValidRgbColor(textColor) then
        error(badColor:format("text"), 0)
    end
 
    local function parseMath(expression)
        local frame = mw.getCurrentFrame()
 
        return frame:preprocess("<math>" .. expression .. "</math>")
    end
 
    local colored = mw.html.create("span")
        :addClass("hidden")
        :css("display", "inline")
        :wikitext(parseMath(
            "\\pagecolor[RGB]{" .. bgColor .. "}" ..
            "{\\color[RGB]{" .. textColor .. "}" .. expression .. "}"
        ))
 
    local plain = mw.html.create("span")
        :css("display", "none")
        :wikitext(parseMath(expression))
 
    return tostring(colored) .. tostring(plain)
end
 
--% Describe the following function here.
--@ frame (table) The
--- [[Lua reference manual/Scribunto libraries#Frame object|frame object]]
--: (str)
function p.main(frame)
    local args = getArgs(frame)
    local success, response = pcall(p.render, args)
 
    return success and response or userError(response)
end
 
return p
-- </nowiki>

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.