Module:Math-colors

-- 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 " ..                    ", 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("$$" .. expression .. "$$") 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 --- 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 --