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

편집 요약 없음
편집 요약 없음
18번째 줄: 18번째 줄:
end
end


-- 소수점 둘째 자리로 반올림하는 함수
-- 밝기 조정
local function round(num, numDecimalPlaces)
    local mult = 10^(numDecimalPlaces or 0)
    return math.floor(num * mult + 0.5) / mult
end
 
-- RGB 값 조정 함수
local function adjust_brightness(r, g, b, factor)
local function adjust_brightness(r, g, b, factor)
     local function adjust_value(value)
     local function adjust_value(value)
        -- 밝기 변경, RGB 값이 0에서 255 사이로 유지되도록 클램핑
         local adjusted = value * factor
         local adjusted = value * factor
         adjusted = math.max(0, math.min(255, adjusted))
         adjusted = math.max(0, math.min(255, adjusted))
         return round(adjusted, 0) -- 반올림
         return math.floor(adjusted + 0.5) -- 반올림
     end
     end


42번째 줄: 35번째 줄:
     local percentage = tonumber(frame.args[3]:match("%d+")) or 0
     local percentage = tonumber(frame.args[3]:match("%d+")) or 0


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


    -- 밝기 조정의 비율 설정
     local factor
     local factor
     if direction == "brighten" then
     if direction == "brighten" then
57번째 줄: 48번째 줄:
     end
     end


    -- 헥스 코드를 RGB로 변환
     local r, g, b, err = hex_to_rgb(hex)
     local r, g, b, err = hex_to_rgb(hex)
     if not r or not g or not b then
     if not r then
         return err
         return err
     end
     end


    -- 각 RGB 값에 대해 밝기 조정
     r, g, b = adjust_brightness(r, g, b, factor)
     r, g, b = adjust_brightness(r, g, b, factor)


    -- 결과로 헥스 코드 반환
     return rgb_to_hex(r, g, b)
     return rgb_to_hex(r, g, b)
end
end


return p
return p

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

이 모듈에 대한 설명문서는 모듈: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)
    local g = tonumber(hex:sub(3, 4), 16)
    local b = tonumber(hex:sub(5, 6), 16)
    return r, g, b
end

-- RGB에서 헥스 코드 생성
local function rgb_to_hex(r, g, b)
    return string.format("#%02X%02X%02X", r, g, b)
end

-- 밝기 조정
local function adjust_brightness(r, g, b, factor)
    local function adjust_value(value)
        local adjusted = value * factor
        adjusted = math.max(0, math.min(255, adjusted))
        return math.floor(adjusted + 0.5) -- 반올림
    end

    return adjust_value(r), adjust_value(g), adjust_value(b)
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

    local factor
    if direction == "brighten" then
        factor = 1 + (percentage / 100) -- 밝게 만들기
    elseif direction == "darken" then
        factor = 1 - (percentage / 100) -- 어둡게 만들기
    else
        return "방향은 'brighten' 또는 'darken'만 가능합니다."
    end

    local r, g, b, err = hex_to_rgb(hex)
    if not r then
        return err
    end

    r, g, b = adjust_brightness(r, g, b, factor)

    return rgb_to_hex(r, g, b)
end

return p