Interpolating from Green to Red for GUI Healthbar

I’m trying to achieve something more mathematical than scripting based, but it is with scripts. I want to do a proper transition from the Green Hue to the Red Hue of a health bar based on the health percent. The linear formula I have for that is this:

local function AdjustColor(healthPercent)
	local H, S, V = HealthLabel.BackgroundColor3:ToHSV()
	HealthLabel.BackgroundColor3 = Color3.fromHSV((0+120*healthPercent)/350, S, V)
end

The transition is fine in a linear setup like this. But I wanted to try a better transition where it builds towards being red longer. I figured something like exponential interpolation or something similar may work, but being less inclined with math I don’t know how to do that and was hoping to get some help building that equation with this setup.

To anyone who has tried to build a color changing health bar it would be great if there was a standardized way to interpolate the hue value that could be passed along to me.

Does this work?

local function AdjustColor(healthPercent)
	local H, S, V = HealthLabel.BackgroundColor3:ToHSV()
	HealthLabel.BackgroundColor3 = Color3.fromHSV((0+120*(healthPercent^2))/350, S, V)
end
1 Like

I couldn’t understand your problem well…
Anyways if this might help, here is the method I’m using to do a color changing health bar:

Color3.fromHSV((CurrentHealth/Humanoid.MaxHealth)*0.28, 1, 0.9)
1 Like

the easiest way realistically is just to have the green color you want, and the red color, then interpolate it from red to green or vice versa using an interpolation formula.

or get your color in terms of a Vector3 and just lerp the entire thing using the Vector3:Lerp() function

1 Like

I was thinking of this but was like “There’s no way it’s as simple as throwing in an exponent” after reading several pages on exponential interpolation.

With programming math it really is the simplest solution sometimes.

For all others, if you do linear interpolation, you won’t hit the deepest shade of red until your health is at about a 1%. But you really want it to be a deep shade of red at 25%, not just an orange.

An exponent will help you reach that deeper shade of red at a more appropriate percentage of health missing.

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