2023年政策修订增补工作正在进行中,欢迎参与!
Module:Sandbox/東東君/sandbox
跳转到导航
跳转到搜索
local module = {}
local getArgs = require('Module:Arguments').getArgs
local Color = require('Module:color')
function _main(args)
local action = args[1]
local arg1 = args[2]
local arg2 = args[3]
local arg3 = args[4]
if action == 'reverse' then
local color = arg1
return Color.create(color):reverse():toString('hex')
end
if action == 'random' then
local min = arg1 or 0
local max = arg2 or 255
return Color.random(min, max):toString('hex')
end
if action == 'opacity' then
local color = arg1
local opacity = arg2
return Color.create(color):setOpacity(opacity):toString()
end
if action == 'isLight' then
local color = arg1
return Color.create(color):isLight() and 1 or ''
end
if action == 'isDark' then
local color = arg1
return Color.create(color):isDark() and 1 or ''
end
if action == 'mix' then
local color1 = Color.create(arg1)
local color2 = Color.create(arg2)
local weight = arg3
return color1:mix(color2, weight):rgb():toString('hex-opacity')
end
if action == 'test' then
function block(text, color)
return '<ruby style="color:'..color..'">■<rt style="color:black">'..text..'<rt></ruby> '
end
local color = Color.create(arg1)
local left = ''
local right = ''
for i=1, 9 do
i = i * 10
left = block('-'..i, color:clone():lighten(i):toString())..left
right = right..block('+'..i, color:clone():darken(i):toString())
end
return '<div style="font-size:30px">'..left..block('▼', color:toString())..right..'</div>'
end
if action:find('^[sl]?[%+%-]') then
local color = Color.create(arg1)
local operateProperty = 'lightness'
local operator = ''
local ratio = 0
if action:find('^[sl]') then
operateProperty = ({
s = 'saturation',
l = 'lightness'
})[action:sub(1, 1)]
operator = action:sub(2, 2)
ratio = tonumber(action:sub(3)) or 10
else
operator = action:sub(1, 1)
ratio = tonumber(action:sub(2)) or 10
end
-- 映射要使用的每组方法
local methods = ({
saturation = {'saturate', 'desaturate'},
lightness = {'darken', 'lighten'}
})[operateProperty]
local usedMethod = operator == '+' and methods[1] or methods[2]
return Color[usedMethod](color, ratio):toString('hex')
end
end
function module.main(frame)
local args = getArgs(frame)
return _main(args)
end
return module