<commit_before>// -----------------------------------------------------------------------
// <copyright file="BrushConverter.cs" company="<NAME>">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------

namespace Perspex.Markup.Xaml.Converters
{
    using System;
    using System.Globalization;
    using System.Text;
    using Media;
    using Media.Imaging;
    using OmniXaml.TypeConversion;
    using Platform;

    public class BrushConverter : ITypeConverter
    {
        public bool CanConvertFrom(IXamlTypeConverterContext context, Type sourceType)
        {
            return true;
        }

        public bool CanConvertTo(IXamlTypeConverterContext context, Type destinationType)
        {
            return true;
        }

        public object ConvertFrom(IXamlTypeConverterContext context, CultureInfo culture, object value)
        {
            var colorString = (string)value;

            var color = DecodeColor(colorString);

            return new SolidColorBrush(color);
        }

        private static Color DecodeColor(string colorString)
        {
            if (colorString[0] == '#')
            {
                var restOfValue = colorString.Remove(0, 1);

                if (restOfValue.Length == 8)
                {
                    var a = Convert.ToByte(restOfValue.Substring(0, 2), 16);
                    var r = Convert.ToByte(restOfValue.Substring(2, 2), 16);
                    var g = Convert.ToByte(restOfValue.Substring(6, 2), 16);
                    var b = Convert.ToByte(restOfValue.Substring(8, 2), 16);
                    return Color.FromArgb(a, r, g, b);
                }
                if (restOfValue.Length == 6)
                {
                    var r = Convert.ToByte(restOfValue.Substring(0, 2), 16);
                    var g = Convert.ToByte(restOfValue.Substring(2, 2), 16);
                    var b = Convert.ToByte(restOfValue.Substring(4, 2), 16);
                    return Color.FromRgb(r, g, b);
                }

                throw new InvalidOperationException("The color code format cannot be parsed");
            }
            else
            {
                return DecodeFromNamedColor(colorString);
            }

            throw new InvalidOperationException($"The color cannot be decoded from the string \"{colorString}\"");
        }

        private static Color DecodeFromNamedColor(string colorString)
        {
            if (colorString == "Crimson")
            {
                return DecodeColor("#DC143C");
            }
            if (colorString == "Coral")
            {
                return DecodeColor("#FF7F50");
            }

            throw new NotImplementedException();
        }

        public object ConvertTo(IXamlTypeConverterContext context, CultureInfo culture, object value, Type destinationType)
        {
            throw new NotImplementedException();
        }
    }
}<commit_msg>Accept named brushes in XAML.<commit_after>// -----------------------------------------------------------------------
// <copyright file="BrushConverter.cs" company="<NAME>">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------

namespace Perspex.Markup.Xaml.Converters
{
    using System;
    using System.Globalization;
    using System.Reflection;
    using System.Text;
    using Media;
    using Media.Imaging;
    using OmniXaml.TypeConversion;
    using Platform;

    public class BrushConverter : ITypeConverter
    {
        public bool CanConvertFrom(IXamlTypeConverterContext context, Type sourceType)
        {
            return true;
        }

        public bool CanConvertTo(IXamlTypeConverterContext context, Type destinationType)
        {
            return true;
        }

        public object ConvertFrom(IXamlTypeConverterContext context, CultureInfo culture, object value)
        {
            var colorString = (string)value;

            var color = DecodeColor(colorString);

            if (color != null)
            {
                return new SolidColorBrush(color.Value);
            }
            else
            {
                var member = typeof(Brushes).GetTypeInfo().GetDeclaredProperty(colorString);

                if (member != null)
                {
                    return (Brush)member.GetValue(null);
                }
            }

            throw new InvalidOperationException("Invalid color string.");
        }

        private static Color? DecodeColor(string colorString)
        {
            if (colorString[0] == '#')
            {
                var restOfValue = colorString.Remove(0, 1);

                if (restOfValue.Length == 8)
                {
                    var a = Convert.ToByte(restOfValue.Substring(0, 2), 16);
                    var r = Convert.ToByte(restOfValue.Substring(2, 2), 16);
                    var g = Convert.ToByte(restOfValue.Substring(6, 2), 16);
                    var b = Convert.ToByte(restOfValue.Substring(8, 2), 16);
                    return Color.FromArgb(a, r, g, b);
                }

                if (restOfValue.Length == 6)
                {
                    var r = Convert.ToByte(restOfValue.Substring(0, 2), 16);
                    var g = Convert.ToByte(restOfValue.Substring(2, 2), 16);
                    var b = Convert.ToByte(restOfValue.Substring(4, 2), 16);
                    return Color.FromRgb(r, g, b);
                }

                throw new InvalidOperationException("The color code format cannot be parsed");
            }

            return null;
        }

        public object ConvertTo(IXamlTypeConverterContext context, CultureInfo culture, object value, Type destinationType)
        {
            throw new NotImplementedException();
        }
    }
}
{'fraction_non_alphanumeric': 0.1343408900083963, 'fraction_numerical': 0.01343408900083963, 'mean_word_length': 1.478568456096546, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7485444', 'n_tokens_mistral': 1568, 'n_tokens_neox': 1425, 'n_words': 360}