Color based off value

Howdy!

I want to base a parts saturation on a value. The code below does that but I would like to cap the max saturation to the value the part starts with.

local part = workspace.Part

local MaxHealth = 100

function updateColor()
	local _hue, _sat, _val = part.Color:ToHSV()

	_val = part:GetAttribute('Health')/MaxHealth
	
	
	part.Color = Color3.fromHSV(_hue, _sat, _val)
end

updateColor()

part:GetAttributeChangedSignal('Health'):Connect(updateColor)

The block on the left is the color I want when the value is 100%.
The block on the right is what it changes to after running the code. - Not the color I want.

You can use math.clamp(number, min, max) to achieve this.

local part = workspace.Part

local MaxHealth = 100

local MAX_HSV_VALUE = 80 -- Example

function updateColor()
	local _hue, _sat, _val = part.Color:ToHSV()

	_val = math.clamp((part:GetAttribute('Health') / MaxHealth), 0, MAX_HSV_VALUE)
	
	part.Color = Color3.fromHSV(_hue, _sat, _val)
end

updateColor()

part:GetAttributeChangedSignal('Health'):Connect(updateColor)
1 Like

Can we get the current saturation and make that the max instead of manually setting the MAX_HSV_VALUE variable?

Okay I figured that out. I grabbed the HSV beforehand and set the max to that value.

local _hue, _sat, _val = part.Color:ToHSV()
local MAX_HSV_VALUE = _val

Thank you so much!

1 Like

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