置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

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>&nbsp;'
    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