Esempio n. 1
0
        public ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
        {
            XmlDocument xmlDoc = loadXmlScheme(schemeName); // Create an XML document object

            if (xmlDoc == null)
            {
                return(null);
            }
            XmlNode     root     = xmlDoc.GetElementsByTagName("dict")[0];
            XmlNodeList children = root.ChildNodes;

            uint[] colorTable = new uint[COLOR_TABLE_SIZE];
            uint?  fgColor = null, bgColor = null;
            int    colorsFound = 0;
            bool   success     = false;

            foreach (var tableEntry in children.OfType <XmlNode>().Where(_ => _.Name == "key"))
            {
                uint    rgb        = 0;
                int     index      = -1;
                XmlNode components = tableEntry.NextSibling;
                success = parseRgbFromXml(components, ref rgb);
                if (!success)
                {
                    break;
                }
                else if (tableEntry.InnerText == FG_KEY)
                {
                    fgColor = rgb;
                }
                else if (tableEntry.InnerText == BG_KEY)
                {
                    bgColor = rgb;
                }
                else if (-1 != (index = Array.IndexOf(PLIST_COLOR_NAMES, tableEntry.InnerText)))
                {
                    colorTable[index] = rgb; colorsFound++;
                }
            }
            if (colorsFound < COLOR_TABLE_SIZE)
            {
                if (reportErrors)
                {
                    Console.WriteLine(Resources.InvalidNumberOfColors);
                }
                success = false;
            }
            if (!success)
            {
                return(null);
            }

            var consoleAttributes = new ConsoleAttributes {
                foreground = fgColor, background = bgColor
            };

            return(new ColorScheme {
                colorTable = colorTable, consoleAttributes = consoleAttributes
            });
        }
Esempio n. 2
0
 public ColorScheme(string name, uint[] colorTable, ConsoleAttributes consoleAttributes)
 {
     Name              = name;
     ColorTable        = colorTable;
     ConsoleAttributes = consoleAttributes;
 }
Esempio n. 3
0
 public ColorScheme(uint[] colorTable, ConsoleAttributes consoleAttributes)
 {
     ColorTable        = colorTable;
     ConsoleAttributes = consoleAttributes;
 }
Esempio n. 4
0
        public ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
        {
            XmlDocument xmlDoc = loadJsonFile(schemeName);

            if (xmlDoc == null)
            {
                return(null);
            }

            try
            {
                XmlNode     root       = xmlDoc.DocumentElement;
                XmlNodeList children   = root.ChildNodes;
                uint[]      colorTable = new uint[COLOR_TABLE_SIZE];;
                for (int i = 0; i < COLOR_TABLE_SIZE; i++)
                {
                    string name = CONCFG_COLOR_NAMES[i];
                    var    node = children.OfType <XmlNode>().Where(n => n.Name == name).Single();
                    colorTable[i] = ParseColor(node.InnerText);
                }


                uint?popupForeground = null;
                uint?popupBackground = null;

                var popupNode = children.OfType <XmlNode>().Where(n => n.Name == "popup_colors").SingleOrDefault();
                if (popupNode != null)
                {
                    var parts = popupNode.InnerText.Split(',');
                    if (parts.Length == 2)
                    {
                        var foregroundIndex = (CONCFG_COLOR_NAMES as IList <string>).IndexOf(parts[0]);
                        var backgroundIndex = (CONCFG_COLOR_NAMES as IList <string>).IndexOf(parts[1]);
                        if (foregroundIndex != -1 && backgroundIndex != -1)
                        {
                            popupForeground = colorTable[foregroundIndex];
                            popupBackground = colorTable[backgroundIndex];
                        }
                    }
                }

                uint?screenForeground = null;
                uint?screenBackground = null;

                var screenNode = children.OfType <XmlNode>().Where(n => n.Name == "screen_colors").SingleOrDefault();
                if (screenNode != null)
                {
                    var parts = screenNode.InnerText.Split(',');
                    if (parts.Length == 2)
                    {
                        var foregroundIndex = (CONCFG_COLOR_NAMES as IList <string>).IndexOf(parts[0]);
                        var backgroundIndex = (CONCFG_COLOR_NAMES as IList <string>).IndexOf(parts[1]);
                        if (foregroundIndex != -1 && backgroundIndex != -1)
                        {
                            screenForeground = colorTable[foregroundIndex];
                            screenBackground = colorTable[backgroundIndex];
                        }
                    }
                }

                var consoleAttributes = new ConsoleAttributes {
                    background = screenBackground, foreground = screenForeground, popupBackground = popupBackground, popupForeground = popupForeground
                };
                return(new ColorScheme {
                    colorTable = colorTable, consoleAttributes = consoleAttributes
                });
            }
            catch (Exception /*e*/)
            {
                if (reportErrors)
                {
                    Console.WriteLine("failes to load json scheme");
                }

                return(null);
            }
        }
