Colors and Brushes

image_pdfimage_print













//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Reflection;
using System.Collections.Generic;

namespace WpfApplication1
{
public partial class ColorExample : Window
{
private Color color;
SolidColorBrush colorBrush = new SolidColorBrush();
public ColorExample()
{
InitializeComponent();
Type colorsType = typeof(Colors);
foreach (PropertyInfo property in colorsType.GetProperties())
{
listBox1.Items.Add(property.Name);
color = Colors.AliceBlue;
listBox1.SelectedIndex = 0;
ColorInfo();
}
}
private void listBox1SelectionChanged(object sender, EventArgs e)
{
string colorString = listBox1.SelectedItem.ToString();
color = (Color)ColorConverter.ConvertFromString(colorString);
float opacity = Convert.ToSingle(textBox.Text);
if (opacity > 1.0f)
opacity = 1.0f;
else if (opacity < 0.0f) opacity = 0.0f; color.ScA = opacity; ColorInfo(); } private void ColorInfo() { rect1.Fill = new SolidColorBrush(color); Console.WriteLine("Alpha = " + color.A.ToString()); Console.WriteLine("Red = " + color.R.ToString()); Console.WriteLine("Green = " + color.G.ToString()); Console.WriteLine("Blue = " + color.B.ToString()); string rgbHex = string.Format("{0:X2}{1:X2}{2:X2}{3:X2}", color.A, color.R, color.G, color.B); Console.WriteLine("ARGB = #" + rgbHex); Console.WriteLine("ScA = " + color.ScA.ToString()); Console.WriteLine("ScR = " + color.ScR.ToString()); Console.WriteLine("ScG = " + color.ScG.ToString()); Console.WriteLine("ScB = " + color.ScB.ToString()); } } } [/csharp]

From a Hex string using ColorConverter

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.SolidColorBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SolidColorBrush Example" Height="415" Width="270">
  <Canvas Margin="5">
    <StackPanel>
      <TextBlock Margin="0,10,0,5">
        From Hex string using ColorConverter:
      </TextBlock>
      <Rectangle x:Name="rect5" Width="100" Height="30"
        Stroke="Blue" />



    </StackPanel>
  </Canvas>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;


namespace WpfApplication1
{
    public partial class SolidColorBrushExample : Window
    {
        public SolidColorBrushExample()
        {
            InitializeComponent();
            SolidColorBrush brush = new SolidColorBrush();

            // From a Hex string using ColorConverter: 
            brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#CBFFFFAA"));
            rect5.Fill = brush;
        }

    }
}

   
    
     


From ScRGB values in the Color structure

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.SolidColorBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SolidColorBrush Example" Height="415" Width="270">
  <Canvas Margin="5">
    <StackPanel>
      <TextBlock Margin="0,10,0,5">
        From SsRGB value in the Color structure:
      </TextBlock>
      <Rectangle x:Name="rect4" Width="100" Height="30"
        Stroke="Blue" />



    </StackPanel>
  </Canvas>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;


namespace WpfApplication1
{
    public partial class SolidColorBrushExample : Window
    {
        public SolidColorBrushExample()
        {
            InitializeComponent();
            SolidColorBrush brush = new SolidColorBrush();

            // From ScRGB values in the Color structure: 
            brush = new SolidColorBrush(Color.FromScRgb(0.5f, 0.7f, 0.0f, 0.5f));
            rect4.Fill = brush;



        }

    }
}

   
    
     


From sRGB values in the Color strutcure

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.SolidColorBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SolidColorBrush Example" Height="415" Width="270">
  <Canvas Margin="5">
    <StackPanel>
      <TextBlock Margin="0,10,0,5">
        From sRGB value in the Color structure:
      </TextBlock>
      <Rectangle x:Name="rect3" Width="100" Height="30"
        Stroke="Blue" />



    </StackPanel>
  </Canvas>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;


namespace WpfApplication1
{
    public partial class SolidColorBrushExample : Window
    {
        public SolidColorBrushExample()
        {
            InitializeComponent();
            SolidColorBrush brush = new SolidColorBrush();

            // From sRGB values in the Color strutcure: 
            brush = new SolidColorBrush(Color.FromArgb(100, 0, 0, 255));
            rect3.Fill = brush;


        }

    }
}

   
    
     


From predefined color name in the Colors class

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.SolidColorBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="SolidColorBrush Example" Height="415" Width="270">
  <Canvas Margin="5">
    <StackPanel>
      <TextBlock Margin="0,10,0,5">
        From predefined color name in the Colors class:
      </TextBlock>
      <Rectangle x:Name="rect2" Width="100" Height="30"
        Stroke="Blue" />


    </StackPanel>
  </Canvas>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;


