【问题标题】:Combined solidcolor brush组合纯色画笔
【发布时间】:2009-05-25 09:26:30
【问题描述】:

有没有办法创建一种混合了 2 个纯色画笔的“纯色画笔”?

对于背景颜色,我希望能够对其他画笔使用 DynamicReference。而另一种颜色(在前面)可能是具有不透明度的静态颜色。

如果这真的没有道理,请随时要求澄清!

【问题讨论】:

    标签: wpf brush


    【解决方案1】:

    我遇到了同样的问题。我通常只为基本的深色、基本的灯光使用一个 xaml,然后为每种颜色重音(蓝色、红色等)使用一个 xaml。 The accent is slightly lookthrough which makes it darker when then darker theme is selected with a darker background.

    在创建具有辅助强调色的主题以在应用程序中具有更多对比度时(例如,选择浅色主题时为灰色,选择深色主题时为强调色),我需要用两种颜色构建画笔,否则我会必须为每种颜色创建一个深色和浅色主题。

    这是我使用的:

    <DrawingBrush x:Key="SecondaryAccentColorBrush" Viewport="0,0,1,1" TileMode="Tile">
        <DrawingBrush.Drawing>
            <DrawingGroup>
                <GeometryDrawing>
                    <GeometryDrawing.Geometry>
                        <RectangleGeometry Rect="0,0,1,1" />
                    </GeometryDrawing.Geometry>
                    <GeometryDrawing.Brush>
                        <SolidColorBrush Color="{DynamicResource AccentColor}"/>
                    </GeometryDrawing.Brush>
                </GeometryDrawing>
                <GeometryDrawing>
                    <GeometryDrawing.Geometry>
                        <RectangleGeometry Rect="0,0,1,1" />
                    </GeometryDrawing.Geometry>
                    <GeometryDrawing.Brush>
                        <SolidColorBrush Color="{DynamicResource Gray10}"/>
                    </GeometryDrawing.Brush>
                </GeometryDrawing>
            </DrawingGroup>
        </DrawingBrush.Drawing>
    </DrawingBrush>
    

    当主题切换时,“Gray10”的 alpha 在 00 和 FF 之间切换,因此画笔显示为灰色或强调色。

    【讨论】:

      【解决方案2】:

      不幸的是,WPF 不支持自定义画笔(画笔类型被标记为“内部”并且不能继承自),因此创建一个混合了两个画笔的画笔,可以像普通的 SolidColorBrush 一样从 XAML 中使用不可能。

      作为一种解决方法,您可以使用 MarkupExtension 来模拟自定义画笔的行为,它允许您使用 XAML 语法并提供自定义值,这允许我们使用内置的 SolidColorBrush(无需自定义画笔)设置为混合两种颜色时得到的值:

      /// <summary>
      /// Markup extension to mix two SolidColorBrushes together to produce a new SolidColorBrush.
      /// </summary>
      [MarkupExtensionReturnType(typeof(SolidColorBrush))]
      public class MixedColorBrush : MarkupExtension, INotifyPropertyChanged
      {
          /// <summary>
          /// The foreground mix color; defaults to white.  
          /// If not changed, the result will always be white.
          /// </summary>
          private SolidColorBrush foreground = Brushes.White;
      
          /// <summary>
          /// The background mix color; defaults to black.  
          /// If not set, the result will be the foreground color.
          /// </summary>
          private SolidColorBrush background = Brushes.Black;
      
          /// <summary>
          /// PropertyChanged event for WPF binding.
          /// </summary>
          public event PropertyChangedEventHandler PropertyChanged;
      
          /// <summary>
          /// Gets or sets the foreground mix color.
          /// </summary>
          public SolidColorBrush Foreground
          {
              get 
              { 
                  return this.foreground; 
              }
              set 
              { 
                  this.foreground = value; 
                  this.NotifyPropertyChanged("Foreground"); 
              }
          }
      
          /// <summary>
          /// Gets or sets the background mix color.
          /// </summary>
          public SolidColorBrush Background
          {
              get 
              { 
                  return this.background; 
              }
              set 
              { 
                  this.background = value; 
                  this.NotifyPropertyChanged("Background"); 
              }
          }
      
          /// <summary>
          /// Returns a SolidColorBrush that is set as the value of the 
          /// target property for this markup extension.
          /// </summary>
          /// <param name="serviceProvider">Object that can provide services for the markup extension.</param>
          /// <returns>The object value to set on the property where the extension is applied.</returns>
          public override object ProvideValue(IServiceProvider serviceProvider)
          {
              if (this.foreground != null && this.background != null)
              {
                  // Create a new brush as a composite of the old ones
                  // This does simple non-perceptual additive color, e.g 
                  // blue + red = magenta, but you can swap in a different
                  // algorithm to do subtractive color (red + yellow = orange)
                  return new SolidColorBrush(this.foreground.Color + this.background.Color);
              }
      
              // If either of the brushes was set to null, return an empty (white) brush.
              return new SolidColorBrush();
          }
      
          /// <summary>
          /// Raise the property changed event.
          /// </summary>
          /// <param name="propertyName">Name of the property which has changed.</param>
          protected void NotifyPropertyChanged(string propertyName)
          {
              if (this.PropertyChanged != null)
              {
                  this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
              }
          }
      }
      

      然后可以像使用普通画笔一样从 XAML 中使用它:

      <Grid>
          <Grid.Background>
              <local:MixedColorBrush Foreground="Blue" Background="Red"/>
          </Grid.Background>
      </Grid>
      

      或者使用标记扩展语法:

      <Grid Background="{local:MixedColorBrush Foreground=Blue, Background=Red}">
      

      这种方法的缺点是您不能使用 DynamicResource 或 StaticResource 引用将值绑定到应用程序中的其他资源。 MarkupExtension 不是 DependencyObject,资源绑定只对 DependencyObjects 起作用;内置的画笔是 DependencyObjects,这就是绑定与传统画笔一起使用的原因。

      【讨论】:

        【解决方案3】:

        从前景和背景画笔中获取颜色,将它们混合,然后根据生成的颜色创建一个新画笔。

        C# 中的示例:

        Color foreground = foregroundBrush.Color;
        Color background = backgroundBrush.Color;
        
        int opacity = 25;
        
        int r = (opacity * (foreground.R - background.R) / 100) + background.R;
        int g = (opacity * (foreground.G - background.G) / 100) + background.G;
        int b = (opacity * (foreground.B - background.B) / 100) + background.B;
        
        SolidColorBrush mixedBrush = new SolidColorBrush(Color.FromArgb(r, g, b));
        

        【讨论】:

          【解决方案4】:

          一种简单的方法(但可能没有优化), 以重复模式创建两种颜色的LinearGradientBrush,端点等于起点:

          <LinearGradientBrush SpreadMethod="Repeat" EndPoint="0,0">
                                          <GradientStop Color="Red" Offset="0" />
                                          <GradientStop Color="Yellow" Offset="1" />
                                      </LinearGradientBrush>
          

          这个给你一个橙色画笔。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-02-23
            • 2012-06-20
            • 1970-01-01
            相关资源
            最近更新 更多