Module:GTest: Difference between revisions

From Wavu Wiki, the 🌊 wavy Tekken wiki
mNo edit summary
mNo edit summary
Tag: Manual revert
 
(4 intermediate revisions by the same user not shown)
Line 19: Line 19:
p._main = function(val, left, right, invert)
p._main = function(val, left, right, invert)
invert = invert or false
invert = invert or false
local transformed = (val - left) * bins / (right - left)
local transformed = (val - left) * bins / ((right + 1) - left)
local bin_idx = math.max(0, math.min(bins - 1, math.floor(transformed)))
local bin_idx = math.max(0, math.min(bins - 1, math.floor(transformed)))
if bin_idx == math.floor(bins / 2) then
if val > 0 then
bin_idx = bin_idx + 1
elseif val < 0 then
bin_idx = bin_idx - 1
end
end
if invert then bin_idx = steps - 1 - bin_idx end
if invert then bin_idx = steps - 1 - bin_idx end
return 'gradient_' .. palette .. '_' .. bin_idx
return 'gradient_' .. palette .. '_' .. bin_idx

Latest revision as of 20:24, 28 May 2024

Documentation for this module may be created at Module:GTest/doc

-- Take a number and calculate how many steps in the gradient it is.
-- Output is a class name defined in MediaWiki:Common.css.

local yesno = require('Module:Yesno')
local p = {}

local bins = 15
local palette = 'diverging-red-blue'

p.main = function(frame)
	local val = tonumber(frame.args[1])
	if val == nil then return '' end
	local right = tonumber(frame.args['right']) or 100
	local left = tonumber(frame.args['left']) or right * -1
	local invert = yesno(frame.args['invert'], false)
	return p._main(val, left, right, invert)
end

p._main = function(val, left, right, invert)
	invert = invert or false
	local transformed = (val - left) * bins / ((right + 1) - left)
	local bin_idx = math.max(0, math.min(bins - 1, math.floor(transformed)))
	if invert then bin_idx = steps - 1 - bin_idx end
	return 'gradient_' .. palette .. '_' .. bin_idx
end

return p