namespace WpfApplication1
{
    public partial class SolidColorBrushExample : Window
    {
        public SolidColorBrushExample()
        {
            InitializeComponent();
            SolidColorBrush brush = new SolidColorBrush();

            // From predefined color name in the Colors class: 
            brush = new SolidColorBrush(Colors.Green);
            rect2.Fill = brush;

        }

    }
}

   
    
     


Fills the polylines with a light gray color

image_pdfimage_print










//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfApplication1
{
public partial class Polygons : Window
{
public Polygons()
{
InitializeComponent();
for (int i = 0; i < 71; i++) { double x = i * Math.PI; double y = 40 + 30 * Math.Sin(x / 10); polygon1.Points.Add(new Point(x, y)); } } } } [/csharp]

Using SystemColors in Code

image_pdfimage_print


   
  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SystemColorsAndBrushes_csharp.Window1"
    Title="System Colors" >
   <Window.Resources>
    <Style TargetType="{x:Type Rectangle}">
      <Setter Property="Margin" Value="10,0,10,0"/>
      <Setter Property="HorizontalAlignment" Value="Left"/>
      <Setter Property="Height" Value="20"/>
      <Setter Property="Width" Value="120"/>
      <Setter Property="Stroke" Value="Black"/>
      <Setter Property="StrokeThickness" Value="1"/>
    </Style>
    <Style TargetType="{x:Type TextBlock}">
      <Setter Property="Margin" Value="10,20,10,0"/>
    </Style>
    <Style TargetType="{x:Type Button}">
      <Setter Property="Margin" Value="10,0,10,0"/>
      <Setter Property="HorizontalAlignment" Value="Left"/>
    </Style>
    </Window.Resources>
    <ScrollViewer>

    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition Width="5" />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
        <RowDefinition />
      </Grid.RowDefinitions>
      <Rectangle Grid.Column="1" Grid.Row="0" 
        HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Fill="Black"
        RadiusX="10" RadiusY="10" />
    
      <StackPanel Name="systemBrushesPanel" Background="White" Grid.Row="0" Grid.Column="0"/> 
      <StackPanel Name="gradientExamplePanel" Background="White" Grid.Row="0" Grid.Column="2"/>

  </Grid>
  
  </ScrollViewer>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Data;

namespace SystemColorsAndBrushes_csharp
{
    public partial class Window1 : Window
    {
        public Window1() {
            InitializeComponent();
            System.Windows.Controls.TextBlock t = new System.Windows.Controls.TextBlock();
            t.Text = "ActiveBorder";
            System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.ActiveBorderBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "ActiveCaption";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.ActiveCaptionBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "ActiveCaptionText";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.ActiveCaptionTextBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "AppWorkspace";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.AppWorkspaceBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "Control";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.ControlBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "ControlDark";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.ControlDarkBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "ControlDarkDark";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.ControlDarkDarkBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "ControlLight";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.ControlLightBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "ControlLightLight";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.ControlLightLightBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "ControlText";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.ControlTextBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "Desktop";
            r = new System.Windows.Shapes.Rectangle();

            r.SetResourceReference(System.Windows.Shapes.Shape.FillProperty, System.Windows.SystemColors.DesktopBrushKey);

            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "GradientActiveCaption";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.GradientActiveCaptionBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "GradientInactiveCaption";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.GradientInactiveCaptionBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "GrayText";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.GrayTextBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "Highlight";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.HighlightBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "HighlightText";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.HighlightTextBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "HotTrack";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.HotTrackBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "InactiveBorder";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.InactiveBorderBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "InactiveCaption";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.InactiveCaptionBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "InactiveCaptionText";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.InactiveCaptionTextBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "Info";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.InfoBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "InfoText";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.InfoTextBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "Menu";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.MenuBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "MenuBar";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.MenuBarBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "MenuHighlight";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.MenuHighlightBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "MenuText";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.MenuTextBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "ScrollBar";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.ScrollBarBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "Window";
            r = new System.Windows.Shapes.Rectangle();
            r.Fill = SystemColors.WindowBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(r);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "WindowFrame";
            System.Windows.Controls.Button b = new System.Windows.Controls.Button();
            b.Width = 120;
            b.Height = 20;
            b.Background = SystemColors.WindowFrameBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(b);

            t = new System.Windows.Controls.TextBlock();
            t.Text = "WindowText";
            b = new System.Windows.Controls.Button();
            b.Width = 120;
            b.Height = 20;
            b.Background = SystemColors.WindowTextBrush;
            systemBrushesPanel.Children.Add(t);
            systemBrushesPanel.Children.Add(b);
        }

    }
}