Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How would you convert a String value into a Color3 value?

Hello I am trying to make a script that takes user input for an RGB color code and turns it into the text color for a SurfaceGUI. The user input is represented in three different text boxes. One is for the red value, one is for the green value, and the other is for the blue value. The issue I am running into is the fact that my output is false. This is what I currently have for the script (it is a LocalScript):

Submit = script.Parent.Submit
RedInput = script.Parent.Red.Text
GreenInput = script.Parent.Green.Text
BlueInput = script.Parent.Blue.Text
Red = tonumber(RedInput)
Green = tonumber(GreenInput)
Blue = tonumber(BlueInput)


Submit.MouseButton1Click:connect(function()
	local player = game.Players.LocalPlayer
	local booths = workspace.under:GetChildren()
		for i,v in pairs(booths) do
			print(v.upforgrabs.Value == true)
			if v.owner.Value == player.Name then
				local TCInput = Color3.fromRGB(Red, Green, Blue)
				v.Screen.SurfaceGui.Frame.UserText.TextColor3 = TCInput
				v.Screen.SurfaceGui.Frame.OwnershipStatus.TextColor3 = TCInput
			else
				assert("WHOOPS")
			end
		end
	script.Parent.Visible = false
	script.Parent.Parent.BoothEdit.Visible = true
end)

-- local a,b,c = string.match("255, 0, 0", "(%d+),* *(%d+),* *(%d+),* *")
-- local color = Color3.new(a,b,c)

In addition, this script is located in StarterGui.ScreenGui.TCF.

1 answer

A simple solution would be to parse the string.

Lets say that your string value separated each color3 value with a comma ,. An example string that could represent a color3 with it's r, g, and b values equal to 1, 2, and 3 could look like so "1, 2, 3". Or you could simply separate each number with spaces like so "1 2 3". Or you could separate the numbers however you like, but for my example I will be using commas to separate each number.

In the following code example I will set the value of an imaginary StringValue to the contents of a color3, then parse it and make a new color3 based off of those values:

local function ParseForColor3(str) --returns a tuple of 3 integers, the r g and b in the string.
	if (str):find("%d+%s*,%s*%d+%s*,%s*%d+") then
		local s, e, = (str):find("%d+%s*,%s*%d+%s*,%s*%d+");
		local sub = (str):sub(s, e);
		local r, g, b;
		local a, b = (str):find("%d+%s*,");
		local r1 = (str):sub(a, b);
		local c, d = (str):find("%d+%s*,", b);
		local g1 = (str):sub(c, d);
		local e, f = (str):find("%s*%d+", d);
		local b1 = (str):sub(e, f);
		r = tonumber((r1):sub((r1):find("%d+")));
		g = tonumber((g1):sub((g1):find("%d+")));
		b = tonumber((b1):sub((b1):find("%d+")));
		return r, g, b;
	end
end
local my_stringValue = game.ServerStorage.MyStringValue;
local myColor3 = Color3.new(100, 10, 35);
my_stringValue.Value = (myColor3.r..", "..myColor3.g..", "..myColor3.b);
local newCol3 = Color3.new(ParseForColor3(my_stringValue.Value));

And once you get the color3, it is easy to place it into a Color3Value object :)

I hope this helped.

This discussion is preserved as it was. New answers, comments, and votes are closed.