<NME> WpfAdapter.cs
<BEF> ﻿// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
// 
// - Sun Tsu,
// "The Art of War"

using System;
using System.IO;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using TheArtOfDev.HtmlRenderer.Adapters;
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
using TheArtOfDev.HtmlRenderer.WPF.Utilities;
using Microsoft.Win32;

namespace TheArtOfDev.HtmlRenderer.WPF.Adapters
{
    /// <summary>
    /// Adapter for WPF platform.
    /// </summary>
    internal sealed class WpfAdapter : RAdapter
    {
        #region Fields and Consts

        /// <summary>
        /// Singleton instance of global adapter.
        /// </summary>
        private static readonly WpfAdapter _instance = new WpfAdapter();

        #endregion


        /// <summary>
        /// Init installed font families and set default font families mapping.
        /// </summary>
        private WpfAdapter()
        {
            AddFontFamilyMapping("monospace", "Courier New");
            AddFontFamilyMapping("Helvetica", "Arial");

            foreach (var family in Fonts.SystemFontFamilies)
            {
	            try
	            {
	                AddFontFamily(new FontFamilyAdapter(family));
	            }
	            catch
	            {
	            }
            }
        }

        /// <summary>

        protected override RColor GetColorInt(string colorName)
        {
            var convertFromString = ColorConverter.ConvertFromString(colorName) ?? Colors.Black;
            return Utils.Convert((Color)convertFromString);
        }
            if (!ValidColorNamesLc.Contains(colorName.ToLower()))
                return RColor.Empty;

            var convertFromString = ColorConverter.ConvertFromString(colorName) ?? Colors.Black;
            return Utils.Convert((Color)convertFromString);
        }

        protected override RPen CreatePen(RColor color)
        {
            return new PenAdapter(GetSolidColorBrush(color));
        }

        protected override RBrush CreateSolidBrush(RColor color)
        {
            var solidBrush = GetSolidColorBrush(color);
            return new BrushAdapter(solidBrush);
        }

        protected override RBrush CreateLinearGradientBrush(RRect rect, RColor color1, RColor color2, double angle)
        {
            var startColor = angle <= 180 ? Utils.Convert(color1) : Utils.Convert(color2);
            var endColor = angle <= 180 ? Utils.Convert(color2) : Utils.Convert(color1);
            angle = angle <= 180 ? angle : angle - 180;
            double x = angle < 135 ? Math.Max((angle - 45) / 90, 0) : 1;
            double y = angle <= 45 ? Math.Max(0.5 - angle / 90, 0) : angle > 135 ? Math.Abs(1.5 - angle / 90) : 0;
            return new BrushAdapter(new LinearGradientBrush(startColor, endColor, new Point(x, y), new Point(1 - x, 1 - y)));
        }

        protected override RImage ConvertImageInt(object image)
        {
            return image != null ? new ImageAdapter((BitmapImage)image) : null;
        }

        protected override RImage ImageFromStreamInt(Stream memoryStream)
        {
            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.StreamSource = memoryStream;
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.EndInit();
            bitmap.Freeze();

            return new ImageAdapter(bitmap);
        }

        protected override RFont CreateFontInt(string family, double size, RFontStyle style)
        {
            var fontFamily = (FontFamily)new FontFamilyConverter().ConvertFromString(family) ?? new FontFamily();
            return new FontAdapter(new Typeface(fontFamily, GetFontStyle(style), GetFontWidth(style), FontStretches.Normal), size);
        }

        protected override RFont CreateFontInt(RFontFamily family, double size, RFontStyle style)
        {
            return new FontAdapter(new Typeface(((FontFamilyAdapter)family).FontFamily, GetFontStyle(style), GetFontWidth(style), FontStretches.Normal), size);
        }

        protected override object GetClipboardDataObjectInt(string html, string plainText)
        {
            return ClipboardHelper.CreateDataObject(html, plainText);
        }

        protected override void SetToClipboardInt(string text)
        {
            ClipboardHelper.CopyToClipboard(text);
        }

