generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Color3, converting to string and matching

Previous Thread :: Next Thread 
filiptibell is not online. filiptibell
Joined: 10 Mar 2013
Total Posts: 2362
04 Mar 2015 09:07 AM
Lets say I have this function to turn a Color3 value into a string:

Color3ToString = function(Color)
return "Color3("..tostring(Color.r)..","..tostring(Color.g)..","..tostring(Color.b)..")"
end

This would turn Color3.new(0.5, 0.75, 1) into "Color3(0.5,0.75,1)"
How would I match the 3 numbers inside the parentheses and get them as a tuple?
What I've got so far is this, which gets me "(0.5,0.75,1)":

StringToColor3 = function(String)
return String:match("%b()")
end

~The herp lerped a derp~
Report Abuse
NovusTheory is not online. NovusTheory
Joined: 14 Oct 2009
Total Posts: 4532
04 Mar 2015 09:15 AM
http://wiki.roblox.com/index.php?title=Function_dump/String_manipulation


CentOS-Web = /var/www/html/
Report Abuse
filiptibell is not online. filiptibell
Joined: 10 Mar 2013
Total Posts: 2362
04 Mar 2015 09:21 AM
I've already read the wiki page on pattern matching, and I obviously don't need a tutorial on string manipulation.
I want to know how to return a tuple value by matching a string such as "0.5,0.75,1". I can't figure it out.

~The herp lerped a derp~
Report Abuse
NovusTheory is not online. NovusTheory
Joined: 14 Oct 2009
Total Posts: 4532
04 Mar 2015 09:24 AM
Color3.new(String)

...


CentOS-Web = /var/www/html/
Report Abuse
NovusTheory is not online. NovusTheory
Joined: 14 Oct 2009
Total Posts: 4532
04 Mar 2015 09:25 AM
Well, I guess maybe that may not work. You may want to get the individual colors...

String Manipulation is all I need to say


CentOS-Web = /var/www/html/
Report Abuse
filiptibell is not online. filiptibell
Joined: 10 Mar 2013
Total Posts: 2362
04 Mar 2015 09:28 AM
Color3.new willnot accept a string as an argument, it will just return the same as Color3.new(), without any arguments. All you need to say is either:
A: Something that will actually help me
or
B: Nothing.

~The herp lerped a derp~
Report Abuse
protectiveEBob is not online. protectiveEBob
Joined: 09 Jul 2012
Total Posts: 1297
04 Mar 2015 09:29 AM
Two things:
print(tostring(Color3.new(1,1,1)))
And you can compare Color3s without doing that.
clr1=Color3.new(1,1,1)
clr2=Color3.new(.5,.75,1)
if clr1.r>clr2.r then print("hi") end
Report Abuse
filiptibell is not online. filiptibell
Joined: 10 Mar 2013
Total Posts: 2362
04 Mar 2015 09:32 AM
@ protectiveEBob

Didn't know tostring() would accept ROBLOX's custom classes like Color3. Thanks.
And what do you mean by comparing? I'm not trying to compare Color3 values, I'm trying to convert it into a string and back.

~The herp lerped a derp~
Report Abuse
protectiveEBob is not online. protectiveEBob
Joined: 09 Jul 2012
Total Posts: 1297
04 Mar 2015 09:46 AM
Let me try this again
Str="0.5,0.75,1"
Pattern="%d%.?%d?"
for i=1,3 do
A=string.sub(string.find(Str:match(Pattern)))
If A then--remove that part of the string

Keep in mind, that was my first attempt at patterns.
Report Abuse
nstrike159 is not online. nstrike159
Joined: 13 Dec 2012
Total Posts: 123
04 Mar 2015 09:55 AM
Loca BC=Color3.new(BrickColor.new(string).Color)
Report Abuse
protectiveEBob is not online. protectiveEBob
Joined: 09 Jul 2012
Total Posts: 1297
04 Mar 2015 09:57 AM
^ would be the easiest.
Report Abuse
filiptibell is not online. filiptibell
Joined: 10 Mar 2013
Total Posts: 2362
04 Mar 2015 09:57 AM
Didnt work. :(
Does anyone know how to do something like this? Returning all the values with just one match?:

local Pattern = ""
local R, G, B = ColorString:match(Pattern)

~The herp lerped a derp~
Report Abuse
epicbreaker is not online. epicbreaker
Joined: 23 Apr 2011
Total Posts: 2791
04 Mar 2015 09:59 AM
Wouldn't this work?
local R, G, B = Color.r, Color.g, Color.b
Report Abuse
filiptibell is not online. filiptibell
Joined: 10 Mar 2013
Total Posts: 2362
04 Mar 2015 10:01 AM
@epicbreaker

No, because what I need to do is get the 3 values from a string such as this "0.5,0.75,1", not an already existing Color3 value. I will use the R, G, and B values to later on create a Color3 value.

~The herp lerped a derp~
Report Abuse
filiptibell is not online. filiptibell
Joined: 10 Mar 2013
Total Posts: 2362
04 Mar 2015 10:04 AM
Has anyone here created custom admin commands where all the arguments get put in a table?
I imagine that would be pretty much the same thing as I'm trying to do...
(ex. "command argument argument2" would be {"command", "argument", "argument2"}

~The herp lerped a derp~
Report Abuse
nomer888 is not online. nomer888
Joined: 13 Feb 2010
Total Posts: 551
04 Mar 2015 10:06 AM
If you want to return an actual tuple object, try this:

local function GetTuple(str)
local output = {}
for item in str:gmatch('[^%s,]+') do
output[#output + 1] = item
end
return unpack(output)
end

local r, g, b = GetTuple(tostring(Color3.new(1, 2, 3)))
print(b, g, r) --> 3 2 1
Report Abuse
filiptibell is not online. filiptibell
Joined: 10 Mar 2013
Total Posts: 2362
04 Mar 2015 10:13 AM
@nomer888

Exactly what I was looking for!
Thank you so much :D

~The herp lerped a derp~
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
04 Mar 2015 10:14 AM
local Red, Green, Blue = String:match("^(.+?), ?(.+?), ?(.+?)$")
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
04 Mar 2015 10:16 AM
Oh, his code works on multiple lengths of tuples. Cool. I thought it was just excessive.

I really thought it was weird how nobody gave string.match until me, when I thought it was super obv. :(
Report Abuse
filiptibell is not online. filiptibell
Joined: 10 Mar 2013
Total Posts: 2362
04 Mar 2015 10:18 AM
@JarodOfOrbiter

Thanks for trying to help anyway, I appreciate it :P

~The herp lerped a derp~
Report Abuse
lax0131 is not online. lax0131
Joined: 20 Nov 2008
Total Posts: 132
04 Mar 2015 10:22 AM
Try ALL of these suggestions
Report Abuse
anaIyze is not online. anaIyze
Joined: 29 May 2014
Total Posts: 2048
04 Mar 2015 12:42 PM
string.format(Color3.new(^%d,+^%d,+^%d));
Report Abuse
AgentFirefox is not online. AgentFirefox
Top 100 Poster
Joined: 20 Jun 2008
Total Posts: 22404
04 Mar 2015 01:18 PM
local ColorStr = "Color(0.2, 0.5555555555555, 0.625)"
local r, g, b = ColorStr:match("(%d+%.?%d*)%s*,%s*(%d+%.?%d*)%s*,%s*(%d+%.?%d*)")
print(r, g, b)


Let's look at the pattern for individual color values.

%d+ - requires 1 or more numbers (since all colors are 0-1, we don't even need the +
%.? - requires 1 or none period characters (used to find decimals)
%d* - requires 0 or more numbers (to get the trailing digits in decimals)

We use parenthesis in the pattern to "capture" the match, which is then sent to a variable.

%s* - requires 0 or more spaces (used to trim whitespace between numbers and commas
, - requires a comma

Since we have three captures, we need three variables. The values matched within the string are sent to the variables in order.
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
04 Mar 2015 01:26 PM
"(%d+%.?%d*)%s*,%s*(%d+%.?%d*)%s*,%s*(%d+%.?%d*)"

Yuck.
Report Abuse
nomer888 is not online. nomer888
Joined: 13 Feb 2010
Total Posts: 551
04 Mar 2015 01:27 PM
@AgentFirefox

That method does not work for expressions or scientific notation, e.g. '1e1 * 0.1'
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image