Module:Gradient

From MuttWiki
Revision as of 09:12, 22 February 2025 by Bofh (talk | contribs)
Jump to navigationJump to search

8410410511532109111100117108101321121141111181051001011153211511711211211111411632102111114321031011101011149711610511010332116101120116321031149710010510111011611546 It contains the following functions:

rgb

{{#invoke:Gradient|rgb|<from>|<to>|<text>}}
  • from: A 6-digit hex code for the start colour. No starting #.
  • to: A 6-digit hex code for the end colour. No starting #.
  • text: The text to apply the gradient effect to. Plain text only.

Example: {{#invoke:Gradient|rgb|FF0000|0000FF|Hello world!}}721011081081113211911111410810033

multi

{{#invoke:Gradient|multi|<colours>|<text>}}
  • colours: A comma-separated list of 6-digit hex codes. No starting #s.
  • text: The text to apply the gradient effect to. Plain text only.

Example: {{#invoke:Gradient|multi|FF0000,FFFF00,00FF00,00FFFF,0000FF,FF00FF|Hello world!}}Script error: The function "multi" does not exist.


local p={}

function parse_colour(c)
	return
		tonumber(string.sub(c,1,2),16),
		tonumber(string.sub(c,3,4),16),
		tonumber(string.sub(c,5,6),16)
end
function fmt_colour(r,g,b)
	return string.format("%02x%02x%02x",math.floor(r),math.floor(g),math.floor(b))
end

function p.rgb(frame)
	local r,g,b = parse_colour(frame.args[1])
	local r2,g2,b2 = parse_colour(frame.args[2])
	local text = frame.args[3]
	local n = mw.ustring.len(text)
	local out = ""
	local dr,dg,db = (r2-r)/n, (g2-g)/n, (b2-b)/n
	for i=1,n do
		local ch = mw.ustring.codepoint(text,i,i)
		out = out.."<span style='color: #"..fmt_colour(r,g,b)..";'>"..mw.text.nowiki(ch).."</span>"
		r=r+dr
		g=g+dg
		b=b+db
	end
	return out
end

return p