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

새 문서: local p = {} local function hexToRgb(hex) hex = 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 local function rgbToHsl(r, g, b) r, g, b = r/255, g/255, b/255 local max, min = math.max(r,g,b), 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...
 
편집 요약 없음
 
(같은 사용자의 중간 판 5개는 보이지 않습니다)
1번째 줄: 1번째 줄:
local p = {}
local p = {}
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
         return tonumber(hex:sub(1,2),16), tonumber(hex:sub(3,4),16), tonumber(hex:sub(5,6),16)
         return tonumber(hex:sub(1,2),16),
              tonumber(hex:sub(3,4),16),
              tonumber(hex:sub(5,6),16)
     end
     end
     return 0, 0, 0
     return 0, 0, 0
30번째 줄: 37번째 줄:
end
end


function p.main(frame)
function p.main(frameOrArgs)
     local args = frame:getParent().args
     local args
 
    -- 🧠 frame인지 직접 args인지 판별
    if type(frameOrArgs) == "table" and frameOrArgs.getParent then
        local frame = frameOrArgs
        args = frame:getParent() and frame:getParent().args or frame.args
    else
        -- Lua 내부 직접 호출용
        args = frameOrArgs or {}
    end
 
     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)


     if colorStr:match("^#%x%x%x%x%x%x$") then
     if type(colorStr) == "string" and colorStr:match("^#%x%x%x%x%x%x$") then
         r, g, b = hexToRgb(colorStr)
         r, g, b = hexToRgb(colorStr)
     end
     end
47번째 줄: 64번째 줄:
     local h, s, l = rgbToHsl(r, g, b)
     local h, s, l = rgbToHsl(r, g, b)


     -- 🎯 새 기준에 따라 처리
     -- 🎯 새 기준
     if l < 17.5 then
     if l < 17.5 then
         return "#c3beb6"
         return "HSL(37, 10%, 74%)"
     elseif l < 64 then
     elseif l < 64 then
         l = 75
         l = 75
     end
     end


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


return p
return p

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

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

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

local function rgbToHsl(r, g, b)
    r, g, b = r/255, g/255, b/255
    local max, min = math.max(r,g,b), 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

function p.main(frameOrArgs)
    local args

    -- 🧠 frame인지 직접 args인지 판별
    if type(frameOrArgs) == "table" and frameOrArgs.getParent then
        local frame = frameOrArgs
        args = frame:getParent() and frame:getParent().args or frame.args
    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 "Invalid RGB input"
    end

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

    -- 🎯 새 기준
    if l < 17.5 then
        return "HSL(37, 10%, 74%)"
    elseif l < 64 then
        l = 75
    end

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

return p