On Fanlore, users with accounts can edit pages including user pages, can create pages, and more. Any information you publish on a page or an edit summary will be accessible by the public and to Fanlore personnel. Because Fanlore is a wiki, information published on Fanlore will be publicly available forever, even if edited later. Be mindful when sharing personal information, including your religious or political views, health, racial background, country of origin, sexual identity and/or personal relationships. To learn more, check out our Terms of Service and Privacy Policy. Select "dismiss" to agree to these terms.

Module:Value color

From Fanlore
Jump to navigation Jump to search

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

local getArgs = require('Module:Arguments').getArgs
local p = {}

local function rgb(color)
	local _, _, R, G, B = color:find('(%w%w)(%w%w)(%w%w)')
	
	return tonumber(R, 16), tonumber(G, 16), tonumber(B, 16)
end

function p.main(frame)
	local args = getArgs(frame)
	local value = tonumber(args[1])
	local minValue = tonumber(args[2])
	local maxValue = tonumber(args[3])
	
	if value == nil or minValue == nil or maxValue == nil then
		return require('Module:Error').error{'Parameters 1, 2, and 3 are required and must be numbers.'}
	end
	
	local minR, minG, minB = rgb(args[4] or 'FFFFFF')
	local maxR, maxG, maxB = rgb(args[5] or '000000')
	
	local percent = math.max(0, math.min(1, (value - minValue) / (maxValue - minValue)))

	local red = minR + ((maxR - minR) * percent)
	local green = minG + ((maxG - minG) * percent)
	local blue = minB + ((maxB - minB) * percent)

	if args['hex'] then
		return string.format('#%x%x%x', red, green, blue)
	else
		return string.format('rgb(%.0f,%.0f,%.0f)', red, green, blue)
	end
end

return p