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(&quot;%06x&quot;, 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 &gt;= 6 and clen &lt;= 8)<br /> assert(string.gmatch(&quot;^&quot;..(&quot;[0-9a-fA-F]&quot;):rep(6)..(&quot;[0-9a-fA-F]?&quot;):rep(2)..&quot;$&quot;, color))<br /> local ret = {}<br /> ret[&quot;R&quot;] = tonumber(color:sub(1,2), 16)<br /> ret[&quot;G&quot;] = tonumber(color:sub(3,4), 16)<br /> ret[&quot;B&quot;] = tonumber(color:sub(5,6), 16)<br /> ret[&quot;A&quot;] = tonumber(color:sub(7,8), 16) or 255<br /> return ret<br /> end<br /> local black = colorTable(&quot;000000&quot;)<br /> local white = colorTable(&quot;FFFFFF&quot;)<br /> <br /> local function colorAsRGBA(colorTbl)<br /> return string.format(&quot;rgba(%.2f,%.2f,%.2f,%.2f)&quot;, <br /> colorTbl[&quot;R&quot;], colorTbl[&quot;G&quot;], <br /> colorTbl[&quot;B&quot;], colorTbl[&quot;A&quot;])<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[&quot;R&quot;] = t_colorTbl[&quot;R&quot;] - colorTbl[&quot;R&quot;] <br /> diff[&quot;G&quot;] = t_colorTbl[&quot;G&quot;] - colorTbl[&quot;G&quot;] <br /> diff[&quot;B&quot;] = t_colorTbl[&quot;B&quot;] - colorTbl[&quot;B&quot;] <br /> diff[&quot;A&quot;] = t_colorTbl[&quot;A&quot;] - colorTbl[&quot;A&quot;]<br /> ret[&quot;R&quot;] = colorTbl[&quot;R&quot;] + (diff[&quot;R&quot;] * percent)<br /> ret[&quot;G&quot;] = colorTbl[&quot;G&quot;] + (diff[&quot;G&quot;] * percent)<br /> ret[&quot;B&quot;] = colorTbl[&quot;B&quot;] + (diff[&quot;B&quot;] * percent)<br /> ret[&quot;A&quot;] = colorTbl[&quot;A&quot;] + (diff[&quot;A&quot;] * percent)<br /> return ret<br /> end<br /> <br /> p.HTML = function(frame, page)<br /> local args = require(&#039;Module:Arguments&#039;).getArgs(frame)<br /> p.args = args<br /> local ret = &#039;&#039;<br /> local inp = args[1]<br /> if not inp then return nil end<br /> local steps = mw.ustring.len(inp)<br /> local repeat_ = (args[&quot;repeat&quot;] and tonumber(args[&quot;repeat&quot;])) 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, &quot;,&quot;, 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 &gt; 0 and newcolors or<br /> -- red orange yellow green blue indigo-violet<br /> {0xFF0000, 0xFF7F00, 0xFFFF00, 0x00FF00, 0x0000FF, 0x8B00FF}, &quot;roygbiv&quot;)<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(&quot;[%z\1-\127\194-\244][\128-\191]*&quot;) 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 &gt; #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 &gt; #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(&quot;^subdued(%d+)%%$&quot;)<br /> end<br /> if subdued then<br /> color = colorMix(color, black, tonumber(subdued) / 100) <br /> end<br /> assert(color)<br /> if args.bg ~= &quot;y&quot; then<br /> local rgba = colorAsRGBA(color)<br /> ret = (ret .. &quot;&lt;span style=&#039;&quot; .. (args.bg == &quot;black&quot; and &quot;background-color:black;&quot; or &quot;&quot;)<br /> .. &quot;color:&quot; .. rgba .. &quot;;&#039;&gt;&quot; .. c ..&quot;&lt;/span&gt;&quot;)<br /> else<br /> local rgba = colorAsRGBA(args.fgcolor and colorTable(args.fgcolor:sub(2, -1)) or black)<br /> ret = (ret .. &quot;&lt;span style=&#039;background-color:&quot; .. colorAsRGBA(color) .. &quot;;&quot;<br /> .. &quot;color:&quot; .. rgba .. &quot;&#039;&gt;&quot; .. c ..&quot;&lt;/span&gt;&quot;)<br /> end<br /> i = i + 1<br /> end<br /> <br /> return ret<br /> end<br /> <br /> return p</div> Psiĥedelisto