# Custom Lerp Function Question:

Just a Reminder: I’m already aware of `Color3:Lerp()`, `CFrame:Lerp()`, and `Vector3:Lerp()`.

Hello,

So Currently at the moment, I am Attempting to make my own Lerp function that I can use to make things easier for work.

I am using the Lerp Formula which If I remember correctly its: Lerp{a, b, t} = a + (b - a) * t

But The Question I’m asking is how can I optimize this custom lerp function to work for stuff like `Color3` and `Vector3`. I dont really intend to use it for `CFrame` but if you want you can help with that

And If you were wondering, I’m just asking on how I should be doing it as in explaining it or at least some assistance to get me close to there, I’m not asking for code to do it.
Here is the code btw:

``````local function lerp(a, b, t) -- Lerp Args
return a + (b - a) * t -- Lerp Formula
end

local full = Color3.new(0,.7,1) -- Full Color
local empt = Color3.new(0,0,.3) -- Empty Color

print(lerp(empt, full, .1/1))
-- Attempting to Lerp a Color3, normally it wont work, so how could I optimize the code to work with it?
``````

Anything Helps, (but Script at Least)

thanks

1 Like

To make the Lerp function work for both color3 and vector3. You need to check if A is a color3 value, if it’s not and it’s a vector value, then do an else statement and do the regular formula. But I’d suggest making 2 separate functions instead of integrating both. Anyways, to make color3 work in the lerp function. You need to do the formula on each coordinate (eg: r, g, b values) separately. The formula in code: (Don’t click on it if you want to figure it without any help.)

``````local function Lerp(a, b, t)
return a.R + (b.R - a.R) * t, a.G + (b.G - a.G) * t, a.B + (b.B - a.B) * t
end
``````
1 Like

Thanks, thats what I was asking about, And about the Spoiler, I already know how to do it, I was wondering if I could integrate them both and to do it efficiently, if that makes sense.

Sorry if I was a bit misleading

To integrate them both would be just doing some if statements to see if they were vectors or not. It cannot be automatic due to them having extremely different property names (tell me if I’m wrong).
Here’s the code to integrate them both (if you need it):

``````local function Lerp(a, b, t)
if typeof(a) == "Color3" then
return a.R + (b.R - a.R) * t, a.G + (b.G - a.G) * t, a.B + (b.B - a.B) * t
elseif typeof(a) == "Vector3" then
return a + (b - a) * t
end
end
``````
1 Like

Just a question, why are you trying to create your own Lerp function?

I want it to be Multi Purpose, instead of me using separate functions.

Thanks, but I guess I could do this instead:

``````local function Lerp(a,b,t)
if typeof(a) == "Color3" then -- if its a Color3
return Color3.new(a.R + (b.R - a.R) * t,a.G + (b.G-a.G) * t,a.B + (b.B-a.B) * t)
end
return a + (b-a) * t -- if none are met, the Default Method
end
``````

or instead of me calling `typeof` multiple times:

``````local function Lerp(a,b,t)
local var = typeof(a)

if var == "Color3" then
return Color3.new(a.R + (b.R - a.R) * t,a.G + (b.G-a.G) * t,a.B + (b.B-a.B) * t)
end
return a + (b-a) * t
end
``````

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.