Mega Code Archive

 
Categories / C# / WPF
 

Colors and Brushes

<Window x:Class="WpfApplication1.ColorExample"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   Title="Color Example" Height="300" Width="300">   <StackPanel>     <TextBlock Text="Select Color"/>     <ListBox Name="listBox1" SelectionChanged="listBox1SelectionChanged"/>     <TextBlock Text="Show selected color:" Margin="5,5,5,0" />     <Rectangle x:Name="rect1" Stroke="Blue" Fill="AliceBlue"/>     <TextBlock Text="Opacity:" Margin="5,5,5,0" />     <TextBox x:Name="textBox" HorizontalAlignment="Left" TextAlignment="Center" Text="1" Width="50" Margin="5,5,5,8" />     <TextBlock FontWeight="Bold" Text="sRGB Information:" Margin="5,5,5,2" />     <TextBlock FontWeight="Bold" Text="ScRGB Information:" Margin="5,5,5,2" />   </StackPanel> </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; 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());         }     } }