Module:Value color/sandbox: Difference between revisions
Appearance
Content deleted Content added
BrandonXLF (talk | contribs) Syncing sandbox code with main template (sync-template-sandbox.js) |
BrandonXLF (talk | contribs) Syncing sandbox code with main template (sync-template-sandbox.js) |
||
Line 4: | Line 4: | ||
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 |
||
Line 10: | Line 9: | ||
function p.main(frame) |
function p.main(frame) |
||
local args = getArgs(frame) |
local args = getArgs(frame) |
||
local value = tonumber(args[1]) |
local value = tonumber(args[1]) |
||
local minValue = tonumber(args[2]) |
local minValue = tonumber(args[2]) |
||
local maxValue = tonumber(args[3]) |
local maxValue = tonumber(args[3]) |
||
if value |
if not (value or minValue or maxValue) then |
||
return require('Module:Error').error{'Parameters 1, 2, and 3 are required and must be numbers.'} |
return require('Module:Error').error{'Parameters 1, 2, and 3 are required and must be numbers.'} |
||
end |
end |
||
Line 20: | Line 20: | ||
local minR, minG, minB = rgb(args[4] or 'FFFFFF') |
local minR, minG, minB = rgb(args[4] or 'FFFFFF') |
||
local maxR, maxG, maxB = rgb(args[5] or '000000') |
local maxR, maxG, maxB = rgb(args[5] or '000000') |
||
⚫ | |||
⚫ | |||
local red = minR + ((maxR - minR) * percent) |
local red = minR + ((maxR - minR) * percent) |
||
local green = minG + ((maxG - minG) * percent) |
local green = minG + ((maxG - minG) * percent) |
||
local blue = minB + ((maxB - minB) * percent) |
local blue = minB + ((maxB - minB) * percent) |
||
⚫ | |||
local formatString = args['hex'] and '#%02x%02x%02x' or 'rgb(%.0f,%.0f,%.0f)' |
|||
if args['hex'] then |
|||
return string.format(formatString, red, green, blue) |
|||
else |
|||
return string.format('rgb(%.0f,%.0f,%.0f)', red, green, blue) |
|||
end |
|||
end |
end |
||
Revision as of 21:36, 24 July 2024
![]() | This is the module sandbox page for Module:Value color (diff). |
Module used by Template:Value color.
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 not (value or minValue or maxValue) 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)
local formatString = args['hex'] and '#%02x%02x%02x' or 'rgb(%.0f,%.0f,%.0f)'
return string.format(formatString, red, green, blue)
end
return p