예제 #1
0
 /// <summary>
 /// Parse an hexadecimal input string and returns
 /// an object of hexadecimal class
 /// </summary>
 /// <param name="input">hexadecimal input</param>
 /// <param name="result">object of hexadecimal class</param>
 /// <returns>true if successfull parsing</returns>
 public static bool TryParse(string input, out Hexadecimal result)
 {
     try
     {
         result = new Hexadecimal(input);
         return(true);
     }
     catch
     {
         result = new Hexadecimal("0");
         return(false);
     }
 }
예제 #2
0
 /// <summary>
 /// Default constructor
 /// </summary>
 /// <param name="input">input string in hexadecimal digits</param>
 public Hexadecimal(string input)
 {
     try
     {
         this.code = Hexadecimal.ToHexadecimal(input);
     }
     catch (ArgumentException e)
     {
         throw new FormatException(Localization.Strings.GetString("ExceptionNotHexadecimal"), e);
     }
     catch (OverflowException e)
     {
         throw new Exception(Localization.Strings.GetString("ExceptionHexadecimalOverflow"), e);
     }
 }
예제 #3
0
        /// <summary>
        /// Converts an hexadecimal string into an int
        /// </summary>
        /// <param name="input">input hexadecimal string</param>
        /// <returns>int</returns>
        public static int ToHexadecimal(string input)
        {
            int output = 0;

            foreach (char c in input)
            {
                try
                {
                    output *= 16;
                    int digitOut = Hexadecimal.ToHexadecimalDigit(c);
                    output += digitOut;
                }
                catch (OverflowException)
                {
                    break;
                }
            }
            return(output);
        }
예제 #4
0
        /// <summary>
        /// Parse color and return a new CSSColor
        /// </summary>
        /// <param name="colorValue">color value to parse</param>
        /// <returns>object result</returns>
        public static CSSColor ParseColor(string colorValue)
        {
            Regex rg = new Regex(@"(#(([0-9a-f][0-9a-f]){3,4}))|(rgb\((([0-9]|\s)+),(([0-9]|\s)+),(([0-9]|\s)+)\))|(rgba\((([0-9]|\s)+),(([0-9]|\s)+),(([0-9]|\s)+),(([0-9]|\s)+)\))|(#\([^)]+\))|([A-Za-z0-9]+)", RegexOptions.IgnoreCase);
            Match m  = rg.Match(colorValue.Trim());

            if (m.Success)
            {
                if (m.Groups[1].Success)
                {
                    Hexadecimal a, r, g, b;
                    bool        ok  = true;
                    int         cap = 0;
                    if (m.Groups[2].Value.Length == 8)
                    {
                        ok &= Hexadecimal.TryParse(m.Groups[3].Captures[cap++].Value, out a);
                    }
                    else
                    {
                        a = new Hexadecimal("FF");
                    }
                    ok &= Hexadecimal.TryParse(m.Groups[3].Captures[cap++].Value, out r);
                    ok &= Hexadecimal.TryParse(m.Groups[3].Captures[cap++].Value, out g);
                    ok &= Hexadecimal.TryParse(m.Groups[3].Captures[cap++].Value, out b);
                    if (ok)
                    {
                        return(new CSSColor(Color.FromArgb(a.Value, r.Value, g.Value, b.Value)));
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                else if (m.Groups[4].Success)
                {
                    byte r = 0, g = 0, b = 0;
                    bool ok = true;
                    ok &= Byte.TryParse(m.Groups[5].Value, out r);
                    ok &= Byte.TryParse(m.Groups[7].Value, out g);
                    ok &= Byte.TryParse(m.Groups[9].Value, out b);
                    if (ok)
                    {
                        return(new CSSColor(Color.FromArgb(0xFF, r, g, b)));
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                else if (m.Groups[11].Success)
                {
                    float a = 0;
                    byte  r = 0, g = 0, b = 0;
                    bool  ok = true;
                    ok &= Byte.TryParse(m.Groups[12].Value, out r);
                    ok &= Byte.TryParse(m.Groups[14].Value, out g);
                    ok &= Byte.TryParse(m.Groups[16].Value, out b);
                    ok &= float.TryParse(m.Groups[19].Value, out a);
                    if (ok)
                    {
                        return(new CSSColor(Color.FromArgb((byte)(a * 0xFF), r, g, b)));
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                else if (m.Groups[21].Success)
                {
                    throw new FormatException();
                }
                else if (m.Groups[22].Success)
                {
                    return(new CSSColor(Color.FromName(m.Groups[22].Value)));
                }
                else
                {
                    throw new FormatException();
                }
            }
            else
            {
                throw new FormatException();
            }
        }