local addonName, LRH = ...

LRH = LRH or {}
LRH.Utils = LRH.Utils or {}

local U = LRH.Utils

function U.Clamp(v, lo, hi)
    if v < lo then return lo end
    if v > hi then return hi end
    return v
end

function U.EnsureColorTable(t, dr, dg, db)
    if type(t) ~= "table" then return { r = dr, g = dg, b = db } end
    if type(t.r) ~= "number" or type(t.g) ~= "number" or type(t.b) ~= "number" then
        return { r = dr, g = dg, b = db }
    end
    return t
end

function U.GetScale()
    return UIParent:GetEffectiveScale() or 1
end

function U.GetPixel()
    return 1 / U.GetScale()
end

function U.PxToUI(px)
    return px / U.GetScale()
end

function U.UIToPx(ui)
    return ui * U.GetScale()
end

function U.SnapPx(v, snap)
    snap = snap or 16
    return math.floor(v / snap + 0.5) * snap
end

function U.SnapUI(v)
    local px = U.GetPixel()
    return math.floor(v / px + 0.5) * px
end

function U.RoundPx(v)
    return math.floor(v + 0.5)
end

function U.EvenPx(v)
    local n = U.RoundPx(v)
    if (n % 2) ~= 0 then
        n = n + 1
    end
    return n
end

-- Rendering Fix für skalierende Texturen
function U.FixTexture(tex)
    if not tex then return end
    if tex.SetSnapToPixelGrid then
        tex:SetSnapToPixelGrid(false)
    end
    if tex.SetTexelSnappingBias then
        tex:SetTexelSnappingBias(0)
    end
end