Converting HSV to RGB?

Was saving colors, and since I had to reduce them to one number, instead if a vector3/color3, for some reason it converted it into HSV instead of staying as RGB, and I’m just looking for a way in which to convert HSV into RGB, without having to manually do it which is what most of the things I saw said to do.

So whats the best way to do it without having to do a lot of math?

4 Likes

Hey there, it’s pretty simple to do:

local color = Color3.new(255, 0, 0)
local h,s,v = color:ToHSV()

Should work just fine

1 Like

that’s converting RGB to HSV, and a saw that method, but the issue is I need the opposite of that, which is HSV to RGB.

1 Like

Oh, I apologize, I’ve read that wrong. I don’t know any other method either then.

Try this:

function hsvToRgb(h, s, v)
	local r, g, b

	local i = math.floor(h * 6)
	local f = h * 6 - i
	local p = v * (1 - s)
	local q = v * (1 - f * s)
	local t = v * (1 - (1 - f) * s)

	i = i % 6

	if i == 0 then r, g, b = v, t, p
	elseif i == 1 then r, g, b = q, v, p
	elseif i == 2 then r, g, b = p, v, t
	elseif i == 3 then r, g, b = p, q, v
	elseif i == 4 then r, g, b = t, p, v
	elseif i == 5 then r, g, b = v, p, q
	end
	
	return Color3.fromRGB(r * 255, g * 255, b * 255)
end

local Color = Color3.fromHSV(0.5155, 0.337255, 1)
local New = hsvToRgb(Color:ToHSV())
print(New)

Source: columns/color.lua at master · EmmanuelOga/columns · GitHub (last function)

2 Likes

Do Color3.fromHSV(h, s, v) where h, s, and v are in the range [0, 1]. You can get the R G and B components from the R G and B properties of the returned Color3. These will also be in the [0, 1] range.

1 Like

Did anyone ever figure it out? i kinda need to know how to do it I’ve tried previous examples with nothing working

Yes, I figured it out.

local color = Color3.fromHSV(.5,.5,.5) -- or any random HSV color
-- multiply the components by 255 and get the nearest number
local r,g,b = math.floor((color.R*255)+0.5),math.floor((color.G*255)+0.5),math.floor((color.B*255)+0.5)
print(r,g,b) -- voila!
8 Likes

you can also just do math.round(), rather then using floor and adding 0.5

2 Likes

I didn’t realize they added that already, thanks!