Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

WPF Color Picker Construction Kit

, 26 Dec 2010
A WPF color picker (like Adobe's) constructed in a modular fashion for easy modification.
ColorPickerDemo.zip
ColorPicker.dll
ColorPickerControls.dll
ColorPickerDemo.exe
ColorPickerSource.zip
ColorPickerDemo
ColorPicker
ColorModels
CMY
CMYK
HSB
Lab
RGB
ExtensionMethods
Properties
Settings.settings
Resources
ColorPickerControls
Chips
Dialogs
Pickers
Properties
Settings.settings
ColorPickerDemo
ColorPickerDemo.ico
Properties
Settings.settings
using System;
using System.Collections.Generic;
using System.ComponentModel;
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;

namespace ColorPicker
{
    /// <summary>
    /// Interaction logic for HexDisplay.xaml
    /// </summary>
    public partial class HexDisplay : UserControl
    {

        public enum EAlphaByteVisibility
        {
            visible,
            hidden, 
            auto //show if Alpha byte not ff
        }
        public static Type ClassType
        {
            get { return typeof(HexDisplay); }
        }
        public event EventHandler<EventArgs<Color>> ColorChanged;

        #region Color

        public static DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), ClassType,
             new FrameworkPropertyMetadata(Colors.Black, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnColorChanged));

        [ Category("ColorPicker")]
        public Color Color
        {
            get
            {
                return (Color)GetValue(ColorProperty);
            }
            set
            {
                SetValue(ColorProperty, value);
            }
        }

        private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var c = (Color)e.NewValue;
            var rd = (HexDisplay)d;
            rd.OnColorChanged(c);
        }

        private void OnColorChanged(Color c)
        {
            string colorText = "";

            if (IsNumberSignIncludedInText)
            {
                colorText="#";
            }
           switch (AlphaByteVisibility )
           {
               case EAlphaByteVisibility.visible:
                    colorText += c.ToString().Substring(1);
                   break;
               case EAlphaByteVisibility.hidden :
                    colorText += c.ToString().Substring(3);
                   break;
               case EAlphaByteVisibility.auto :
                   break;
           }


            txtHex.Text = colorText;
            if (ColorChanged != null)
            {
                ColorChanged(this, new EventArgs<Color>(c));
            }
        }

        #endregion

        #region Text

        public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), ClassType, new PropertyMetadata("", new PropertyChangedCallback(OnTextChanged)));

        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var display = (HexDisplay) d;
            var oldtext = (string) e.OldValue;
            var newText = (String) e.NewValue;

        }

        #endregion

        #region IsNumberSignIncludedInText

        public static DependencyProperty IsNumberSignIncludedInTextProperty = DependencyProperty.Register("IsNumberSignIncludedInText", typeof(bool), ClassType, 
            new PropertyMetadata(false, OnIsNumberSignIncludedInTextChanged));

         [Category("ColorPicker")]
        public bool IsNumberSignIncludedInText
        {
            get
            {
                return (bool)GetValue(IsNumberSignIncludedInTextProperty);
            }
            set
            {
                SetValue(IsNumberSignIncludedInTextProperty, value);
            }
        }

        private static void OnIsNumberSignIncludedInTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }

        #endregion

        #region AlphaByteVisibility

        public static DependencyProperty AlphaByteVisibilityProperty = DependencyProperty.Register("AlphaByteVisibility", typeof(EAlphaByteVisibility), ClassType,
            new PropertyMetadata(EAlphaByteVisibility.hidden, OnAlphaByteVisibilityChanged));
         [Category("ColorPicker")]
        public EAlphaByteVisibility AlphaByteVisibility
        {
            get
            {
                return (EAlphaByteVisibility)GetValue(AlphaByteVisibilityProperty);
            }
            set
            {
                SetValue(AlphaByteVisibilityProperty, value);
            }
        }

        private static void OnAlphaByteVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        }

        #endregion



        public HexDisplay()
        {
            InitializeComponent();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

KenJohnson
Software Developer (Senior)
United States United States
Written software for what seems like forever. I'm currenly infatuated with WPF. Hopefully my affections are returned.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 26 Dec 2010
Article Copyright 2010 by KenJohnson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid