Module:Gradient: Difference between revisions
From MuttWiki
				
				
				Jump to navigationJump to search
				
				
No edit summary  | 
				No edit summary  | 
				||
| Line 20: | Line 20: | ||
	for i=1,n do  | 	for i=1,n do  | ||
		local ch = mw.ustring.codepoint(text,i,i)  | 		local ch = mw.ustring.codepoint(text,i,i)  | ||
		out = out.."<span style='color: #"..fmt_colour(r,g,b)..";'>"..mw.text.nowiki(ch).."</span>"  | 		out = out.."<span style='color: #"..fmt_colour(r,g,b)..";'>"..mw.text.nowiki(mw.ustring.char(ch)).."</span>"  | ||
		r=r+dr  | 		r=r+dr  | ||
		g=g+dg  | 		g=g+dg  | ||
Revision as of 09:13, 22 February 2025
This module provides support for generating text gradients. 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!}} → Hello world!
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(mw.ustring.char(ch)).."</span>"
		r=r+dr
		g=g+dg
		b=b+db
	end
	return out
end
return p