/// <summary>
        /// Adds or updates a theme.
        /// </summary>
        /// <param name="colorThemeData">The theme to update.</param>
        /// <remarks>
        /// Just updates the one and only "CustomTheme". No other themes are supported at the moment.
        /// </remarks>
        public void AddOrUpdate(ColorThemeData colorThemeData)
        {
            var colorThemes = GetColorThemesSettings();

            //if (connectionData.Id == null)
            //{
            //    connectionData.Id = Guid.NewGuid().ToString();
            //}

            JsonObject jsonObject = new JsonObject();

            jsonObject.Add("FontFamily", JsonValue.CreateStringValue(colorThemeData.FontFamily));
            jsonObject.Add("FontSize", JsonValue.CreateNumberValue(colorThemeData.FontSize));

            JsonObject jsonColorTable = new JsonObject();
            foreach (var colorTableEntry in colorThemeData.ColorTable.Where(c => (int)c.Key < 16))
            {
                jsonColorTable.Add(colorTableEntry.Key.ToString(), JsonValue.CreateNumberValue(ColorToDouble(colorTableEntry.Value)));
            }

            jsonObject.Add("ColorTable", jsonColorTable);

            string colorThemeJsonString = jsonObject.Stringify();

            colorThemes["CustomTheme"] = colorThemeJsonString;

            this.CustomTheme = colorThemeData;
        }
        /// <summary>
        /// Adds or updates a theme.
        /// </summary>
        /// <param name="colorThemeData">The theme to update.</param>
        /// <remarks>
        /// Just updates the one and only "CustomTheme". No other themes are supported at the moment.
        /// </remarks>
        public void AddOrUpdate(ColorThemeData colorThemeData)
        {
            var colorThemes = GetColorThemesSettings();

            //if (connectionData.Id == null)
            //{
            //    connectionData.Id = Guid.NewGuid().ToString();
            //}

            JsonObject jsonObject = new JsonObject();

            jsonObject.Add("FontFamily", JsonValue.CreateStringValue(colorThemeData.FontFamily));
            jsonObject.Add("FontSize", JsonValue.CreateNumberValue(colorThemeData.FontSize));

            JsonObject jsonColorTable = new JsonObject();

            foreach (var colorTableEntry in colorThemeData.ColorTable.Where(c => (int)c.Key < 16))
            {
                jsonColorTable.Add(colorTableEntry.Key.ToString(), JsonValue.CreateNumberValue(ColorToDouble(colorTableEntry.Value)));
            }

            jsonObject.Add("ColorTable", jsonColorTable);

            string colorThemeJsonString = jsonObject.Stringify();

            colorThemes["CustomTheme"] = colorThemeJsonString;

            this.CustomTheme = colorThemeData;
        }
        /// <summary>
        /// Reads all themes from the roaming app settings.
        /// </summary>
        public void GetColorThemes()
        {
            var colorThemes = GetColorThemesSettings();

            if (!colorThemes.ContainsKey("CustomTheme"))
            {
                this.CustomTheme = ColorThemeData.CreateDefault();
                return;
            }

            ColorThemeData colorThemeData = ColorThemeData.CreateDefault();

            try
            {
                string     colorThemeJsonString = (string)colorThemes["CustomTheme"];
                JsonObject jsonObject           = JsonObject.Parse(colorThemeJsonString);

                colorThemeData.FontFamily = jsonObject.ContainsKey("FontFamily") ? jsonObject.GetNamedString("FontFamily") : colorThemeData.FontFamily;
                colorThemeData.FontSize   = jsonObject.ContainsKey("FontSize") ? (int)jsonObject.GetNamedNumber("FontSize") : colorThemeData.FontSize;

                JsonObject jsonColorTable = jsonObject.GetNamedObject("ColorTable");
                foreach (var jsonColorTableEntry in jsonColorTable)
                {
                    ScreenColor screenColor;
                    if (!Enum.TryParse <ScreenColor>(jsonColorTableEntry.Key, out screenColor))
                    {
                        continue;
                    }

                    if (jsonColorTableEntry.Value.ValueType != JsonValueType.Number)
                    {
                        continue;
                    }

                    colorThemeData.ColorTable[screenColor] = DoubleToColor(jsonColorTableEntry.Value.GetNumber());
                }
            }
            catch (Exception)
            {
                // A color theme seems to contain invalid data, ignore it, don't delete it.
                // Maybe a future update is able to read the data.
                //continue;
            }

            this.CustomTheme = colorThemeData;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ColorSettingsFlyout"/> class.
        /// </summary>
        public ColorSettingsFlyout()
        {
            this.InitializeComponent();

            this.customTheme = ColorThemesDataSource.GetCustomTheme();
            for (int i = 0; i < this.ScreenColorListBox.Items.Count; i++)
            {
                ListBoxItem item = (ListBoxItem)this.ScreenColorListBox.Items[i];

                int screenColor = i - 4;
                Color color = this.customTheme.ColorTable[(ScreenColor)screenColor];
                item.BorderBrush = new SolidColorBrush(color);
                item.BorderThickness = new Thickness(50.0d, 0.0d, 0.0d, 0.0d);
            }
            this.ScreenColorListBox.SelectedIndex = 0;

            this.FontFamilyListBox.Items.Clear();
            this.FontFamilyListBox.ItemsSource = ScreenDisplay.BaseLogicalFontMetrics.Keys;
            this.FontFamilyListBox.SelectedItem = this.customTheme.FontFamily;
            this.FontSizeSlider.Value = this.customTheme.FontSize;
        }
        /// <summary>
        /// Reads all themes from the roaming app settings.
        /// </summary>
        public void GetColorThemes()
        {
            var colorThemes = GetColorThemesSettings();
            if (!colorThemes.ContainsKey("CustomTheme"))
            {
                this.CustomTheme = ColorThemeData.CreateDefault();
                return;
            }

            ColorThemeData colorThemeData = ColorThemeData.CreateDefault();

            try
            {
                string colorThemeJsonString = (string)colorThemes["CustomTheme"];
                JsonObject jsonObject = JsonObject.Parse(colorThemeJsonString);

                colorThemeData.FontFamily = jsonObject.ContainsKey("FontFamily") ? jsonObject.GetNamedString("FontFamily") : colorThemeData.FontFamily;
                colorThemeData.FontSize = jsonObject.ContainsKey("FontSize") ? (int)jsonObject.GetNamedNumber("FontSize") : colorThemeData.FontSize;

                JsonObject jsonColorTable = jsonObject.GetNamedObject("ColorTable");
                foreach (var jsonColorTableEntry in jsonColorTable)
                {
                    ScreenColor screenColor;
                    if (!Enum.TryParse<ScreenColor>(jsonColorTableEntry.Key, out screenColor))
                    {
                        continue;
                    }

                    if (jsonColorTableEntry.Value.ValueType != JsonValueType.Number)
                    {
                        continue;
                    }

                    colorThemeData.ColorTable[screenColor] = DoubleToColor(jsonColorTableEntry.Value.GetNumber());
                }
            }
            catch (Exception)
            {
                // A color theme seems to contain invalid data, ignore it, don't delete it.
                // Maybe a future update is able to read the data.
                //continue;
            }

            this.CustomTheme = colorThemeData;
        }