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.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using ColorPicker.ExtensionMethods;

namespace ColorPicker
{
    /// <summary>
    /// Interaction logic for NewCurrent.xaml
    /// </summary>
    public partial class NewCurrent : UserControl
    {
        public static Type ClassType
        {
            get { return typeof(NewCurrent); }
        }

        public NewCurrent()
        {
            InitializeComponent();
        }

        #region NewColor

        public static DependencyProperty NewColorProperty = DependencyProperty.Register("NewColor", typeof(Color), ClassType, 
            new FrameworkPropertyMetadata(Colors.Gray, new PropertyChangedCallback(OnNewColorChanged)));
         [Category("ColorPicker")]
        public Color NewColor
        {
            get
            {
                return (Color)GetValue(NewColorProperty);
            }
            set
            {
                SetValue(NewColorProperty, value);
            }
        }

        private static void OnNewColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var nc = (NewCurrent)d;
            nc.rNew.Fill = new SolidColorBrush(((Color)e.NewValue).WithAlpha(nc.Alpha));

        }

        #endregion

        #region CurrentColor

        public static DependencyProperty CurrentColorProperty = DependencyProperty.Register("CurrentColor", typeof(Color), ClassType, 
            new FrameworkPropertyMetadata(Colors.Black, new PropertyChangedCallback(OnCurrentColorChanged)));

        /// <summary>
        /// The color being selected 
        /// </summary>
         [Category("ColorPicker")]
        public Color CurrentColor
        {
            get
            {
                return (Color)GetValue(CurrentColorProperty);
            }
            set
            {
                SetValue(CurrentColorProperty, value);
            }
        }

        private static void OnCurrentColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var nc = (NewCurrent) d;
            nc.rCurrent.Fill = new SolidColorBrush((Color) e.NewValue);
        }

        #endregion

        #region Alpha

        public static DependencyProperty AlphaProperty = DependencyProperty.Register("Alpha", typeof(byte), ClassType, new PropertyMetadata((byte)255, new PropertyChangedCallback(OnAlphaChanged)));
        /// <summary>
        /// The Alpha Component of the currrent color
        /// </summary>
          [Category("ColorPicker")]
        public byte Alpha
        {
            get
            {
                return (byte)GetValue(AlphaProperty);
            }
            set
            {
                SetValue(AlphaProperty, value);
            }
        }

        private static void OnAlphaChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var nc = (NewCurrent)d;
            nc.rNew.Fill = new SolidColorBrush(nc.NewColor.WithAlpha(Convert.ToByte(e.NewValue)));
        }

        #endregion

        //public bool ShowLabels { get; set; }

        #region orientation
        
        public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(Orientation), typeof(NewCurrent),
            new FrameworkPropertyMetadata(Orientation.Vertical, FrameworkPropertyMetadataOptions.AffectsMeasure, OnOrientationChanged));

        private static void OnOrientationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var or = (Orientation) e.NewValue ;
            if (or != (Orientation)e.OldValue)
            {
                var newCurrent = (NewCurrent) d;
                if (or == Orientation.Horizontal)
                {
                    newCurrent.SetOrientationToHorizontal();
                }
                else
                {
                    newCurrent.SetOrientationToVertical();
                }
            }


        }
        private void SetOrientationToHorizontal()
        {
            var star = new GridLength(50, GridUnitType.Star);
            var auto = GridLength.Auto;

            LayoutRoot.ColumnDefinitions[0].Width = star;
            LayoutRoot.ColumnDefinitions[1].Width = star;

            LayoutRoot.RowDefinitions[0].Height = auto;
            LayoutRoot.RowDefinitions[1].Height = star;
            LayoutRoot.RowDefinitions[2].Height = auto;
            LayoutRoot.RowDefinitions[3].Height = auto;

            tbCurrent.SetValue(Grid.RowProperty ,  0);
            tbCurrent.SetValue(Grid.ColumnProperty , 0);

            tbNew.SetValue(Grid.RowProperty,0);
            tbNew.SetValue(Grid.ColumnProperty, 1);


            rNew.SetValue(Grid.RowProperty, 1);
            rNew.SetValue(Grid.ColumnProperty, 1);
            rNewBG.SetValue(Grid.RowProperty, 1);
            rNewBG.SetValue(Grid.ColumnProperty, 1);

            rCurrent.SetValue(Grid.RowProperty, 1);
            rCurrent.SetValue(Grid.ColumnProperty, 0);
            rCurrentBG.SetValue(Grid.RowProperty, 1);
            rCurrentBG.SetValue(Grid.ColumnProperty, 0);
        }

        private void SetOrientationToVertical()
        {
            var star = new GridLength(35, GridUnitType.Star);
            var auto = GridLength.Auto;

            LayoutRoot.ColumnDefinitions[0].Width = star;
            LayoutRoot.ColumnDefinitions[1].Width = auto;

            LayoutRoot.RowDefinitions[0].Height = auto;
            LayoutRoot.RowDefinitions[1].Height = star;
            LayoutRoot.RowDefinitions[2].Height = star;
            LayoutRoot.RowDefinitions[3].Height = auto;

           
            tbNew.SetValue(Grid.RowProperty, 0);
            tbNew.SetValue(Grid.ColumnProperty, 0);

            rNew.SetValue(Grid.RowProperty, 1);
            rNew.SetValue(Grid.ColumnProperty, 0);
            rNewBG.SetValue(Grid.RowProperty, 1);
            rNewBG.SetValue(Grid.ColumnProperty, 0);

            rCurrent.SetValue(Grid.RowProperty, 2);
            rCurrent.SetValue(Grid.ColumnProperty, 0);
            rCurrentBG.SetValue(Grid.RowProperty, 2);
            rCurrentBG.SetValue(Grid.ColumnProperty, 0);


            tbCurrent.SetValue(Grid.RowProperty, 3);
            tbCurrent.SetValue(Grid.ColumnProperty, 0);
        
        


        }

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

#endregion


    }
}

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.140926.1 | Last Updated 26 Dec 2010
Article Copyright 2010 by KenJohnson
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid