From sRGB values in the Color strutcure


   
  
<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 ScRGB values in the Color structure


   
  
<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 a Hex string using ColorConverter


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

    }
}

   
    
     


Colors and Brushes













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

Color Converter With String Format


   
  

<Window x:Class="ColorConverter.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:local="clr-namespace:ColorConverter"
    Title="Colors" Height="400" Width="400">
    <Window.Resources>
        <local:ColorMultiConverter x:Key="colorConverter"/>
        <local:ColorNameValueConverter x:Key="colorNameConverter"/>
    </Window.Resources>
    <Grid>
        <Label Name="label1">Red:</Label>
        <Slider Name="redSlider"/>
        <Label Name="label2">Green:</Label>
        <Slider Name="greenSlider"/>
        <Label Name="label3">Blue:</Label>
        <Slider Name="blueSlider"/>
        <Grid Name="colorBlock">
                <Grid.Background>
                    <SolidColorBrush>
                        <SolidColorBrush.Color>
                            <MultiBinding Converter="{StaticResource colorConverter}">
                                <Binding ElementName="redSlider" Path="Value"/>
                                <Binding ElementName="greenSlider" Path="Value"/>
                                <Binding ElementName="blueSlider" Path="Value"/>
                            </MultiBinding>
                        </SolidColorBrush.Color>
                    </SolidColorBrush>
                </Grid.Background>
                <Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" VerticalAlignment="Center">
                    <Label.Foreground>
                        <SolidColorBrush>
                            <SolidColorBrush.Color>
                                <MultiBinding Converter="{StaticResource colorConverter}" ConverterParameter="inverse">
                                    <Binding ElementName="redSlider" Path="Value"/>
                                    <Binding ElementName="greenSlider" Path="Value"/>
                                    <Binding ElementName="blueSlider" Path="Value"/>
                                </MultiBinding>
                            </SolidColorBrush.Color>
                        </SolidColorBrush>
                    </Label.Foreground>
                    <TextBlock>
          <TextBlock.Text>
            <MultiBinding StringFormat="Red={0}, Green={1}, Blue={2}">
              <Binding ElementName="redSlider" Path="Value" />
              <Binding ElementName="greenSlider" Path="Value" />
              <Binding ElementName="blueSlider" Path="Value" />
            </MultiBinding>
          </TextBlock.Text>
                    </TextBlock>
                </Label>
        </Grid>
        <TextBox>
            <TextBox.Text>
                <MultiBinding StringFormat=" R{0} G{1} B{2}">
                    <Binding ElementName="redSlider" Path="Value" />
                    <Binding ElementName="greenSlider" Path="Value" />
                    <Binding ElementName="blueSlider" Path="Value" />
                </MultiBinding>
            </TextBox.Text>
        </TextBox>
    </Grid>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Reflection;
namespace ColorConverter
{
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();
    }

    private void colorBlock_MouseDown(object sender, MouseButtonEventArgs e)
    {
      Random rnd = new Random((int)DateTime.Now.Ticks);

      Color newColor = Color.FromRgb((byte)rnd.Next(255), (byte)rnd.Next(255), (byte)rnd.Next(255));
      colorBlock.Background = new SolidColorBrush(newColor);
    }
  }
  class ColorMultiConverter : IMultiValueConverter
  {

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      bool inverse = (parameter != null) &amp;&amp; (string.Compare(parameter.ToString(), "inverse", true) == 0);

      byte R = System.Convert.ToByte((double)values[0]);
      byte G = System.Convert.ToByte((double)values[1]);
      byte B = System.Convert.ToByte((double)values[2]);

      Color newColor;
      if (inverse)
        newColor = Color.FromRgb((byte)(255 - R), (byte)(255 - G), (byte)(255 - B));
      else
        newColor = Color.FromRgb(R, G, B);

      return newColor;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
      Color oldColor = (Color)value;
      double R = (double)oldColor.R;
      double G = (double)oldColor.G;
      double B = (double)oldColor.B;

      return new object[] { R, G, B };
    }

  }
  class ColorNameValueConverter : IValueConverter
  {
    private Dictionary<Color, string> namedColors = new Dictionary<Color, string>();

    public ColorNameValueConverter()
    {
      PropertyInfo[] colorProperties = typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public);
      Color stepColor;
      foreach (PropertyInfo pi in colorProperties)
      {
        if (pi.PropertyType == typeof(Color))
        {
          stepColor = (Color)pi.GetValue(null, null);
          namedColors[stepColor] = pi.Name;
        }
      }

    }


    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      Color col = (Color)value;

      if(namedColors.ContainsKey(col))
        return namedColors[col];

      return DependencyProperty.UnsetValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      throw new NotImplementedException();
    }

  }
  
}

   
    
     


Simulating lighting effects with linear fills


   
             

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Center" VerticalAlignment="Center">

    <Grid Width="80" Height="26">
        <Grid.RowDefinitions>
            <RowDefinition Height="2*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Rectangle Grid.RowSpan="2" RadiusX="13" RadiusY="13">
            <Rectangle.Fill>
                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                    <GradientStop Color="Green" Offset="0" />
                    <GradientStop Color="DarkGreen" Offset="0.5" />
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</Page>

   
    
    
    
    
    
    
    
    
    
    
    
    
     


GradientStop with Transparent color


   
       

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      HorizontalAlignment="Center" VerticalAlignment="Center">

<Viewbox>
    <Grid Margin="0,0,5,0" >
      <Ellipse>
        <Ellipse.Fill>
          <RadialGradientBrush Center="0.5,0.1" GradientOrigin="0.5,0.1" RadiusX="0.7" RadiusY="0.5">
            <GradientStop Color="#efff" Offset="0" />
            <GradientStop Color="Transparent" Offset="1" />
          </RadialGradientBrush>
        </Ellipse.Fill>
      </Ellipse>
    </Grid>
</Viewbox>

</Page>