Exemple #1
0
        private string GetResult(SeparatedList originalList, IReadOnlyList <Double> numbersList)
        {
            var sb = new StringBuilder();

            var list = numbersList.Select(l => l.Value).ToList();

            sb.AppendLine(TableStart());
            sb.AppendLine(Row("Sum:", Fixed(list.Sum().ToString())));
            sb.AppendLine(Row("Product:", Fixed(list.Aggregate(1.0, (n, m) => n * m).ToString())));
            sb.AppendLine(Row("Average:", Fixed(list.Average().ToString())));
            sb.AppendLine(Row("Min:", Fixed(list.Min().ToString())));
            sb.AppendLine(Row("Max:", Fixed(list.Max().ToString())));
            sb.AppendLine(Row("Count:", Fixed(list.Count().ToString())));

            list.Sort();
            sb.AppendLine(Row("Sorted:", Fixed(string.Join(", ", list))));

            var integerList = originalList.GetStructuresOfType <Integer>();

            if (integerList != null && integerList.Count == originalList.Count)
            {
                sb.AppendLine(Row("Hex to decimal:", Fixed(string.Join(", ", integerList.Select(l => l.ForceHexadecimalValue().ToString())))));
                if (integerList.All(i => i.Kind == IntegerKind.Decimal))
                {
                    sb.AppendLine(Row("Decimal to hex:", Fixed(string.Join(", ", integerList.Select(l => l.Int32.ToHex())))));
                }
            }

            sb.AppendLine("</table>");

            return(sb.ToString());
        }
Exemple #2
0
        private object GetResult(SeparatedList originalList, IReadOnlyList <Double> numbersList)
        {
            var list = numbersList.Select(l => l.Value).ToList();

            var pairs = new List <(string, string)>
            {
                ("Sum:", list.Sum().ToString()),
                ("Product:", list.Aggregate(1.0, (n, m) => n * m).ToString()),
                ("Average:", list.Average().ToString()),
                ("Min:", list.Min().ToString()),
                ("Max:", list.Max().ToString()),
                ("Count:", list.Count().ToString()),
                ("Sorted:", string.Join(", ", list.OrderBy(s => s)))
            };

            var integerList = originalList.GetStructuresOfType <Integer>();

            if (integerList != null && integerList.Count == originalList.Count)
            {
                pairs.Add(("Hex to decimal:", string.Join(", ", integerList.Select(l => l.ForceHexadecimalValue().ToString()))));

                if (integerList.All(i => i.Kind == IntegerKind.Decimal))
                {
                    IEnumerable <string> hexList = null;
                    string separator             = ", ";
                    if (integerList.All(i => i.Int32 >= 0 && i.Int32 < 256))
                    {
                        hexList   = integerList.Select(l => l.Int32.ToHex().PadLeft(2, '0'));
                        separator = " ";
                    }
                    else
                    {
                        hexList = integerList.Select(l => l.Int32.ToHex());
                    }

                    pairs.Add(("Decimal to hex:", string.Join(separator, hexList)));
                }
            }

            return(NameValueTable(null, right => right.Style = "Fixed", pairs.ToArray()));
        }
Exemple #3
0
        public string GetResult(Query query)
        {
            if (query.IsHelp)
            {
                return(HelpTable
                       (
                           ("color", "Named color palette"),
                           ("red", "Named color"),
                           ("#FFC0CB", "Hex color"),
                           ("rgb 23 145 175", "RGB")
                       ));
            }

            var input = query.OriginalInput.Trim();

            if (string.Equals(input, "color", StringComparison.OrdinalIgnoreCase) ||
                string.Equals(input, "colors", StringComparison.OrdinalIgnoreCase))
            {
                return(RenderColorTable());
            }

            string knownColor = null;

            if (knownColors.TryGetValue(input.Trim(), out knownColor))
            {
                return(GetResultFromHexString(knownColor));
            }

            if (query.Structure == null)
            {
                return(null);
            }

            SeparatedList list = query.TryGetStructure <SeparatedList>();

            if (list == null)
            {
                var invocation = query.TryGetStructure <Invocation>();
                if (invocation != null &&
                    string.Equals(invocation.Prefix, "rgb", StringComparison.OrdinalIgnoreCase))
                {
                    list = Engine.TryGetStructure <SeparatedList>(invocation.ArgumentListParsed);
                    if (list != null && list.Count != 3)
                    {
                        list = null;
                    }
                }
            }

            if (list != null &&
                (list.Count == 3 ||
                 (list.Count == 4 &&
                  (list.TryGetStructure <Keyword>(0) == "rgb" ||
                   list.TryGetStructure <Keyword>(3) == "rgb"))))
            {
                var intList = list.GetStructuresOfType <Integer>();
                if (intList.Count == 3)
                {
                    var r = intList[0].Value;
                    var g = intList[1].Value;
                    var b = intList[2].Value;
                    if (r >= 0 && r < 256 && g >= 0 && g < 256 && b >= 0 && b < 256)
                    {
                        return(GetResult((int)r, (int)g, (int)b));
                    }
                }
            }

            var hashPrefix = query.TryGetStructure <Prefix>();

            if (hashPrefix != null &&
                hashPrefix.PrefixString == "#")
            {
                var remainderString = hashPrefix.RemainderString;
                if (remainderString.Length == 3 || remainderString.Length == 6 || remainderString.Length == 8)
                {
                    var integer = Engine.TryGetStructure <Integer>(hashPrefix.Remainder);
                    if (integer != null)
                    {
                        return(GetResultFromHexString(remainderString));
                    }
                }
            }

            return(null);
        }