FANDOM


local color = {}
 
local getArgs = getArgs or require( 'Module:Arguments' ).getArgs
local scheme = scheme or mw.loadData( 'Module:Color/Scheme' )
local typeScheme = typeScheme or scheme.types -- 타입 색상표
local moveScheme = moveScheme or scheme.moves -- 기술분류 색상표
 
-- 문자열이 유효한 Hex색상값인지 확인
-- 입력값이 유효하면 색상 포맷으로 리턴(#가 없으면 붙임)
-- 입력값이 nil 이면 'inherit'을 리턴
local function formatColor( value )
    if value == nil then return 'inherit' end
    local hex = value:match( '%x%x%x[%x]?[%x]?[%x]?' )
    return hex and '#' .. hex  or value
end
 
-- 틀:색
function color.toText ( colorStr, text )
    if not text then return '' end
 
    local colorVal = formatColor( colorStr )
    local ret = mw.html.create( 'span' )
        :css( 'color', colorVal )
        :wikitext( text )
        :done()
 
    return ret
end
 
-- 틀:색2
function color.toLink ( colorStr, pageName, altName )
    if not pageName then return '' end
 
    local colorVal = formatColor( colorStr )
    local ret = '[[' .. pageName .. '|<span style="color:' .. colorVal ..'">' .. ( altName or pageName ) .. '</span>]]'
 
    return ret
end
 
-- 틀:색타
local function typeToLink( typeName )
    return '[[' .. typeName .. ' (타입)|<span class="split-cell text-white" style="background:' .. typeScheme[typeName][2] .. '">' .. typeName .. '</span>]]'
end
 
local function typeToGradient( typeTable )
    local colors, gradientMap, gradientStop, ret = {}, {}, 0, ''
 
    for i = 1, #typeTable do
        colors[i] = typeScheme[ typeTable[i] ][2]
    end
 
    if #typeTable == 1 then
        ret = 'background: ' .. colors[1] .. ';'
    else
        for i = 1, #colors do
            table.insert( gradientMap, colors[i] .. ' ' .. gradientStop .. '%' )
            gradientStop = gradientStop + ( 100 / #colors )
            table.insert( gradientMap, colors[i] .. ' ' .. gradientStop .. '%' )
        end
        ret = 'background: linear-gradient(to right, '
            .. table.concat( gradientMap, ',') .. ');'
    end
 
    return ret
end
 
function color.toTypeLink ( ... )
    local argHash, types, ret = {}, {}, ''
 
    for _, v in ipairs( arg ) do
        if ( v ~= nil ) and ( not argHash[v] ) then -- 중복, 빈 파라미터 무시
            if typeScheme[v] then -- 올바른 타입만 픽업
                ret = ret .. typeToLink( v ) -- 링크 생성
                table.insert( types, v ) -- 새 테이블에 타입 추가
                argHash[v] = true
            end
        end
    end
 
    ret = '<span class="split-cell-wrap text-center"><span class="split-cell-bg" style="' .. typeToGradient( types ) .. '"></span>' .. ret .. '</span>'
 
    return ret
end
 
-- 틀:기분
function color.toMoveText ( move )
    local colorVal
    local ret = ''
 
    if move and moveScheme[move] then
        colorVal = moveScheme[move][2]
        ret = '<span class="split-cell-wrap text-center"><span class="split-cell-bg" style="background: ' .. colorVal .. ';"></span><span class="split-cell text-white" style="background: ' .. colorVal .. ';">' .. move .. '</span></span>'
    end
 
    return ret
end
 
function color.main( frame )
    local args = getArgs( frame )
    local requested = args['request']
    local ret
 
    if requested == 'toText' then
        ret = color.toText( args[1], args[2] )
    elseif requested == 'toLink' then
        ret = color.toLink( args[1], args[2], args[3] )
    elseif requested == 'toTypeLink' then
        ret = color.toTypeLink( args[1], args[2] )
    elseif requested == 'toMoveText' then
        ret = color.toMoveText( args[1] )
    end
 
    return ret
end
 
return color