Click here to Skip to main content
12,508,996 members (55,947 online)
Click here to Skip to main content

Stats

65.5K views
5.5K downloads
71 bookmarked
Posted

WPF Color Picker Construction Kit

, 26 Dec 2010 CPOL
A WPF color picker (like Adobe's) constructed in a modular fashion for easy modification.
ColorPicker.dll
ColorPickerControls.dll
ColorPickerDemo.exe
ColorPickerDemo
ColorPicker
ColorModels
CMY
CMYK
HSB
Lab
RGB
ExtensionMethods
Properties
Resources
ColorPickerControls
Chips
Dialogs
Pickers
Properties
ColorPickerDemo
ColorPickerDemo.ico
Properties
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;
using ColorPicker;
namespace ColorPicker.ColorModels.HSB
{
    /// <summary>
    /// Interaction logic for HsbDisplay.xaml
    /// </summary>
    public partial class HsbDisplay : UserControl
    {
        private static HSBModel sModel = new HSBModel();
        private static Hue sHue = new Hue();
        private static Brightness sBrightness = new Brightness();
        private static Saturation sSaturation = new Saturation();

        public event EventHandler<EventArgs<NormalComponent>> ColorComponentChanged;
        public static Type ClassType
        {
            get { return typeof(HsbDisplay); }
        }
        #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 = (HsbDisplay)d;
            rd.OnColorChanged(c);
        }

        private void OnColorChanged(Color c)
        {
            txtH.Text = sHue.Value(c).ToString();
            txtS.Text = sSaturation.Value(c).ToString();
            txtB.Text = sBrightness.Value(c).ToString();

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

        #endregion

        #region NormalComponent

        public static DependencyProperty NormalComponentProperty = DependencyProperty.Register("NormalComponent", typeof(NormalComponent), ClassType,
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnColorComponentChanged));

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

        private static void OnColorComponentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                var cp = (NormalComponent)e.NewValue;
               
                var rd = (HsbDisplay)d;
                rd.OnColorComponentChanged(cp);
            }
            catch
            {
            }
        }

        private void OnColorComponentChanged(NormalComponent colorPlaneColorComponent)
        {

            if (colorPlaneColorComponent.Name == "HSB_Hue")
            {
                rH.IsChecked = true;
            }
            else if (colorPlaneColorComponent.Name == "HSB_Saturation")
            {
                rS.IsChecked = true;
            }
            else if (colorPlaneColorComponent.Name == "HSB_Brightness")
            {
                rB.IsChecked = true;
            }
            else
            {
                rH.IsChecked = false;
                rS.IsChecked = false;
                rB.IsChecked = false;
            }

            if (ColorComponentChanged != null)
            {
                ColorComponentChanged(this, new EventArgs<NormalComponent>(colorPlaneColorComponent));
            }



        }

        #endregion

        public event EventHandler<EventArgs<Color>> ColorChanged;


        public HsbDisplay()
        {
            InitializeComponent();
        }


        private void txtR_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled = NumbersOnly(e.Text);
            base.OnPreviewTextInput(e);

        }

        private bool NumbersOnly(string text)
        {
            String okChars = "0123456789";
            return text.ToCharArray().All(c => okChars.IndexOf(c) == -1);
        }


        private void TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {

                Color = sModel.Color(Convert.ToDouble(txtH.Text), Convert.ToDouble(txtS.Text)/100,
                                     Convert.ToDouble(txtB.Text)/100);
            }
            catch
            {
            }
        }

        private void rH_Checked(object sender, RoutedEventArgs e)
        {
            NormalComponent = sHue;
        }

        private void rS_Checked(object sender, RoutedEventArgs e)
        {
            NormalComponent = sSaturation;
        }

        private void rB_Checked(object sender, RoutedEventArgs e)
        {
            NormalComponent = sBrightness;
        }
    }


}

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.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160927.1 | Last Updated 26 Dec 2010
Article Copyright 2010 by KenJohnson
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid