|
| 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 |
|
|
|
| 04 Mar 2015 09:15 AM |
http://wiki.roblox.com/index.php?title=Function_dump/String_manipulation
CentOS-Web = /var/www/html/ |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 04 Mar 2015 09:24 AM |
Color3.new(String)
...
CentOS-Web = /var/www/html/ |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 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 |
|
|
|
| 04 Mar 2015 09:55 AM |
| Loca BC=Color3.new(BrickColor.new(string).Color) |
|
|
| Report Abuse |
|
|
| |
|
|
| 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 |
|
|
|
| 04 Mar 2015 09:59 AM |
Wouldn't this work? local R, G, B = Color.r, Color.g, Color.b |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| 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 |
|
|
|
| 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 |
|
|
|
| 04 Mar 2015 10:14 AM |
| local Red, Green, Blue = String:match("^(.+?), ?(.+?), ?(.+?)$") |
|
|
| Report Abuse |
|
|
|
| 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 |
|
|
|
| 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
|
  |
| Joined: 20 Nov 2008 |
| Total Posts: 132 |
|
|
| 04 Mar 2015 10:22 AM |
| Try ALL of these suggestions |
|
|
| Report Abuse |
|
|
anaIyze
|
  |
| Joined: 29 May 2014 |
| Total Posts: 2048 |
|
|
| 04 Mar 2015 12:42 PM |
string.format(Color3.new(^%d,+^%d,+^%d));
|
|
|
| Report Abuse |
|
|
|
| 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
|
  |
| 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
|
  |
| 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 |
|
|