Is there an efficient way to get character's health color?

By health color, I mean this.

As you know, the health gradually goes from green > orange > red. Is there an efficient way to get the specific colors of those instead of just making it have 3 colors?

1 Like

Nevermind, solved it. I didn’t realize that the hue goes from 100 to 0 (green to red)

local color = Color3.fromHSV((Humanoid.Health/255), 1, 1)
4 Likes

I put together another method uses ColorSequence, and a modification of the example for NumberSequence since neither have an interpolate method. This one should work for any sequence of colors.

local Sequence = ColorSequence.new({
	ColorSequenceKeypoint.new(0,   Color3.new(1, 0, 0)), -- red
	ColorSequenceKeypoint.new(0.5, Color3.new(1, 1, 0)), -- yellow
	ColorSequenceKeypoint.new(1,   Color3.new(0, 1, 0))  -- green
})

function evalCS(cs, time)
	-- If we are at 0 or 1, return the first or last value respectively
	if time == 0 then return cs.Keypoints[1].Value end
	if time == 1 then return cs.Keypoints[#cs.Keypoints].Value end
	-- Step through each sequential pair of keypoints and see if alpha
	-- lies between the points' time values.
	for i = 1, #cs.Keypoints - 1 do
		local this = cs.Keypoints[i]
		local next = cs.Keypoints[i + 1]
		if time >= this.Time and time < next.Time then
			-- Calculate how far alpha lies between the points
			local alpha = (time - this.Time) / (next.Time - this.Time)
			-- Evaluate the real value between the points using alpha
			return this.Value:Lerp(next.Value, alpha)
		end
	end
end

print(evalCS(Sequence, 0.75)) -- would be 0.5, 1, 0 or green-ish
print(evalCS(Sequence, 0.31419)) -- would be 1, 0.62838, 0, or light orange-ish
2 Likes