Microsoft.Xna.Framework.ColorExtensions.FromParser C# (CSharp) Method

FromParser() public static method

Gets a color in the format of SadConsole.ParseCommandRecolor.
public static FromParser ( this color, string value, bool &keepR, bool &keepG, bool &keepB, bool &keepA, bool &useDefault ) : Color
color this
value string
keepR bool
keepG bool
keepB bool
keepA bool
useDefault bool
return Color
        public static Color FromParser(this Color color, string value, out bool keepR, out bool keepG, out bool keepB, out bool keepA, out bool useDefault)
        {
            useDefault = false;
            keepR = false;
            keepG = false;
            keepB = false;
            keepA = false;

            ArgumentException exception = new ArgumentException("Cannot parse color string");
            Color returnColor = color;

            if (value.Contains(","))
            {
                string[] channels = value.Trim(' ').Split(',');

                if (channels.Length >= 3)
                {

                    byte colorValue;

                    // Red
                    if (channels[0] == "x")
                        keepR = true;
                    else if (byte.TryParse(channels[0], out colorValue))
                        returnColor.R = colorValue;
                    else
                        throw exception;

                    // Green
                    if (channels[1] == "x")
                        keepG = true;
                    else if (byte.TryParse(channels[1], out colorValue))
                        returnColor.G = colorValue;
                    else
                        throw exception;

                    // Blue
                    if (channels[2] == "x")
                        keepB = true;
                    else if (byte.TryParse(channels[2], out colorValue))
                        returnColor.B = colorValue;
                    else
                        throw exception;

                    if (channels.Length == 4)
                    {
                        // Alpha
                        if (channels[3] == "x")
                            keepA = true;
                        else if (byte.TryParse(channels[3], out colorValue))
                            returnColor.A = colorValue;
                        else
                            throw exception;
                    }
                    else
                        returnColor.A = 255;

                    return returnColor;
                }
                else
                    throw exception;
            }
            else if (value == "default")
            {
                useDefault = true;
                return returnColor;
            }
            else
            {
                value = value.ToLower();

                if (ColorMappings.ContainsKey(value))
                    return ColorMappings[value];
                else
                {
                    // Lookup color in framework
                    Type colorType = typeof(ColorHelper);

                    global::System.Reflection.PropertyInfo[] propInfoList =
                        colorType.GetProperties(global::System.Reflection.BindingFlags.Static | global::System.Reflection.BindingFlags.DeclaredOnly | global::System.Reflection.BindingFlags.Public);

                    if (propInfoList.Length != 0)
                    {
                        for (int i = 0; i < propInfoList.Length; i++)
                        {
                            if (propInfoList[i].Name.ToLower() == value)
                            {
                                return (Color)propInfoList[i].GetValue(null, null);
                            }
                        }
                    }
                    else
                    {
                        var fieldInfoList =
                        colorType.GetFields(global::System.Reflection.BindingFlags.Static | global::System.Reflection.BindingFlags.DeclaredOnly | global::System.Reflection.BindingFlags.Public);

                        for (int i = 0; i < fieldInfoList.Length; i++)
                        {
                            if (fieldInfoList[i].Name.ToLower() == value)
                            {
                                return (Color)fieldInfoList[i].GetValue(null);
                            }
                        }
                    }

                    throw exception;
                }
            }
        }