Jump to content

Module:Value color

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BrandonXLF (talk | contribs) at 22:03, 9 May 2021 (Module for Template:Value color). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

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 an must be numbers.'}
	end
	
	local minR, minG, minB = rgb(args[4] or 'FFFFFF')
	local maxR, maxG, maxB = rgb(args[5] or '000000')
	
	local percent = (value - minValue) / (maxValue - minValue)

	local R = minR + ((maxR - minR) * percent)
	local G = minG + ((maxG - minG) * percent)
	local B = minB + ((maxB - minB) * percent)
	
	return 'rgb(' .. R .. ',' .. G .. ',' .. B .. ')'
end

return p