모듈:HSL: 두 판 사이의 차이

새 문서: -- Module:HSL local p = {} -- HEX 색상 문자열 → R, G, B 변환 local function hexToRgb(hex) hex = hex:gsub("#", "") if #hex == 6 then local r = tonumber(hex:sub(1, 2), 16) local g = tonumber(hex:sub(3, 4), 16) local b = tonumber(hex:sub(5, 6), 16) return r, g, b end return 0, 0, 0 -- fallback end -- RGB → HSL local function rgbToHsl(r, g, b) r = r / 255 g = g / 255 b = b / 255 local max = math.max(r, g,...
 
편집 요약 없음
 
(같은 사용자의 중간 판 6개는 보이지 않습니다)
2번째 줄: 2번째 줄:
local p = {}
local p = {}


-- HEX 색상 문자열 → R, G, B 변환
local function trim(s)
    if not s then return nil end
    return s:match("^%s*(.-)%s*$")
end
 
local function hexToRgb(hex)
local function hexToRgb(hex)
     hex = hex:gsub("#", "")
     hex = trim(hex):gsub("#", "")
     if #hex == 6 then
     if #hex == 6 then
         local r = tonumber(hex:sub(1, 2), 16)
         return tonumber(hex:sub(1,2),16),
        local g = tonumber(hex:sub(3, 4), 16)
              tonumber(hex:sub(3,4),16),
        local b = tonumber(hex:sub(5, 6), 16)
              tonumber(hex:sub(5,6),16)
        return r, g, b
     end
     end
     return 0, 0, 0 -- fallback
     return 0, 0, 0
end
end


43번째 줄: 46번째 줄:
-- L 조정
-- L 조정
local function adjustL(l, h)
local function adjustL(l, h)
     local l_adj = l > 50 and (-0.6 * l + 70) or (0.8 * l)
     local l_adj = l > 50 and (-0.6 * l + 70) or (0.75 * l)
     if h > 39 and h < 88 then
     if h > 39 and h < 88 then
         l_adj = l_adj - 10
         l_adj = l_adj - 10
52번째 줄: 55번째 줄:
-- H, S 수정 규칙
-- H, S 수정 규칙
local function applySpecialRules(h, s, l)
local function applySpecialRules(h, s, l)
     if (l > 90 or (h > 220 and h < 268 and l > 90)) or s < 12.5 then
     if (l > 90 or (h > 220 and h < 268 and l > 80)) or s < 12.5 then
         return 200, 5, l
         return 200, 5, l
     else
     else
58번째 줄: 61번째 줄:
     end
     end
end
end
function p.main(frameOrArgs)
    local args
    -- frame인지, 직접 args인지 판별
    if type(frameOrArgs) == "table" and frameOrArgs.getParent then
        local frame = frameOrArgs
        args = frame.args or {}
        -- 부모 args 병합 (기존 동작 유지)
        if frame:getParent() then
            for k, v in pairs(frame:getParent().args) do
                if args[k] == nil then
                    args[k] = v
                end
            end
        end
    else
        -- Lua 내부 직접 호출
        args = frameOrArgs or {}
    end


-- 메인
function p.main(frame)
    local args = frame:getParent().args
     local colorStr = args[1] or ""
     local colorStr = args[1] or ""
     local r = tonumber(args['r'])
     local r = tonumber(args.r)
     local g = tonumber(args['g'])
     local g = tonumber(args.g)
     local b = tonumber(args['b'])
     local b = tonumber(args.b)


    -- HEX 형식이면 파싱
     if type(colorStr) == "string" and colorStr:match("^#%x%x%x%x%x%x$") then
     if colorStr:match("^#%x%x%x%x%x%x$") then
         r, g, b = hexToRgb(colorStr)
         r, g, b = hexToRgb(colorStr)
     end
     end


     if not (r and g and b) then
     if not (r and g and b) then
         return "Invalid RGB input"
         return colorStr ~= "" and colorStr or "Invalid RGB input"
     end
     end


80번째 줄: 99번째 줄:
     local l_adj = adjustL(l, h)
     local l_adj = adjustL(l, h)


     return string.format("HSL(%d, %d%%, %d%%)", math.floor(h + 0.5), math.floor(s + 0.5), l_adj)
     return string.format(
        "hsl(%d %d%% %d%%)",
        math.floor(h + 0.5),
        math.floor(s + 0.5),
        l_adj
    )
end
end
return p
return p

2026년 1월 2일 (금) 21:59 기준 최신판

이 모듈에 대한 설명문서는 모듈:HSL/설명문서에서 만들 수 있습니다

-- Module:HSL
local p = {}

local function trim(s)
    if not s then return nil end
    return s:match("^%s*(.-)%s*$")
end

local function hexToRgb(hex)
    hex = trim(hex):gsub("#", "")
    if #hex == 6 then
        return tonumber(hex:sub(1,2),16),
               tonumber(hex:sub(3,4),16),
               tonumber(hex:sub(5,6),16)
    end
    return 0, 0, 0
end

-- RGB → HSL
local function rgbToHsl(r, g, b)
    r = r / 255
    g = g / 255
    b = b / 255

    local max = math.max(r, g, b)
    local min = math.min(r, g, b)
    local h, s, l = 0, 0, (max + min) / 2

    if max ~= min then
        local d = max - min
        s = l > 0.5 and d / (2 - max - min) or d / (max + min)

        if max == r then
            h = (g - b) / d + (g < b and 6 or 0)
        elseif max == g then
            h = (b - r) / d + 2
        else
            h = (r - g) / d + 4
        end
        h = h / 6
    end

    return h * 360, s * 100, l * 100
end

-- L 조정
local function adjustL(l, h)
    local l_adj = l > 50 and (-0.6 * l + 70) or (0.75 * l)
    if h > 39 and h < 88 then
        l_adj = l_adj - 10
    end
    return math.max(0, math.min(100, math.floor(l_adj + 0.5)))
end

-- H, S 수정 규칙
local function applySpecialRules(h, s, l)
    if (l > 90 or (h > 220 and h < 268 and l > 80)) or s < 12.5 then
        return 200, 5, l
    else
        return h, s, l
    end
end
function p.main(frameOrArgs)
    local args

    -- frame인지, 직접 args인지 판별
    if type(frameOrArgs) == "table" and frameOrArgs.getParent then
        local frame = frameOrArgs
        args = frame.args or {}

        -- 부모 args 병합 (기존 동작 유지)
        if frame:getParent() then
            for k, v in pairs(frame:getParent().args) do
                if args[k] == nil then
                    args[k] = v
                end
            end
        end
    else
        -- Lua 내부 직접 호출
        args = frameOrArgs or {}
    end

    local colorStr = args[1] or ""
    local r = tonumber(args.r)
    local g = tonumber(args.g)
    local b = tonumber(args.b)

    if type(colorStr) == "string" and colorStr:match("^#%x%x%x%x%x%x$") then
        r, g, b = hexToRgb(colorStr)
    end

    if not (r and g and b) then
        return colorStr ~= "" and colorStr or "Invalid RGB input"
    end

    local h, s, l = rgbToHsl(r, g, b)
    h, s, l = applySpecialRules(h, s, l)
    local l_adj = adjustL(l, h)

    return string.format(
        "hsl(%d %d%% %d%%)",
        math.floor(h + 0.5),
        math.floor(s + 0.5),
        l_adj
    )
end
return p