        protected override void SetToClipboardInt(string html, string plainText)
        {
            ClipboardHelper.CopyToClipboard(html, plainText);
        }

        protected override void SetToClipboardInt(RImage image)
        {
            Clipboard.SetImage(((ImageAdapter)image).Image);
        }

        protected override RContextMenu CreateContextMenuInt()
        {
            return new ContextMenuAdapter();
        }

        protected override void SaveToFileInt(RImage image, string name, string extension, RControl control = null)
        {
            var saveDialog = new SaveFileDialog();
            saveDialog.Filter = "Images|*.png;*.bmp;*.jpg;*.tif;*.gif;*.wmp;";
            saveDialog.FileName = name;
            saveDialog.DefaultExt = extension;

            var dialogResult = saveDialog.ShowDialog();
            if (dialogResult.GetValueOrDefault())
            {
                var encoder = Utils.GetBitmapEncoder(Path.GetExtension(saveDialog.FileName));
                encoder.Frames.Add(BitmapFrame.Create(((ImageAdapter)image).Image));
                using (FileStream stream = new FileStream(saveDialog.FileName, FileMode.OpenOrCreate))
                    encoder.Save(stream);
            }
        }


        #region Private/Protected methods

        /// <summary>
        /// Get solid color brush for the given color.
        /// </summary>
        private static Brush GetSolidColorBrush(RColor color)
        {
            Brush solidBrush;
            if (color == RColor.White)
                solidBrush = Brushes.White;
            else if (color == RColor.Black)
                solidBrush = Brushes.Black;
            else if (color.A < 1)
                solidBrush = Brushes.Transparent;
            else
                solidBrush = new SolidColorBrush(Utils.Convert(color));
            return solidBrush;
        }

        /// <summary>
        /// Get WPF font style for the given style.
        /// </summary>
        private static FontStyle GetFontStyle(RFontStyle style)
        {
            if ((style & RFontStyle.Italic) == RFontStyle.Italic)
                return FontStyles.Italic;

            return FontStyles.Normal;
        }

        /// <summary>
        /// Get WPF font style for the given style.
        /// </summary>
        private static FontWeight GetFontWidth(RFontStyle style)
        {
            if ((style & RFontStyle.Bold) == RFontStyle.Bold)
                return FontWeights.Bold;

            return FontWeights.Normal;
        }

        #endregion
    }
}
<MSG> Make GetColorInt check if color name is valid to avoid ColorConverter throwing an exception

<DFF> @@ -11,7 +11,9 @@
 // "The Art of War"
 
 using System;
+using System.Collections.Generic;
 using System.IO;
+using System.Reflection;
 using System.Windows;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
@@ -34,8 +36,22 @@ internal sealed class WpfAdapter : RAdapter
         /// </summary>
         private static readonly WpfAdapter _instance = new WpfAdapter();
 
+        /// <summary>
+        /// List of valid predefined color names in lower-case
+        /// </summary>
+        private static readonly List<string> ValidColorNamesLc; 
+
         #endregion
 
+        static WpfAdapter()
+        {
+            ValidColorNamesLc = new List<string>();
+            var colorList = new List<PropertyInfo>(typeof(Colors).GetProperties());
+            foreach (var colorProp in colorList)
+            {
+                ValidColorNamesLc.Add(colorProp.Name.ToLower());
+            }
+        }
 
         /// <summary>
         /// Init installed font families and set default font families mapping.
@@ -61,6 +77,10 @@ public static WpfAdapter Instance
 
         protected override RColor GetColorInt(string colorName)
         {
+            // check if color name is valid to avoid ColorConverter throwing an exception
+            if (!ValidColorNamesLc.Contains(colorName.ToLower()))
+                return RColor.Empty;
+
             var convertFromString = ColorConverter.ConvertFromString(colorName) ?? Colors.Black;
             return Utils.Convert((Color)convertFromString);
         }

