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

편집 요약 없음
편집 요약 없음
3번째 줄: 3번째 줄:
-- 헥스 코드에서 RGB 추출
-- 헥스 코드에서 RGB 추출
local function hex_to_rgb(hex)
local function hex_to_rgb(hex)
    -- 헥스 코드의 # 제거 및 유효성 검사
     hex = hex:match("^#?(%x%x%x%x%x%x)$")
     hex = hex:match("^#?(%x%x%x%x%x%x)$")
     if not hex then
     if not hex then
         return nil, "유효하지 않은 헥스 코드입니다. 6자리 색상 코드 (#RRGGBB)를 입력하세요."
         return nil, "유효하지 않은 헥스 코드입니다. 6자리 색상 코드 (#RRGGBB)를 입력하세요."
     end
     end
    -- RGB 값 추출
     local r = tonumber(hex:sub(1, 2), 16)
     local r = tonumber(hex:sub(1, 2), 16)
     local g = tonumber(hex:sub(3, 4), 16)
     local g = tonumber(hex:sub(3, 4), 16)
17번째 줄: 15번째 줄:
-- RGB에서 헥스 코드 생성
-- RGB에서 헥스 코드 생성
local function rgb_to_hex(r, g, b)
local function rgb_to_hex(r, g, b)
     return string.format("#%02X%02X%02X", math.floor(r), math.floor(g), math.floor(b))
     return string.format("#%02X%02X%02X", r, g, b)
end
end


-- RGB 값 조정 (정확한 반올림 포함)
-- 밝기 조정 함수
local function adjust_brightness(r, g, b, factor)
local function adjust_brightness(r, g, b, factor)
     -- 밝기 조정 및 반올림
     -- 밝기 조정 및 반올림 처리
     local function clamp_and_round(value)
     local function adjust_value(value)
         value = value * factor
         local adjusted = value * factor
         value = math.min(255, math.max(0, value)) -- 0~255 범위 제한
         adjusted = math.min(255, math.max(0, adjusted)) -- 0~255로 클램핑
         return math.floor(value + 0.5) -- 반올림
         return math.floor(adjusted + 0.5) -- 반올림
     end
     end


     return clamp_and_round(r), clamp_and_round(g), clamp_and_round(b)
     return adjust_value(r), adjust_value(g), adjust_value(b)
end
end


-- 메인 함수
-- 메인 함수
function p.adjust(frame)
function p.adjust(frame)
     local hex = frame.args[1] or "#808080" -- 기본값: 중간 회색
     local hex = frame.args[1] or "#808080"
     local direction = frame.args[2] or "brighten" -- 기본값: 밝게
     local direction = frame.args[2] or "brighten"
     local percentage = tonumber((frame.args[3] or "0"):match("%d+"))
     local percentage = tonumber((frame.args[3] or "0"):match("%d+"))


    -- 퍼센트값 유효성 검사
     if not percentage or percentage < 0 or percentage > 100 then
     if not percentage or percentage < 0 or percentage > 100 then
         return "퍼센트는 0%에서 100% 사이여야 합니다."
         return "퍼센트는 0%에서 100% 사이여야 합니다."
     end
     end


    -- 밝기 조정 방향 설정
     local factor = 1 + (percentage / 100)
     local factor = 1 + (percentage / 100)
     if direction == "darken" then
     if direction == "darken" then
51번째 줄: 47번째 줄:
     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 or not g or not b then
         return err -- 헥스 코드 오류 메시지 반환
         return err
     end
     end


    -- 밝기 조정 후 새로운 색상 반환
     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)

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

이 모듈에 대한 설명문서는 모듈: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.min(255, math.max(0, adjusted)) -- 0~255로 클램핑
        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] or "0"):match("%d+"))

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

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

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

    r, g, b = adjust_brightness(r, g, b, factor)
    return rgb_to_hex(r, g, b)
end

return p