How to convert a HSV color to RGB color?

As the title suggests, how can I do this? I tried searching for answers but nothing had worked. Including multiplying it by 255.

local hsv

local function updateColour(centreOfWheel)
	local colourPickerCentre = Vector2.new(
		colourWheel.Picker.AbsolutePosition.X + (colourWheel.Picker.AbsoluteSize.X/2),
		colourWheel.Picker.AbsolutePosition.Y + (colourWheel.Picker.AbsoluteSize.Y/2)
	)
	local h = (math.pi - math.atan2(colourPickerCentre.Y - centreOfWheel.Y, colourPickerCentre.X - centreOfWheel.X)) / (math.pi * 2)
	local s = (centreOfWheel - colourPickerCentre).Magnitude / (colourWheel.AbsoluteSize.X/2)
	local v = math.abs((darknessSlider.AbsolutePosition.Y - darknessPicker.AbsolutePosition.Y) / darknessPicker.AbsoluteSize.Y - 1)

	hsv = Color3.fromHSV(math.clamp(h, 0, 1), math.clamp(s, 0, 1), math.clamp(v, 0, 1))
	
	colourDisplay.ImageColor3 = hsv
	darknessPicker.UIGradient.Color = ColorSequence.new{
		ColorSequenceKeypoint.new(0, hsv), 
		ColorSequenceKeypoint.new(1, Color3.new(0, 0, 0))
	}
end
2 Likes

???

You can do it manually too, HEX is just 3 base16 numbers. You split the HEX string into three pairs, turn each pair back into base10 with tonumber(number, 16) which will give you a number between 0-255

2 Likes

Apologies, I meant to say HSV, I just got confused.

1 Like

same thing. All of the functions you need are provided by the Color3 datatype.

Can you provide some example code?

Try using the debugger to step through your code line by line and see that your h s and v are all what you expect.

I did, but it wasn’t accurate for some reason.

This post should help you Here.

Please read my entire post, I already explained that multiplying the HSV color by 255 did not work.

I’m not quite sure what you mean. Anyways, if you just print(h, s, v) are your values what you expect?

Maybe your problem is just that you’re using the final value for the slider color when you really should be using ColorSequenceKeypoint.new(0, Color3.fromHSV(math.clamp(h, 0, 1), math.clamp(s, 0, 1), 1))

Relatively simple, you just construct a Color3 value using the ‘fromHex’ constructor function and then construct a new Color3 value using the ‘new’ constructor function.

local function FromHsvToRgb(HSVColor : HSVColor) : Color3
	return Color3.new(HSVColor.R, HSVColor.G, HSVColor.B)
end

local RGBColor = FromHsvToRgb(Color3.fromHSV(0, 0, 1)) --White.
print(RGBColor.R, RGBColor.G, RGBColor.B) --1, 1, 1 (as expected).

You technically don’t need to construct a second Color3 datatype value you can just index the first Color3 datatype value’s ‘R’, ‘G’ and ‘B’ properties to fetch the values of those channels.

1 Like