コード例 #1
0
ファイル: Term.cs プロジェクト: Mofsy/jinxbot
 public Color ToColor()
 {
     string hex = "000000";
     if (type == TermType.Hex)
     {
         if ((val.Length == 7 || val.Length == 4) && val.StartsWith("#", StringComparison.InvariantCulture))
         {
             hex = val.Substring(1);
         }
         else if (val.Length == 6 || val.Length == 3)
         {
             hex = val;
         }
     }
     else if (type == TermType.Function)
     {
         if ((function.Name.ToLower().Equals("rgb") && function.Expression.Terms.Count == 3)
             || (function.Name.ToLower().Equals("rgba") && function.Expression.Terms.Count == 4)
             )
         {
             int fr = 0, fg = 0, fb = 0;
             for (int i = 0; i < function.Expression.Terms.Count; i++)
             {
                 if (function.Expression.Terms[i].Type != TermType.Number) { return Color.Black; }
                 switch (i)
                 {
                     case 0: fr = GetRGBValue(function.Expression.Terms[i]); break;
                     case 1: fg = GetRGBValue(function.Expression.Terms[i]); break;
                     case 2: fb = GetRGBValue(function.Expression.Terms[i]); break;
                 }
             }
             return Color.FromArgb(fr, fg, fb);
         }
         else if ((function.Name.ToLower().Equals("hsl") && function.Expression.Terms.Count == 3)
             || (function.Name.Equals("hsla") && function.Expression.Terms.Count == 4)
             )
         {
             int h = 0, s = 0, v = 0;
             for (int i = 0; i < function.Expression.Terms.Count; i++)
             {
                 if (function.Expression.Terms[i].Type != TermType.Number) { return Color.Black; }
                 switch (i)
                 {
                     case 0: h = GetHueValue(function.Expression.Terms[i]); break;
                     case 1: s = GetRGBValue(function.Expression.Terms[i]); break;
                     case 2: v = GetRGBValue(function.Expression.Terms[i]); break;
                 }
             }
             HSV hsv = new HSV(h, s, v);
             return hsv.Color;
         }
     }
     else
     {
         try
         {
             KnownColor kc = (KnownColor)Enum.Parse(typeof(KnownColor), val, true);
             Color c = Color.FromKnownColor(kc);
             return c;
         }
         catch { }
     }
     if (hex.Length == 3)
     {
         string temp = "";
         foreach (char c in hex)
         {
             temp += c.ToString() + c.ToString();
         }
         hex = temp;
     }
     int r = DeHex(hex.Substring(0, 2));
     int g = DeHex(hex.Substring(2, 2));
     int b = DeHex(hex.Substring(4));
     return Color.FromArgb(r, g, b);
 }
コード例 #2
0
        public Color ToColor()
        {
            string hex = "000000";

            if (type == TermType.Hex)
            {
                if ((val.Length == 7 || val.Length == 4) && val.StartsWith("#", StringComparison.InvariantCulture))
                {
                    hex = val.Substring(1);
                }
                else if (val.Length == 6 || val.Length == 3)
                {
                    hex = val;
                }
            }
            else if (type == TermType.Function)
            {
                if ((function.Name.ToLower().Equals("rgb") && function.Expression.Terms.Count == 3) ||
                    (function.Name.ToLower().Equals("rgba") && function.Expression.Terms.Count == 4)
                    )
                {
                    int fr = 0, fg = 0, fb = 0;
                    for (int i = 0; i < function.Expression.Terms.Count; i++)
                    {
                        if (function.Expression.Terms[i].Type != TermType.Number)
                        {
                            return(Color.Black);
                        }
                        switch (i)
                        {
                        case 0: fr = GetRGBValue(function.Expression.Terms[i]); break;

                        case 1: fg = GetRGBValue(function.Expression.Terms[i]); break;

                        case 2: fb = GetRGBValue(function.Expression.Terms[i]); break;
                        }
                    }
                    return(Color.FromArgb(fr, fg, fb));
                }
                else if ((function.Name.ToLower().Equals("hsl") && function.Expression.Terms.Count == 3) ||
                         (function.Name.Equals("hsla") && function.Expression.Terms.Count == 4)
                         )
                {
                    int h = 0, s = 0, v = 0;
                    for (int i = 0; i < function.Expression.Terms.Count; i++)
                    {
                        if (function.Expression.Terms[i].Type != TermType.Number)
                        {
                            return(Color.Black);
                        }
                        switch (i)
                        {
                        case 0: h = GetHueValue(function.Expression.Terms[i]); break;

                        case 1: s = GetRGBValue(function.Expression.Terms[i]); break;

                        case 2: v = GetRGBValue(function.Expression.Terms[i]); break;
                        }
                    }
                    HSV hsv = new HSV(h, s, v);
                    return(hsv.Color);
                }
            }
            else
            {
                try
                {
                    KnownColor kc = (KnownColor)Enum.Parse(typeof(KnownColor), val, true);
                    Color      c  = Color.FromKnownColor(kc);
                    return(c);
                }
                catch { }
            }
            if (hex.Length == 3)
            {
                string temp = "";
                foreach (char c in hex)
                {
                    temp += c.ToString() + c.ToString();
                }
                hex = temp;
            }
            int r = DeHex(hex.Substring(0, 2));
            int g = DeHex(hex.Substring(2, 2));
            int b = DeHex(hex.Substring(4));

            return(Color.FromArgb(r, g, b));
        }