Esempio n. 5
0
        public ColorScheme ParseScheme(string schemeName, bool reportErrors = false)
        {
            bool success = true;

            string filename = FindIniScheme(schemeName);

            if (filename == null)
            {
                return(null);
            }

            string[] tableStrings         = new string[COLOR_TABLE_SIZE];
            uint[]   colorTable           = null;
            uint?    foregroundColor      = null;
            uint?    backgroundColor      = null;
            uint?    popupForegroundColor = null;
            uint?    popupBackgroundColor = null;

            for (int i = 0; i < COLOR_TABLE_SIZE; i++)
            {
                string        name   = COLOR_NAMES[i];
                StringBuilder buffer = new StringBuilder(512);
                GetPrivateProfileString("table", name, null, buffer, 512, filename);

                tableStrings[i] = buffer.ToString();
                if (tableStrings[i].Length <= 0)
                {
                    success = false;
                    if (reportErrors)
                    {
                        Console.WriteLine(string.Format(Resources.IniParseError, filename, name, tableStrings[i]));
                    }
                    break;
                }
            }

            if (success)
            {
                try
                {
                    colorTable = new uint[COLOR_TABLE_SIZE];
                    for (int i = 0; i < COLOR_TABLE_SIZE; i++)
                    {
                        colorTable[i] = ParseColor(tableStrings[i]);
                    }

                    if (ReadAttributes("popup", out var foreground, out var background))
                    {
                        var foregroundIndex = (COLOR_NAMES as IList <string>).IndexOf(foreground);
                        var backgroundIndex = (COLOR_NAMES as IList <string>).IndexOf(background);
                        if (foregroundIndex != -1 && backgroundIndex != -1)
                        {
                            popupForegroundColor = colorTable[foregroundIndex];
                            popupBackgroundColor = colorTable[backgroundIndex];
                        }
                    }

                    if (ReadAttributes("screen", out foreground, out background))
                    {
                        var foregroundIndex = (COLOR_NAMES as IList <string>).IndexOf(foreground);
                        var backgroundIndex = (COLOR_NAMES as IList <string>).IndexOf(background);
                        if (foregroundIndex != -1 && backgroundIndex != -1)
                        {
                            foregroundColor = colorTable[foregroundIndex];
                            backgroundColor = colorTable[backgroundIndex];
                        }
                    }
                }
                catch (Exception /*e*/)
                {
                    if (reportErrors)
                    {
                        Console.WriteLine(string.Format(Resources.IniLoadError, filename));
                    }

                    colorTable = null;
                }
            }

            if (colorTable != null)
            {
                var consoleAttributes = new ConsoleAttributes {
                    background = backgroundColor, foreground = foregroundColor, popupBackground = popupBackgroundColor, popupForeground = popupForegroundColor
                };
                return(new ColorScheme {
                    colorTable = colorTable, consoleAttributes = consoleAttributes
                });
            }
            else
            {
                return(null);
            }

            bool ReadAttributes(string section, out string foreground, out string background)
            {
                foreground = null;
                background = null;

                StringBuilder buffer = new StringBuilder(512);

                GetPrivateProfileString(section, "FOREGROUND", null, buffer, 512, filename);
                foreground = buffer.ToString();
                if (!COLOR_NAMES.Contains(foreground))
                {
                    return(false);
                }


                buffer = new StringBuilder(512);
                GetPrivateProfileString(section, "BACKGROUND", null, buffer, 512, filename);
                background = buffer.ToString();
                if (!COLOR_NAMES.Contains(background))
                {
                    return(false);
                }

                return(true);
            }
        }