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

편집 요약 없음
편집 요약 없음
52번째 줄: 52번째 줄:
     if not r then return g end -- 오류 메시지 반환
     if not r then return g end -- 오류 메시지 반환
     local h, s, l = rgb_to_hsl(r, g, b)
     local h, s, l = rgb_to_hsl(r, g, b)
    -- 명도 조정
     if direction == "brighten" then
     if direction == "brighten" then
         l = math.min(l + percentage / 100, 1)
         l = math.min(l + percentage / 100, 0.95) -- 최대 95%로 제한
     elseif direction == "darken" then
     elseif direction == "darken" then
         l = math.max(l - percentage / 100, 0)
         l = math.max(l - percentage / 100, 0.05) -- 최소 5%로 제한
     else
     else
         return "방향은 'brighten' 또는 'darken'만 가능합니다."
         return "방향은 'brighten' 또는 'darken'만 가능합니다."
     end
     end
    -- 다시 RGB로 변환
     r, g, b = hsl_to_rgb(h, s, l)
     r, g, b = hsl_to_rgb(h, s, l)
     return string.format("#%02X%02X%02X", math.floor(r * 255), math.floor(g * 255), math.floor(b * 255))
     return string.format("#%02X%02X%02X", math.floor(r * 255), math.floor(g * 255), math.floor(b * 255))

2025년 4월 19일 (토) 09:41 판

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

local p = {}

-- 헥스 코드에서 RGB 추출
local function hex_to_rgb(hex)
    hex = hex:match("^#?(%x%x%x%x%x%x)$")
    if not hex then
        return nil, "유효하지 않은 헥스 코드입니다. 6자리 색상 코드 (#RRGGBB)를 입력하세요."
    end
    local r = tonumber(hex:sub(1, 2), 16) / 255
    local g = tonumber(hex:sub(3, 4), 16) / 255
    local b = tonumber(hex:sub(5, 6), 16) / 255
    return r, g, b
end

-- RGB를 HSL로 변환
local function rgb_to_hsl(r, g, b)
    local max = math.max(r, g, b)
    local min = math.min(r, g, b)
    local l = (max + min) / 2
    if max == min then
        return 0, 0, l -- 무채색
    end
    local d = max - min
    local s = d / (l > 0.5 and (2 - max - min) or (max + min))
    local h = ((max == r and (g - b) / d + (g < b and 6 or 0)) or
               (max == g and (b - r) / d + 2) or
               (max == b and (r - g) / d + 4)) / 6
    return h, s, l
end

-- HSL을 RGB로 변환
local function hsl_to_rgb(h, s, l)
    local function hue_to_rgb(p, q, t)
        if t < 0 then t = t + 1 end
        if t > 1 then t = t - 1 end
        if t < 1/6 then return p + (q - p) * 6 * t end
        if t < 1/2 then return q end
        if t < 2/3 then return p + (q - p) * (2/3 - t) * 6 end
        return p
    end
    if s == 0 then return l, l, l end -- 무채색
    local q = l < 0.5 and l * (1 + s) or l + s - l * s
    local p = 2 * l - q
    return hue_to_rgb(p, q, h + 1/3),
           hue_to_rgb(p, q, h),
           hue_to_rgb(p, q, h - 1/3)
end

-- 밝기 조정
local function adjust_lightness(hex, direction, percentage)
    local r, g, b = hex_to_rgb(hex)
    if not r then return g end -- 오류 메시지 반환
    local h, s, l = rgb_to_hsl(r, g, b)

    -- 명도 조정
    if direction == "brighten" then
        l = math.min(l + percentage / 100, 0.95) -- 최대 95%로 제한
    elseif direction == "darken" then
        l = math.max(l - percentage / 100, 0.05) -- 최소 5%로 제한
    else
        return "방향은 'brighten' 또는 'darken'만 가능합니다."
    end

    -- 다시 RGB로 변환
    r, g, b = hsl_to_rgb(h, s, l)
    return string.format("#%02X%02X%02X", math.floor(r * 255), math.floor(g * 255), math.floor(b * 255))
end

-- 메인 함수
function p.adjust(frame)
    local hex = frame.args[1] or "#808080" -- 기본 색상
    local direction = frame.args[2] or "brighten" -- 기본 방향
    local percentage = tonumber(frame.args[3]:match("%d+")) or 0 -- 기본 퍼센트

    if percentage < 0 or percentage > 100 then
        return "퍼센트는 0%에서 100% 사이여야 합니다."
    end

    return adjust_lightness(hex, direction, percentage)
end

return p