Wednesday, May 07, 2008

WPF Color Pad

Ever wanted to see the list of named colors available in WPF's System.Windows.Media.Colors? And fancied using it's hexa code to get the colors like 'Tomato' in your web page?

Here is a simple WPF Application called ColorPad that lists out all the named colors. Click on a color to see its hexa code value. Remember to remove the first two letters/digits that represents the transparency (Alpha value) if you are using this color in html pages.


The application can be downloaded from here.

The XAML Source Code:

<Window x:Class="ColorPad.Window1" Icon="ColorPad.ico"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ColorPad" Height="500" Width="300" WindowStyle="SingleBorderWindow" ResizeMode="NoResize">
<Window.Resources>
<DataTemplate x:Key="tempGrid">
<Rectangle Width="60" Height="20" Fill="{Binding Name}" Stroke="#FF000000"/>
</DataTemplate>
</Window.Resources>

<Grid x:Name="LayoutRoot" ShowGridLines="True">
<StackPanel Orientation="Vertical">
<TextBlock x:Name="uxTitle" Text="ColorPad Version 1.0.0" Width="210" TextAlignment="Center" />
<ListView Height="400" Width="210" Name="uxColorGrid">
<ListView.View>
<GridView x:Name="uxColorGridView">
<GridViewColumn Header="Color Name" DisplayMemberBinding="{Binding Path=Name}" />
<GridViewColumn CellTemplate="{DynamicResource tempGrid}" Header="Color" />
</GridView>
</ListView.View>
</ListView>
<TextBlock x:Name="uxFooter" Visibility="Collapsed" Width="200" Background="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
<TextBox x:Name="uxColorCode" Width="210" TextAlignment="Center" />
</StackPanel>
</Grid>
</Window>


And the Code Behind in C#:

using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Collections.Generic;

namespace ColorPad
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();

uxColorGrid.ItemsSource = typeof(Colors).GetProperties();
uxColorGrid.SelectionChanged += new SelectionChangedEventHandler(uxColorGrid_SelectionChanged);
}

void uxColorGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
PropertyInfo c = uxColorGrid.SelectedItem as PropertyInfo;
uxFooter.Text = c.Name;

SolidColorBrush scb = uxFooter.Background as SolidColorBrush;
uxColorCode.Text = scb.Color.ToString();
uxTitle.Foreground = uxFooter.Background;
}
}
}

1 comment:

  1. i think im gonna make a windows-gadget out of it =)

    ReplyDelete