Garry's Mod Wiki

Revision Difference

string.ToColor#568092

<function name="ToColor" parent="string" type="libraryfunc"> <description>Fetches a Color type from a string.</description>⤶ <description>⤶ Attempts to create a Color from a string.⤶ </description>⤶ <realm>Shared and Menu</realm> <file line="343-L355">lua/includes/extensions/string.lua</file> <args> <arg name="Inputstring" type="string">The string to convert from.</arg>⤶ <arg name="colorString" type="string">⤶ The string to convert from.⤶ ⤶ The expected format is 3 or 4 integer numbers in the range `0`-`255` with a single space separating them. ⤶ These numbers are in the order: `red green blue alpha` where `alpha` is optional.⤶ </arg>⤶ </args> <rets> <ret name="" type="Color">The output <page>Color</page></ret>⤶ <ret name="" type="Color">⤶ The output <page>Color</page> ⤶ ⤶ If the input string is improperly formatted, this will be `Color( 255, 255, 255, 255 )`⤶ </ret>⤶ </rets> </function> <example> <description>Demonstrates the use of string.ToColor</description>⤶ <code>PrintTable( string.ToColor( "255 0 255 125" ) )</code>⤶ <description>⤶ Converting `"255 0 255 125"` to `Color( 255, 0, 255, 125 )`⤶ </description>⤶ <code>⤶ PrintTable( string.ToColor( "255 0 255 125" ) )⤶ </code>⤶ <output>⤶ ```⤶ ["a"] = 125⤶ ["b"] = 255⤶ ["g"] = 0⤶ ["r"] = 255⤶ ```⤶ </output>⤶ </example>⤶ ⤶ <example name="Failed Conversion" >⤶ <description>⤶ Demonstrates what happens when an invalid string is entered.⤶ </description>⤶ <code>⤶ PrintTable( string.ToColor( "Hello, world!" ) )⤶ </code>⤶ <output> ``` r = 255 g = 0⤶ b = 255 a = 125⤶ ["a"] = 255 ["b"] = 255⤶ ["g"] = 255 ["r"] = 255⤶ ``` </output> ⤶ </example></example>