How would I get RGB from Color3

I’m basically just wanting to get the RGB from a parts BackgroundColor3 and print it but for some reason this only prints 0,0,0.

This is what I have so far.

	local BGColor = part.BackgroundColor3
	print(Color3.fromRGB(child.BackgroundColor3))

Color3 by default prints in Color3.new() form.

If you wish to print the RGB version, you’d do something more like this.

local childBGColor = child.BackgroundColor3
print(childBGColor.R* 255 ..", "..childBGColor.G * 255 ..", "..childBGColor.B * 255)

This will not be 100% accurate due to floating point errors, you will likely need to round.

4 Likes

Ah I see that’s great I did see something about that. How would I round it?

You can use the math.round function from Roblox to round these RGB values to what you’d like after you scale them by 255 as @pwrcosmic explained.

local function c3ToRGB(c3: Color3): {R: number, G: number, B: number}
  return {
    R = math.floor(c3.R * 255)
    G = math.floor(c3.G * 255)
    B = math.floor(c3.B * 255)
  }
}
2 Likes

It’s also doable with the ToHex if you want to avoid rounding:

local function color3ToRGB(color)
	if typeof(color) == 'Color3' then
		local hex = color:ToHex()
		
		local r = tonumber('0x'..string.sub(hex,1,2))
		local g = tonumber('0x'..string.sub(hex,3,4))
		local b = tonumber('0x'..string.sub(hex,5,6))
		
		return r, g, b
	end
end

--[[
	Example:
	local part = workspace.Baseplate
	print(color3ToRGB(part.Color))        -- 99 95 98

	local r,g,b = color3ToRGB(part.Color)
	print(r,g,b)         -- 99 95 98
]]

why are you making a color3 from a color3

1 Like

Color3.R, Color3.G, Color3.B in order to get the RGB components of a “Color3” value.

Why are you using floor instead of round?

1 Like