Color3 converts to nearest Brick Color

Hi, i’m making a game where there is a save and load function, and colors are stored in Color3Values. But some colors don’t get saved because they get changed when inserted in game, which makes my save and load functions not work.

Here is the functions:

LOAD

local function getColor(colorCode)
	local colors = workspace.ColorValues
	if colorCode == 0 then
		return colors.Brown.Value --Brown
	elseif colorCode == 1 then 
		return colors.GrassGreen.Value --Grass Green
	elseif colorCode == 2 then 
		return colors.EarthGreen.Value --Earth Green
	elseif colorCode == 3 then 
		return colors.StoneGray.Value --Stone Gray
	elseif colorCode == 4 then 
		return colors.BrightRed.Value --Bright Red
	elseif colorCode == 5 then 
		return colors.BrightOrange.Value --Bright Orange
	elseif colorCode == 6 then 
		return colors.BrightYellow.Value --Bright Yellow
	elseif colorCode == 7 then 
		return colors.BrightGreen.Value --Bright Green
	elseif colorCode == 8 then 
		return colors.BrightBlue.Value --Bright Blue
	elseif colorCode == 9 then 
		return colors.BrightViolet.Value --Bright Violet
	elseif colorCode == 10 then
		return colors.White.Value --White
	elseif colorCode == 11 then
		return colors.Black.Value --Black
	elseif colorCode == 12 then
		return colors.Pink.Value --Pink
	elseif colorCode == 13 then
		return colors.CottonPink.Value --CottonPink
	elseif colorCode == 14 then
		return colors.CottonBlue.Value --CottonBlue
	elseif colorCode == 15 then
		return colors.LightBrown.Value
	elseif colorCode == 16 then
		return colors.Turquoise.Value
	elseif colorCode == 17 then
		return colors.PumpkinOrange.Value
	elseif colorCode == 18 then
		return colors.Red.Value
	elseif colorCode == 19 then
		return colors.Green.Value
	elseif colorCode == 20 then
		return colors.Blue.Value
	elseif colorCode == 21 then
		return colors.Bread.Value
	elseif colorCode == 22 then
		return colors.BluishGreen.Value
	elseif colorCode == 23 then
		return colors.Yellow.Value
	else
		return colors.StoneGray.Value
	end

end

SAVE

local function convertColor(colorCode)
	local colors = workspace.ColorValues
	if colorCode == colors.Brown.Value then
		return 0 --Brown
	elseif colorCode == colors.GrassGreen.Value then 
		return 1 --Grass Green
	elseif colorCode == colors.EarthGreen.Value then 
		return 2 --Earth Green
	elseif colorCode == colors.StoneGray.Value then 
		return 3 --Stone Gray
	elseif colorCode == colors.BrightRed.Value then 
		return 4 --Bright Red
	elseif colorCode == colors.BrightOrange.Value then 
		return 5 --Bright Orange
	elseif colorCode == colors.BrightYellow.Value then 
		return 6 --Bright Yellow
	elseif colorCode == colors.BrightGreen.Value then 
		return 7 --Bright Green
	elseif colorCode == colors.BrightBlue.Value then 
		return 8 --Bright Blue
	elseif colorCode == colors.BrightViolet.Value then 
		return 9 --Bright Violet
	elseif colorCode == colors.White.Value then
		return 10 --White
	elseif colorCode == colors.Black.Value then
		return 11 --Black
	elseif colorCode == colors.Pink.Value then
		return 12 --Pink
	elseif colorCode == colors.CottonPink.Value then
		return 13 --CottonPink
	elseif colorCode == colors.CottonBlue.Value then
		return 14 --CottonBlue
	elseif colorCode == colors.LightBrown.Value then
		return 15 --LightBrown
	elseif colorCode == colors.Turquoise.Value then
		return 16
	elseif colorCode == colors.PumpkinOrange.Value then
		return 17
	elseif colorCode == colors.Red.Value then
		return 18
	elseif colorCode == colors.Green.Value then
		return 19
	elseif colorCode == colors.Blue.Value then
		return 20
	elseif colorCode == colors.Bread.Value then
		return 21
	elseif colorCode == colors.BluishGreen.Value then
		return 22
	elseif colorCode == colors.Yellow.Value then
		return 23
	else
		return 3
	end
