Jump to content

Module:Value color: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
Tags: Reverted Mobile edit Mobile web edit Advanced mobile edit
Reverted 2 edits by R1F4T (talk): Broke usage with |hex=, feel free to discuss your changes on the talk page
Tags: Twinkle Undo Reverted
Line 1: Line 1:
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
local p = {}
local p = {}

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

function p.main(frame)
function p.main(frame)
local args = getArgs(frame)
local args = getArgs(frame)
local value, minValue, maxValue = tonumber(args[1]), tonumber(args[2]), tonumber(args[3])
local value = tonumber(args[1])
local minValue = tonumber(args[2])
if not (value and minValue and maxValue) then
local maxValue = tonumber(args[3])
return require('Module:Error').error{'Parameters 1, 2, and 3 are required and must be numbers.'}
end
if value == nil or minValue == nil or maxValue == nil then
local minR, minG, minB = rgb(args[4] or 'FFFFFF')
return require('Module:Error').error{'Parameters 1, 2, and 3 are required and must be numbers.'}
local maxR, maxG, maxB = rgb(args[5] or '000000')
end
local percent = math.max(0, math.min(1, (value - minValue) / (maxValue - minValue)))
local red, green, blue = minR + (maxR - minR) * percent, minG + (maxG - minG) * percent, minB + (maxB - minB) * percent
local minR, minG, minB = rgb(args[4] or 'FFFFFF')
return args['hex'] and string.format('#%x%x%x', red, green, blue) or string.format('rgb(%.0f,%.0f,%.0f)', red, green, blue)
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
end



Revision as of 21:17, 24 July 2024

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