I want to sort numbers on a red to green gradient. Like a function returns color code based on input value you typed.
I want this for a distance system. For example, if the distance between x,y is >= 10000 it will return full red; if it is <= 100 then it will return full green.
I can do it with if elseif cases but I want it to be smoother. I want it to include more colors. Any idea?
[code]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)[/code]
[QUOTE=code_gs;52639988][code]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)[/code][/QUOTE]
thank you..
Sorry, you need to Log In to post a reply to this thread.