{'commit': 'diff --git a/Source/HtmlRenderer.WPF/Adapters/WpfAdapter.cs b/Source/HtmlRenderer.WPF/Adapters/WpfAdapter.cs\nindex 681a074..13f5eed 100644\n--- a/Source/HtmlRenderer.WPF/Adapters/WpfAdapter.cs\n+++ b/Source/HtmlRenderer.WPF/Adapters/WpfAdapter.cs\n@@ -11,7 +11,9 @@\n // "The Art of War"\n \n using System;\n+using System.Collections.Generic;\n using System.IO;\n+using System.Reflection;\n using System.Windows;\n using System.Windows.Media;\n using System.Windows.Media.Imaging;\n@@ -34,8 +36,22 @@ internal sealed class WpfAdapter : RAdapter\n         /// </summary>\n         private static readonly WpfAdapter _instance = new WpfAdapter();\n \n+        /// <summary>\n+        /// List of valid predefined color names in lower-case\n+        /// </summary>\n+        private static readonly List<string> ValidColorNamesLc; \n+\n         #endregion\n \n+        static WpfAdapter()\n+        {\n+            ValidColorNamesLc = new List<string>();\n+            var colorList = new List<PropertyInfo>(typeof(Colors).GetProperties());\n+            foreach (var colorProp in colorList)\n+            {\n+                ValidColorNamesLc.Add(colorProp.Name.ToLower());\n+            }\n+        }\n \n         /// <summary>\n         /// Init installed font families and set default font families mapping.\n@@ -61,6 +77,10 @@ public static WpfAdapter Instance\n \n         protected override RColor GetColorInt(string colorName)\n         {\n+            // check if color name is valid to avoid ColorConverter throwing an exception\n+            if (!ValidColorNamesLc.Contains(colorName.ToLower()))\n+                return RColor.Empty;\n+\n             var convertFromString = ColorConverter.ConvertFromString(colorName) ?? Colors.Black;\n             return Utils.Convert((Color)convertFromString);\n         }\n', 'commit_subject': 'Make GetColorInt check if color name is valid to avoid ColorConverter throwing an exception', 'repo_name': 'ArthurHub/HTML-Renderer', 'committer': {'name': 'mikkelxn', 'email': 'b1c1d8736f20db3fb6c1c66bb1455ed43909f0d8@xenanetworks.com', 'time_sec': '1419928109', 'tz_offset': '60', 'date': {'seconds': '1419928109'}}, 'commit_tree': 'b2d9a9efe062c921c20cbce404e443da99218f30', 'commit_message': 'Make GetColorInt check if color name is valid to avoid ColorConverter throwing an exception\n', 'language_name': 'C#', 'license': 'bsd-3-clause', 'hunks': '@@ -11,7 +11,9 @@\n // "The Art of War"\n \n using System;\n+using System.Collections.Generic;\n using System.IO;\n+using System.Reflection;\n using System.Windows;\n using System.Windows.Media;\n using System.Windows.Media.Imaging;\n@@ -34,8 +36,22 @@ internal sealed class WpfAdapter : RAdapter\n         /// </summary>\n         private static readonly WpfAdapter _instance = new WpfAdapter();\n \n+        /// <summary>\n+        /// List of valid predefined color names in lower-case\n+        /// </summary>\n+        private static readonly List<string> ValidColorNamesLc; \n+\n         #endregion\n \n+        static WpfAdapter()\n+        {\n+            ValidColorNamesLc = new List<string>();\n+            var colorList = new List<PropertyInfo>(typeof(Colors).GetProperties());\n+            foreach (var colorProp in colorList)\n+            {\n+                ValidColorNamesLc.Add(colorProp.Name.ToLower());\n+            }\n+        }\n \n         /// <summary>\n         /// Init installed font families and set default font families mapping.\n@@ -61,6 +77,10 @@ public static WpfAdapter Instance\n \n         protected override RColor GetColorInt(string colorName)\n         {\n+            // check if color name is valid to avoid ColorConverter throwing an exception\n+            if (!ValidColorNamesLc.Contains(colorName.ToLower()))\n+                return RColor.Empty;\n+\n             var convertFromString = ColorConverter.ConvertFromString(colorName) ?? Colors.Black;\n             return Utils.Convert((Color)convertFromString);\n         }\n', 'addition_count': 20, 'deletion_count': 0, 'src_file': 'a/Source/HtmlRenderer.WPF/Adapters/WpfAdapter.cs', 'tgt_file': 'b/Source/HtmlRenderer.WPF/Adapters/WpfAdapter.cs', 'file_extension': '.cs', 'before_file': '\ufeff// "Therefore those skilled at the unorthodox\n// are infinite as heaven and earth,\n// inexhaustible as the great rivers.\n// When they come to an end,\n// they begin again,\n// like the days and months;\n// they die and are reborn,\n// like the four seasons."\n// \n// - Sun Tsu,\n// "The Art of War"\n\nusing System;\nusing System.IO;\nusing System.Windows;\nusing System.Windows.Media;\nusing System.Windows.Media.Imaging;\nusing TheArtOfDev.HtmlRenderer.Adapters;\nusing TheArtOfDev.HtmlRenderer.Adapters.Entities;\nusing TheArtOfDev.HtmlRenderer.WPF.Utilities;\nusing Microsoft.Win32;\n\nnamespace TheArtOfDev.HtmlRenderer.WPF.Adapters\n{\n    /// <summary>\n    /// Adapter for WPF platform.\n    /// </summary>\n    internal sealed class WpfAdapter : RAdapter\n    {\n        #region Fields and Consts\n\n        /// <summary>\n        /// Singleton instance of global adapter.\n        /// </summary>\n        private static readonly WpfAdapter _instance = new WpfAdapter();\n\n        #endregion\n\n\n        /// <summary>\n        /// Init installed font families and set default font families mapping.\n        /// </summary>\n        private WpfAdapter()\n        {\n            AddFontFamilyMapping("monospace", "Courier New");\n            AddFontFamilyMapping("Helvetica", "Arial");\n\n            foreach (var family in Fonts.SystemFontFamilies)\n            {\n\t            try\n\t            {\n\t                AddFontFamily(new FontFamilyAdapter(family));\n\t            }\n\t            catch\n\t            {\n\t            }\n            }\n        }\n\n        /// <summary>\n\n        protected override RColor GetColorInt(string colorName)\n        {\n            var convertFromString = ColorConverter.ConvertFromString(colorName) ?? Colors.Black;\n            return Utils.Convert((Color)convertFromString);\n        }\n            if (!ValidColorNamesLc.Contains(colorName.ToLower()))\n                return RColor.Empty;\n\n            var convertFromString = ColorConverter.ConvertFromString(colorName) ?? Colors.Black;\n            return Utils.Convert((Color)convertFromString);\n        }\n\n        protected override RPen CreatePen(RColor color)\n        {\n            return new PenAdapter(GetSolidColorBrush(color));\n        }\n\n        protected override RBrush CreateSolidBrush(RColor color)\n        {\n            var solidBrush = GetSolidColorBrush(color);\n            return new BrushAdapter(solidBrush);\n        }\n\n        protected override RBrush CreateLinearGradientBrush(RRect rect, RColor color1, RColor color2, double angle)\n        {\n            var startColor = angle <= 180 ? Utils.Convert(color1) : Utils.Convert(color2);\n            var endColor = angle <= 180 ? Utils.Convert(color2) : Utils.Convert(color1);\n            angle = angle <= 180 ? angle : angle - 180;\n            double x = angle < 135 ? Math.Max((angle - 45) / 90, 0) : 1;\n            double y = angle <= 45 ? Math.Max(0.5 - angle / 90, 0) : angle > 135 ? Math.Abs(1.5 - angle / 90) : 0;\n            return new BrushAdapter(new LinearGradientBrush(startColor, endColor, new Point(x, y), new Point(1 - x, 1 - y)));\n        }\n\n        protected override RImage ConvertImageInt(object image)\n        {\n            return image != null ? new ImageAdapter((BitmapImage)image) : null;\n        }\n\n        protected override RImage ImageFromStreamInt(Stream memoryStream)\n        {\n            var bitmap = new BitmapImage();\n            bitmap.BeginInit();\n            bitmap.StreamSource = memoryStream;\n            bitmap.CacheOption = BitmapCacheOption.OnLoad;\n            bitmap.EndInit();\n            bitmap.Freeze();\n\n            return new ImageAdapter(bitmap);\n        }\n\n        protected override RFont CreateFontInt(string family, double size, RFontStyle style)\n        {\n            var fontFamily = (FontFamily)new FontFamilyConverter().ConvertFromString(family) ?? new FontFamily();\n            return new FontAdapter(new Typeface(fontFamily, GetFontStyle(style), GetFontWidth(style), FontStretches.Normal), size);\n        }\n\n        protected override RFont CreateFontInt(RFontFamily family, double size, RFontStyle style)\n        {\n            return new FontAdapter(new Typeface(((FontFamilyAdapter)family).FontFamily, GetFontStyle(style), GetFontWidth(style), FontStretches.Normal), size);\n        }\n\n        protected override object GetClipboardDataObjectInt(string html, string plainText)\n        {\n            return ClipboardHelper.CreateDataObject(html, plainText);\n        }\n\n        protected override void SetToClipboardInt(string text)\n        {\n            ClipboardHelper.CopyToClipboard(text);\n        }\n\n        protected override void SetToClipboardInt(string html, string plainText)\n        {\n            ClipboardHelper.CopyToClipboard(html, plainText);\n        }\n\n        protected override void SetToClipboardInt(RImage image)\n        {\n            Clipboard.SetImage(((ImageAdapter)image).Image);\n        }\n\n        protected override RContextMenu CreateContextMenuInt()\n        {\n            return new ContextMenuAdapter();\n        }\n\n        protected override void SaveToFileInt(RImage image, string name, string extension, RControl control = null)\n        {\n            var saveDialog = new SaveFileDialog();\n            saveDialog.Filter = "Images|*.png;*.bmp;*.jpg;*.tif;*.gif;*.wmp;";\n            saveDialog.FileName = name;\n            saveDialog.DefaultExt = extension;\n\n            var dialogResult = saveDialog.ShowDialog();\n            if (dialogResult.GetValueOrDefault())\n            {\n                var encoder = Utils.GetBitmapEncoder(Path.GetExtension(saveDialog.FileName));\n                encoder.Frames.Add(BitmapFrame.Create(((ImageAdapter)image).Image));\n                using (FileStream stream = new FileStream(saveDialog.FileName, FileMode.OpenOrCreate))\n                    encoder.Save(stream);\n            }\n        }\n\n\n        #region Private/Protected methods\n\n        /// <summary>\n        /// Get solid color brush for the given color.\n        /// </summary>\n        private static Brush GetSolidColorBrush(RColor color)\n        {\n            Brush solidBrush;\n            if (color == RColor.White)\n                solidBrush = Brushes.White;\n            else if (color == RColor.Black)\n                solidBrush = Brushes.Black;\n            else if (color.A < 1)\n                solidBrush = Brushes.Transparent;\n            else\n                solidBrush = new SolidColorBrush(Utils.Convert(color));\n            return solidBrush;\n        }\n\n        /// <summary>\n        /// Get WPF font style for the given style.\n        /// </summary>\n        private static FontStyle GetFontStyle(RFontStyle style)\n        {\n            if ((style & RFontStyle.Italic) == RFontStyle.Italic)\n                return FontStyles.Italic;\n\n            return FontStyles.Normal;\n        }\n\n        /// <summary>\n        /// Get WPF font style for the given style.\n        /// </summary>\n        private static FontWeight GetFontWidth(RFontStyle style)\n        {\n            if ((style & RFontStyle.Bold) == RFontStyle.Bold)\n                return FontWeights.Bold;\n\n            return FontWeights.Normal;\n        }\n\n        #endregion\n    }\n}', 'message': 'Make GetColorInt check if color name is valid to avoid ColorConverter throwing an exception\n', 'author_name': 'mikkelxn', 'lang': 'WPF/Adapters/WpfAdapter'}