2009-05-25 25 views
5

Có cách nào để tạo ra một loại "solidcolorbrush" mà là một hỗn hợp của 2 bàn chải solidcolor?Cọ kết hợp solidcolor

Đối với màu sau, tôi muốn có thể sử dụng DynamicReference cho một số cọ vẽ khác. Trong khi màu khác (ở phía trước) có thể là một màu tĩnh với độ mờ đục.

Hãy yêu cầu làm rõ nếu điều này không thực sự làm cho sence!

Trả lời

4

Thật không may, bàn chải tùy chỉnh không được hỗ trợ trong WPF (các loại bàn chải được đánh dấu 'nội' và không thể được thừa hưởng từ), do đó tạo ra một bàn chải đó là một hỗn hợp của hai bàn chải có thể được sử dụng từ XAML như một SolidColorBrush bình thường là không thể. Để giải quyết sự cố, bạn có thể sử dụng MarkupExtension để mô phỏng hành vi của một cọ vẽ tùy chỉnh, cho phép bạn sử dụng cú pháp XAML và cung cấp giá trị tùy chỉnh, cho phép chúng tôi sử dụng SolidColorBrush tích hợp sẵn (không cần bàn chải tùy chỉnh).) để thiết lập các giá trị mà bạn nhận được khi trộn hai màu:

/// <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)); 
     } 
    } 
} 

nào sau đó có thể được sử dụng từ XAML như bạn làm một bàn chải bình thường:

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

Hoặc bằng cách sử dụng cú pháp mở rộng đánh dấu:

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

Nhược điểm của phương pháp này là bạn không thể sử dụng tham chiếu DynamicResource hoặc StaticResource để liên kết các giá trị với các tài nguyên khác trong ứng dụng của bạn. MarkupExtension không phải là DependencyObject và liên kết tài nguyên chỉ hoạt động trên DependencyObjects; các Brushes tích hợp là DependencyObjects, đó là lý do tại sao việc liên kết hoạt động với các cọ vẽ truyền thống.

3

Lấy các màu từ bàn chải nền và nền sau, trộn chúng và tạo một cọ vẽ mới từ màu kết quả.

Ví dụ trong 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)); 
+0

Ví dụ tốt ...nhưng tôi đã hy vọng phiên bản XAML này ... – Entrodus

6

Tôi đã gặp phải sự cố tương tự. Tôi thường chỉ sử dụng một xaml mỗi cho bóng tối cơ bản, đèn cơ bản, và sau đó một cho mỗi màu sắc giọng (xanh, đỏ vv). Giọng là hơi nhìn mà làm cho nó tối hơn khi sau đó chủ đề tối hơn được chọn với một nền tối hơn.

Khi tạo chủ đề có màu nhấn phụ để có độ tương phản cao hơn trong ứng dụng (ví dụ: màu xám khi chủ đề ánh sáng được chọn, màu nhấn khi chủ đề tối), tôi cần phải tạo một cọ vẽ bằng hai màu. sẽ phải tạo ra một chủ đề tối và sáng cho mỗi màu.

Dưới đây là những gì tôi sử dụng:

<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> 

Khi chủ đề được bật, các alpha của "Gray10" công tắc giữa 00 và FF, do đó bàn chải cho thấy một trong hai màu xám hoặc màu nhấn.

3

Một cách dễ dàng để làm điều đó (nhưng có lẽ không được tối ưu hóa), tạo LinearGradientBrush của hai màu sắc trong chế độ lặp lại với Endpoint bằng để bắt đầu điểm:

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

một này mang đến cho bạn một bàn chải da cam.