local flMinDist = 100
local flMaxDist = 10000
local cMin = Color(255, 0, 0)
local cMax = Color(0, 255, 0)
local function LerpColor(t, cFrom, cTo)
local r, g, b, a = cFrom.r, cFrom.g, cFrom.b, cFrom.a
-- from + (to - from) * percentage
return Color(
r + (cTo.r - r) * t,
g + (cTo.g - g) * t
b + (cTo.b - b) * t
a + (cTo.a - a) * t)
end
---------------------------- Inside function ----------------------------
local flDist = ... -- Whatever distance determination here
local t -- Lerp percentage
-- Could also use math.Clamp on the fractional calculation
-- But this is more efficient
if (flDist <= flMinDist) then
t = 0
elseif (flDist >= flMaxDist) then
t = 1
else
-- t is calculated by subtracting the minimum distance from the top and bottom of a fraction of flDist/flMaxDist
-- This will give a number 0-1
t = (flDist - flMinDist) / (flMaxDist - flMinDist)
end
-- This is your gradient
local cLerp = LerpColor(t, cMin, cMax)