Пример #1
0
        public override object ConvertFrom(
            ITypeDescriptorContext context,
            CultureInfo culture,
            object value)
        {
            if (value == null)
            {
                throw new InvalidOperationException(
                          $"Cannot convert from a null value to a MaterialDescriptor.");
            }

            var strVal = value.ToString().Trim();

            if (_literalDescriptorRegex.IsMatch(strVal))
            {
                var match = _literalDescriptorRegex.Match(strVal);

                var swatchName = match.Groups["swatchName"].Value;
                if (!Enum.TryParse <SwatchClassifier>(swatchName, out var classifier))
                {
                    throw new FormatException(
                              $"The swatch name {swatchName.Quote()} is not a valid defined member of the enum " +
                              $"{typeof(SwatchClassifier).Name.SQuote()}.");
                }

                var swatch = GlobalResources.Palette.GetSwatch(classifier);

                var luminosityStr = match.Groups["luminosity"].Value;
                var luminosity    = Luminosity.Parse(luminosityStr);

                var material = swatch.GetMaterial(luminosity);

                var opacityStr = match.Groups["opacity"]?.Value ?? "1";
                var opacity    = double.Parse(opacityStr);

                return(new LiteralMaterialDescriptor(
                           material,
                           opacity));
            }
            if (_luminosityDescriptorRegex.IsMatch(strVal))
            {
                var match = _luminosityDescriptorRegex.Match(strVal);

                var luminosityStr = match.Groups["luminosity"].Value;
                var luminosity    = Luminosity.Parse(luminosityStr);

                var opacityStr = match.Groups["opacity"]?.Value ?? "1";
                var opacity    = double.Parse(opacityStr);

                return(new LuminosityMaterialDescriptor(
                           luminosity,
                           opacity));
            }
            else
            {
                throw new FormatException(
                          $"The value {strVal.Quote()} cannot be converted to a MaterialDescriptor.");
            }
        }
Пример #2
0
        public override object ConvertTo(
            ITypeDescriptorContext context,
            CultureInfo culture,
            object value,
            Type destinationType)
        {
            switch (value)
            {
            case string @string:
            {
                if (destinationType == typeof(InstanceDescriptor))
                {
                    return(Luminosity.Parse(@string));
                }
                else if (destinationType == typeof(Luminosity))
                {
                    return(Luminosity.Parse(@string));
                }
                else if (destinationType == typeof(string))
                {
                    return(@string);
                }

                throw new NotSupportedException();
            }

            case Luminosity luminosity:
            {
                if (destinationType == typeof(InstanceDescriptor))
                {
                    return(luminosity);
                }
                else if (destinationType == typeof(Luminosity))
                {
                    return(luminosity);
                }
                else if (destinationType == typeof(string))
                {
                    return(luminosity.ToString());
                }

                throw new NotSupportedException();
            }

            default:
            {
                throw new NotSupportedException();
            }
            }
        }
Пример #3
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            try
            {
                var strVal = value.RequireType <string>().Trim();

                if (strVal.StartsWith("->"))
                {
                    var postString     = strVal.Substring(2);
                    var materialString = postString;
                    var opacity        = 1.0;
                    if (postString.Contains(','))
                    {
                        var seperatorIndex = postString.IndexOf(',');                         // postString.Length -
                        materialString = postString.Substring(0, seperatorIndex);
                        var opacitystr = postString.Substring(seperatorIndex + 1);
                        opacity = Convert.ToDouble(opacitystr);
                    }
                    var fields = typeof(LegacyPalette).GetFields(BindingFlags.Public | BindingFlags.Static);

                    var parsedMaterial = LegacyPalette.Black000;
                    foreach (var field in fields.Where(field => field.Name == materialString))
                    {
                        parsedMaterial = field.GetValue(null).RequireType <SolidColorBrush>();
                    }
                    return(new LiteralMaterialDescriptor(parsedMaterial, opacity));
                    //var materialString = ;
                }
                else
                {
                    var luminosityStr = strVal.Substring(0, 4);
                    var lum           = Luminosity.Parse(luminosityStr);

                    var opacity = strVal.Substring(4);
                    if (!opacity.StartsWith("O"))
                    {
                        return(new LuminosityMaterialDescriptor(lum));
                    }
                    var ostr = opacity.Substring(1);
                    var oval = Convert.ToInt32(ostr);
                    return(new LuminosityMaterialDescriptor(lum, ((double)oval) / 100));
                }
            }
            catch
            {
                throw new Exception("convertfrom failed");
            }
        }
Пример #4
0
        public override object ConvertFrom(
            ITypeDescriptorContext context,
            CultureInfo culture,
            object value)
        {
            if (value == null)
            {
                return(null);
            }

            var propertyType = context?.PropertyDescriptor?.PropertyType;

            if (propertyType == null)
            {
                throw new NotSupportedException();
            }

            switch (value)
            {
            case string @string:
            {
                if (propertyType == typeof(InstanceDescriptor))
                {
                    return(Luminosity.Parse(@string));
                }
                else if (propertyType == typeof(Luminosity))
                {
                    return(Luminosity.Parse(@string));
                }
                else if (propertyType == typeof(string))
                {
                    return(@string);
                }

                throw new NotSupportedException();
            }

            case Luminosity luminosity:
            {
                if (propertyType == typeof(InstanceDescriptor))
                {
                    return(luminosity);
                }
                else if (propertyType == typeof(Luminosity))
                {
                    return(luminosity);
                }
                else if (propertyType == typeof(string))
                {
                    return(luminosity.ToString());
                }

                throw new NotSupportedException();
            }

            default:
            {
                throw new NotSupportedException();
            }
            }
        }