Colors and Brushes : Color « Windows Presentation Foundation « C# / CSharp Tutorial





<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());
        }
    }
}
WPF Colors And Brushes










24.82.Color
24.82.1.Color AnimationColor Animation
24.82.2.Highlights the gradient origin and the gradient circleHighlights the gradient origin and the gradient circle
24.82.3.using Color structuresusing Color structures
24.82.4.Use System Color to draw LineUse System Color to draw Line
24.82.5.This text uses a gradientThis text uses a gradient
24.82.6.Diagonal Linear GradientDiagonal Linear Gradient
24.82.7.A Partially Transparent ButtonA Partially Transparent Button
24.82.8.A Radial GradientA Radial Gradient
24.82.9.A Radial Gradient with an Offset CenterA Radial Gradient with an Offset Center
24.82.10.GradientStop with Transparent colorGradientStop with Transparent color
24.82.11.Simulating lighting effects with linear fillsSimulating lighting effects with linear fills
24.82.12.Color Converter With String FormatColor Converter With String Format
24.82.13.Colors and BrushesColors and Brushes
24.82.14.From a Hex string using ColorConverterFrom a Hex string using ColorConverter
24.82.15.From ScRGB values in the Color structureFrom ScRGB values in the Color structure
24.82.16.From sRGB values in the Color strutcureFrom sRGB values in the Color strutcure
24.82.17.From predefined color name in the Colors classFrom predefined color name in the Colors class
24.82.18.Fills the polylines with a light gray colorFills the polylines with a light gray color
24.82.19.Using SystemColors in CodeUsing SystemColors in Code
24.82.20.using system colors to create gradientsusing system colors to create gradients
24.82.21.Using Colors.Red, Colors.Indigo and Colors.Violet to create rainbow