Garry's Mod Wiki

string.ToColor

  Color string.ToColor( string colorString )

Description

Attempts to create a Color from a string.

Arguments

1 string colorString
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.

If the input string is malformed but contains a correctly formatted substring within it, that valid substring will be used which may produce unexpected results.

Returns

1 Color
The output Color

If the input string is improperly formatted, this will be Color( 255, 255, 255, 255 )

Example

Converting "255 0 255 125" to Color( 255, 0, 255, 125 )

PrintTable( string.ToColor( "255 0 255 125" ) )
Output:
["a"] = 125 ["b"] = 255 ["g"] = 0 ["r"] = 255

Example: Failed Conversion

Demonstrates what happens when an invalid string is entered.

PrintTable( string.ToColor( "Hello, world!" ) )
Output:
["a"] = 255 ["b"] = 255 ["g"] = 255 ["r"] = 255

Example: Invalid String with Valid Substring

Demonstrates how a malformed input can produce an unexpected output.

Because the input string contains the valid substring 5 100 25, that will be treated as a "red green blue" formatted string to produce Color( 5, 100, 25 ).
All other text in the string, including the number 50, will be ignored.

PrintTable( string.ToColor( "ThisWillBeIgnored5 100 25 ThisWillAlsoBeIgnored 50" ) )
Output:
["a"] = 255 ["b"] = 25 ["g"] = 100 ["r"] = 5