Changing Color based off range

I’m trying to make a bar get darker as the value gets higher, but I lack all of the knowledge to do so. I’ve found some community resources like this one:

This works perfectly but its the opposite of what I want to do. It gets redder as the value gets lower, as intended. But how do I achieve the opposite of this effect?

My current code:

StructureBar.BackgroundColor3 = Color3.fromHSV((Structure/MaxStructure)*0.1344, 0.9328, 0.9333)

That makes it go from this color
image
To this once it’s low.
image

I’d like it to go from yellow when it’s lower in value, to the red when it’s higher in value.

Yellow HSV = 0.1344, 0.9328, 0.9333
Yellow Color3 = 238, 195, 16

Red HSV = 0.0155, 1, 0.9294
Red Color3 = 237, 22, 0

Just perform the opposite operation on the other two, subtraction instead of multiplication, so it reduces instead of growing.

StructureBar.BackgroundColor3 = Color3.fromHSV((Structure/MaxStructure)*0.1344, 
0.9328*(MaxStructure - Structure), 
0.9333*(MaxStructure - Structure)

2 Likes

That’s what I thought too, but it just turns the bar into a strobe light with a bunch of colors
image

Roblox accually has an API for this in the DevHub docs. Use the Color3:lerp() function.

In your case it would look like

local yellowColor = Color3.fromHSV(0.1344, 0.9328, 0.9333)
local redColor = Color3.fromHSV(0.0155, 1, 0.9294)

StructureBar.BackgroundColor3 = yellowColor:lerp(redColor, (Structure / MaxStructure))

(I’m assuming Structure is the length of the bar and MaxStructure is the max length of the bar)

2 Likes

You can lerp colours.

For example, you can simply lerp the colour depening on the X Scale value

Edit: @scotch101tape already has said this

1 Like

You assumed right! Thank you, I guess I didn’t do enough searching.

1 Like