end

SELECTING COLOR

local scrollingFrame = script.Parent.ScrollingFrame
local player = script.Parent.Parent.Parent.Parent.Parent
for _, v in pairs(scrollingFrame:GetChildren()) do
	v:SetAttribute("ValueActive", false)
	local color = workspace.ColorValues:FindFirstChild(v.Name).Value
	v.UIText:Destroy()
	v.BackgroundColor3 = color
	v.MouseButton1Click:Connect(function()
		if v:GetAttribute("ValueActive") == false then
			print("Choosing Color: "..v.Text)
			v:SetAttribute("ValueActive", true)
			workspace.Settings[player.Name.."_Settings"].Color.Value = color
			v:SetAttribute("ColorText", v.Text)
			v.Text = "✅"
			task.wait(1)
			v.Text = v:GetAttribute("ColorText")
			v:SetAttribute("ValueActive", false)
		end
	end)
end

SETTING PART COLOR

shape.Color = workspace.Settings[player.Name.."_Settings"].Color.Value

The BrickColor.new constructor accepts a Color3 value as an argument, which will return the closest BrickColor to the Color3:

local color3 = Color3.new(0, 1, 1)

local brickColor = BrickColor.new(color3)
print(brickColor) -- Will print: Toothpaste

print(brickColor.Name) -- Will also print Toothpaste, but the type of the value is now a string instead of a BrickColor

You can then save the BrickColor’s name to the DataStore in-order to save its value as a string :slight_smile::+1:


@AntonAKAO

By the way, I’d recommend converting Color3 values into a hex color value if you wish to save/store them as a string, since converting them into a BrickColor will loose a lot of the range of colors that Color3 values are able to provide. Here’s how you can do so:

local color3Value = Color3.new(0, 1, 1)

local color3AsHex = color3Value:ToHex() -- :ToHex() method converts a Color3 value into a hex color string value
print(color3AsHex)

print(Color3.fromHex(color3AsHex)) -- Color3.fromHex returns a Color3 value from a hex color string value

There’s also a third option available that uses buffers as an efficient way of storing a Color3 value:

local function color3ToBuffer(color3: Color3): buffer
	local color3Buffer = buffer.create(12) -- 32 bits * 3 = 96 bits, and 96 bits in bytes is 12, since 8 bits is 1 byte
	buffer.writef32(color3Buffer, 0, color3.R)
	buffer.writef32(color3Buffer, 4, color3.G)
	buffer.writef32(color3Buffer, 8, color3.B)

	return color3Buffer
end

local function bufferToColor3(color3Buffer: buffer): Color3
	return Color3.new(
		buffer.readf32(color3Buffer, 0),
		buffer.readf32(color3Buffer, 4),
		buffer.readf32(color3Buffer, 8))
end

local color3 = Color3.new(0.25, 0.5, 1)

local color3Buffer = color3ToBuffer(color3)
print(color3Buffer)

print(bufferToColor3(color3Buffer))

Although this method works best if you store multiple Color3 values inside of one buffer, rather than creating a new buffer for each different value. It’s also less safe to use than the methods above if you aren’t careful with reading from the correct memory offsets

1 Like

It works! I made the save and load functions use brickcolor.new() instead of color3, and i also made setting and placing colors use brickcolor.new() .

Examples:

elseif colorCode == BrickColor.new(colors.CottonBlue.Value) then
		return 14 --CottonBlue
elseif colorCode == 14 then
		return BrickColor.new(colors.CottonBlue.Value)
workspace.Settings[player.Name.."_Settings"].Color.Value = BrickColor.new(color)
1 Like

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