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

WPF Color Picker Construction Kit

, 26 Dec 2010 CPOL
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.Diagnostics;
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.ColorModels.Lab
{
    /// <summary>
    /// Interaction logic for LabDisplay.xaml
    /// </summary>
    public partial class LabDisplay : UserControl
    {
        private bool mProcessEvents = true;

        private static LabModel sModel = new LabModel();
        private static Lightness sLightness = new Lightness();
        private static A sA = new A();
        private static B sB = new B();
        public static Type ClassType
        {
            get { return typeof(LabDisplay); }
        }
        public LabDisplay()
        {
            InitializeComponent();
            l = color => sModel.LComponent(color).ToString(LFormat);
            a = color => sModel.AComponent(color).ToString(AFormat);
            b = color => sModel.BComponent(color).ToString(BFormat);
            LFormat = "N0";
            AFormat = "N0";
            BFormat = "N0";
        }

        private Func<Color, string> l;
        private Func<Color, string> a;
        private Func<Color, string> b;
        public string LFormat { get; set; }
        public string AFormat { get; set; }
        public string BFormat { get; set; }

        #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 color = (Color)e.NewValue;
            var display = (LabDisplay)d;
            display.OnColorChanged(color);
        }

        private void OnColorChanged(Color color)
        {
            mProcessEvents = false;
            txtL.Text = l(color);
            txtA.Text = a(color);
            txtB.Text = b(color);
            mProcessEvents = true;

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

        #endregion
        public event EventHandler<EventArgs<Color>> ColorChanged;


        #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 = (LabDisplay)d;
                rd.OnColorComponentChanged(cp);
            }
            catch
            {
            }
        }

        #endregion

        private void OnColorComponentChanged(NormalComponent colorPlaneColorComponent)
        {

            if (colorPlaneColorComponent.Name == "LAB_Lightness")
            {
                rL.IsChecked = true;
            }
            else if (colorPlaneColorComponent.Name == "LAB_A")
            {
                rA.IsChecked = true;
            }
            else if (colorPlaneColorComponent.Name == "LAB_B")
            {
                rB.IsChecked = true;
            }
            else
            {
                rL.IsChecked = false;
                rA.IsChecked = false;
                rB.IsChecked = false;
            }

         


        }



        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)
        {
            if (mProcessEvents)
            {
                try
                {
                    Color = sModel.Color(Convert.ToDouble(txtL.Text), Convert.ToDouble(txtA.Text),
                                         Convert.ToDouble(txtB.Text));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }
        }

        private void rL_Checked(object sender, RoutedEventArgs e)
        {
            NormalComponent = sLightness;
        }

        private void rA_Checked(object sender, RoutedEventArgs e)
        {
            NormalComponent = sA;
        }

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

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 | Terms of Use | Mobile
Web03 | 2.8.150129.1 | Last Updated 26 Dec 2010
Article Copyright 2010 by KenJohnson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid