示例#1
0
        protected object NormalConvert(object value, Type targetType, object parameter, CultureInfo culture, ExOpration operation)
        {
            //解析参数
            Boolean TryParse2Byte(String str, out Byte result)
            {
                if (Double.TryParse(str, out double dr))
                {
                    if (dr >= 0 && dr <= 1)
                    {
                        result = (Byte)(Byte.MaxValue * dr);
                        return(true);
                    }
                    if (dr > 1 && dr <= 255)
                    {
                        result = (Byte)dr;
                        return(true);
                    }
                }
                result = 0;
                return(false);
            }

            Color devColor;

            if (parameter is String)
            {
                devColor = defaultDevColor;
                String[] vs   = ((String)parameter).Split(new char[] { ';' }, 4, StringSplitOptions.RemoveEmptyEntries);
                bool     flag = true;
                int      len  = vs.Length;
                Byte[]   bs   = new Byte[len];
                for (int i = 0; i < len; i++)
                {
                    if (!TryParse2Byte(vs[i], out bs[i]))
                    {
                        flag = false;
                        break;
                    }
                }
                if (flag)
                {
                    if (len == 1)
                    {
                        devColor = Color.FromRgb(bs[0], bs[0], bs[0]);
                    }
                    else if (len == 2)
                    {
                        devColor = Color.FromArgb(bs[0], bs[1], bs[1], bs[1]);
                    }
                    else if (len == 3)
                    {
                        devColor = Color.FromRgb(bs[0], bs[1], bs[2]);
                    }
                    else if (len == 4)
                    {
                        devColor = Color.FromArgb(bs[0], bs[1], bs[2], bs[3]);
                    }
                    else
                    {
                        devColor = defaultDevColor;
                    }
                }
            }
            else
            {
                devColor = defaultDevColor;
            }

            //转换
            if (targetType == typeof(Color))
            {
                if (value is Color)
                {
                    return(ExColorOpration((Color)value, devColor, operation));
                }
                if (value is SolidColorBrush)
                {
                    return(ExColorOpration(((SolidColorBrush)value).Color, devColor, operation));
                }
            }
            else if (targetType == typeof(Brush))
            {
                if (value is Color)
                {
                    return(new SolidColorBrush(ExColorOpration((Color)value, devColor, operation)));
                }
                if (value is SolidColorBrush)
                {
                    SolidColorBrush scb = value as SolidColorBrush;
                    SolidColorBrush ns  = scb.CloneCurrentValue();
                    ns.Color = ExColorOpration(scb.Color, devColor, operation);
                    return(ns);
                }
                if (value is GradientBrush)
                {
                    GradientBrush gb = value as GradientBrush;
                    GradientBrush ng = gb.CloneCurrentValue();
                    foreach (var g in ng.GradientStops)
                    {
                        g.Color = ExColorOpration(g.Color, devColor, operation);
                    }
                    return(ng);
                }
            }
            return(null);
        }