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






Colors and Brushes

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

   
    
  








Related examples in the same category

1.Color AnimationColor Animation
2.Highlights the gradient origin and the gradient circle
3.using Color structuresusing Color structures
4.Use System Color to draw LineUse System Color to draw Line
5.This text uses a gradientThis text uses a gradient
6.Diagonal Linear GradientDiagonal Linear Gradient
7.A Partially Transparent ButtonA Partially Transparent Button
8.A Radial GradientA Radial Gradient
9.A Radial Gradient with an Offset CenterA Radial Gradient with an Offset Center
10.GradientStop with Transparent colorGradientStop with Transparent color
11.Simulating lighting effects with linear fillsSimulating lighting effects with linear fills
12.Color Converter With String FormatColor Converter With String Format
13.From a Hex string using ColorConverterFrom a Hex string using ColorConverter
14.From ScRGB values in the Color structureFrom ScRGB values in the Color structure
15.From sRGB values in the Color strutcureFrom sRGB values in the Color strutcure
16.From predefined color name in the Colors classFrom predefined color name in the Colors class
17.Fills the polylines with a light gray colorFills the polylines with a light gray color
18.Using SystemColors in CodeUsing SystemColors in Code
19.using system colors to create gradientsusing system colors to create gradients