Exemplo n.º 1
0
        internal static object INTERNAL_ConvertFromString(string colorcode)
        {
            try
            {
                // Check if the color is a named color:
                if (!colorcode.StartsWith("#"))
                {
                    Colors.INTERNAL_ColorsEnum namedColor = (Colors.INTERNAL_ColorsEnum)Enum.Parse(typeof(Colors.INTERNAL_ColorsEnum), colorcode, true); // Note: "TryParse" does not seem to work in JSIL.
                    return(INTERNAL_ConvertFromInt32((int)namedColor));
                }
                else
                {
                    colorcode = colorcode.Replace("#", "");
                    if (colorcode.Length == 6) // This is becaue XAML is tolerant when the user has forgot the alpha channel (eg. #DDDDDD for Gray).
                    {
                        colorcode = "FF" + colorcode;
                    }
#if !BRIDGE
                    int colorAsInt = int.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
#else
                    int colorAsInt;
                    if (CSHTML5.Interop.IsRunningInTheSimulator)
                    {
                        colorcode  = colorcode.Replace("#", "");
                        colorAsInt = INTERNAL_BridgeWorkarounds.HexToInt_SimulatorOnly(colorcode);
                    }
                    else
                    {
                        colorAsInt = Script.Write <int>("parseInt({0}, 16);", colorcode.Replace("#", ""));
                    }
#endif
                    return(INTERNAL_ConvertFromInt32(colorAsInt));
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Invalid color: " + colorcode, ex);
            }
        }
Exemplo n.º 2
0
        internal static object INTERNAL_ConvertFromString(string colorString)
        {
            string trimmedString = colorString.Trim();

            if (!string.IsNullOrEmpty(trimmedString) && (trimmedString[0] == '#'))
            {
                string tokens = trimmedString.Substring(1);
                if (tokens.Length == 6) // This is becaue XAML is tolerant when the user has forgot the alpha channel (eg. #DDDDDD for Gray).
                {
                    tokens = "FF" + tokens;
                }

#if NETSTANDARD
                int color;
                if (int.TryParse(tokens, NumberStyles.HexNumber, NumberFormatInfo.CurrentInfo, out color))
                {
                    return(INTERNAL_ConvertFromInt32(color));
                }
#else // BRIDGE
                int color;
                if (CSHTML5.Interop.IsRunningInTheSimulator)
                {
                    color = INTERNAL_BridgeWorkarounds.HexToInt_SimulatorOnly(tokens);
                }
                else
                {
                    color = Script.Write <int>("parseInt({0}, 16);", tokens);
                }

                return(INTERNAL_ConvertFromInt32(color));
#endif
            }
            else if (trimmedString != null && trimmedString.StartsWith("sc#", StringComparison.Ordinal))
            {
                string tokens = trimmedString.Substring(3);

                char[] separators = new char[1] {
                    ','
                };
                string[] words  = tokens.Split(separators);
                float[]  values = new float[4];
                for (int i = 0; i < 3; i++)
                {
                    values[i] = Convert.ToSingle(words[i]);
                }
                if (words.Length == 4)
                {
                    values[3] = Convert.ToSingle(words[3]);
                    return(Color.FromScRgb(values[0], values[1], values[2], values[3]));
                }
                else
                {
                    return(Color.FromScRgb(1.0f, values[0], values[1], values[2]));
                }
            }
            else
            {
                // Check if the color is a named color
                Colors.INTERNAL_ColorsEnum namedColor;
                if (Enum.TryParse(trimmedString, true, out namedColor))
                {
                    return(INTERNAL_ConvertFromInt32((int)namedColor));
                }
            }
            throw new Exception(string.Format("Invalid color: {0}", colorString));
        }