https://en.wikipedia.org/w/index.php?action=history&feed=atom&title=Module%3ARainbow
Module:Rainbow - Revision history
2025-06-09T09:42:19Z
Revision history for this page on the wiki
MediaWiki 1.45.0-wmf.4
https://en.wikipedia.org/w/index.php?title=Module:Rainbow&diff=968502569&oldid=prev
Psiĥedelisto: Implements Template:Rainbow
2020-07-19T20:06:23Z
<p>Implements <a href="/wiki/Template:Rainbow" title="Template:Rainbow">Template:Rainbow</a></p>
<p><b>New page</b></p><div>local p = {}<br />
<br />
local function format_it(tbl, name)<br />
for i, v in ipairs(tbl) do<br />
tbl[i] = string.format("%06x", tbl[i]) <br />
end<br />
p[name] = tbl<br />
end<br />
<br />
local function mod(a, b)<br />
return a - math.floor(a/b) * b<br />
end<br />
<br />
-- takes hex color as string, gives back table<br />
local function colorTable(color)<br />
local clen = string.len(color)<br />
assert(clen >= 6 and clen <= 8)<br />
assert(string.gmatch("^"..("[0-9a-fA-F]"):rep(6)..("[0-9a-fA-F]?"):rep(2).."$", color))<br />
local ret = {}<br />
ret["R"] = tonumber(color:sub(1,2), 16)<br />
ret["G"] = tonumber(color:sub(3,4), 16)<br />
ret["B"] = tonumber(color:sub(5,6), 16)<br />
ret["A"] = tonumber(color:sub(7,8), 16) or 255<br />
return ret<br />
end<br />
local black = colorTable("000000")<br />
local white = colorTable("FFFFFF")<br />
<br />
local function colorAsRGBA(colorTbl)<br />
return string.format("rgba(%.2f,%.2f,%.2f,%.2f)", <br />
colorTbl["R"], colorTbl["G"], <br />
colorTbl["B"], colorTbl["A"])<br />
end<br />
<br />
-- simple mix of two colors. t_ means target. percent is as float (.4=40%)<br />
local function colorMix(colorTbl, t_colorTbl, percent)<br />
local ret = {}<br />
local diff = {}<br />
diff["R"] = t_colorTbl["R"] - colorTbl["R"] <br />
diff["G"] = t_colorTbl["G"] - colorTbl["G"] <br />
diff["B"] = t_colorTbl["B"] - colorTbl["B"] <br />
diff["A"] = t_colorTbl["A"] - colorTbl["A"]<br />
ret["R"] = colorTbl["R"] + (diff["R"] * percent)<br />
ret["G"] = colorTbl["G"] + (diff["G"] * percent)<br />
ret["B"] = colorTbl["B"] + (diff["B"] * percent)<br />
ret["A"] = colorTbl["A"] + (diff["A"] * percent)<br />
return ret<br />
end<br />
<br />
p.HTML = function(frame, page)<br />
local args = require('Module:Arguments').getArgs(frame)<br />
p.args = args<br />
local ret = ''<br />
local inp = args[1]<br />
if not inp then return nil end<br />
local steps = mw.ustring.len(inp)<br />
local repeat_ = (args["repeat"] and tonumber(args["repeat"])) or 1<br />
<br />
local newcolors = {}<br />
if args.colors then<br />
for k = 1, repeat_, 1 do<br />
nc = mw.text.gsplit(args.colors, ",", true)<br />
<br />
for c in nc do<br />
newcolors[#newcolors+1] = tonumber(c:sub(2, -1), 16)<br />
end<br />
end<br />
end<br />
<br />
format_it(#newcolors > 0 and newcolors or<br />
-- red orange yellow green blue indigo-violet<br />
{0xFF0000, 0xFF7F00, 0xFFFF00, 0x00FF00, 0x0000FF, 0x8B00FF}, "roygbiv")<br />
<br />
-- lua pattern from https://stackoverflow.com/questions/13235091/extract-the-first-letter-of-a-utf-8-string-with-lua<br />
local i = 0<br />
for c in inp:gmatch("[%z\1-\127\194-\244][\128-\191]*") do<br />
local color = colorTable(p.roygbiv[mod(i, #p.roygbiv)+1])<br />
<br />
local s_per = steps / #p.roygbiv<br />
local j = (i / s_per)<br />
local jf = math.floor(j)<br />
if args.gradient then<br />
local cidx = mod(jf, #p.roygbiv)+1<br />
if mod(cidx, 2) == 0 then cidx = cidx + 1 end<br />
if cidx > #p.roygbiv then cidx = #p.roygbiv end<br />
color = colorTable(p.roygbiv[cidx])<br />
<br />
local tidx = mod(jf+1, #p.roygbiv)+1<br />
if mod(tidx, 2) == 1 then tidx = tidx + 1 end<br />
if tidx > #p.roygbiv then tidx = cidx end<br />
local tcolor = colorTable(p.roygbiv[tidx])<br />
<br />
color = colorMix(color, tcolor, mod(j, 1))<br />
end<br />
local subdued = nil<br />
if args.theme then <br />
subdued = args.theme:match("^subdued(%d+)%%$")<br />
end<br />
if subdued then<br />
color = colorMix(color, black, tonumber(subdued) / 100) <br />
end<br />
assert(color)<br />
if args.bg ~= "y" then<br />
local rgba = colorAsRGBA(color)<br />
ret = (ret .. "<span style='" .. (args.bg == "black" and "background-color:black;" or "")<br />
.. "color:" .. rgba .. ";'>" .. c .."</span>")<br />
else<br />
local rgba = colorAsRGBA(args.fgcolor and colorTable(args.fgcolor:sub(2, -1)) or black)<br />
ret = (ret .. "<span style='background-color:" .. colorAsRGBA(color) .. ";"<br />
.. "color:" .. rgba .. "'>" .. c .."</span>")<br />
end<br />
i = i + 1<br />
end<br />
<br />
return ret<br />
end<br />
<br />
return p</div>
Psiĥedelisto