Click here to Skip to main content
15,895,192 members
Articles / Desktop Programming / WPF

WPF Color Picker Construction Kit

Rate me:
Please Sign up or sign in to vote.
4.97/5 (59 votes)
26 Dec 2010CPOL9 min read 117.1K   7.7K   76  
A WPF color picker (like Adobe's) constructed in a modular fashion for easy modification.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Media;
using ColorPicker.ExtensionMethods;

namespace ColorPicker.ColorModels.HSB
{
 public  class HSBModel
    {
    public enum EHSBComponent
     {
         Hue=0,
         Saturation= 1,
         Brightness =2
     }

        Color Color(double[] components)
        {
            return Color(components[0], components[1], components[2]);
        }

        public Color Color(double hue, double saturation, double brightness)
        {
            double r = 0;
            double g = 0;
            double b = 0;

            if (saturation == 0)
            {
                r = g = b =brightness;
            }
            else
            {
                // the color wheel consists of 6 sectors. Figure out which sector you're in.
                double sectorPos = hue / 60.0;
                int sectorNumber = (int)(Math.Floor(sectorPos));
                // get the fractional part of the sector
                double fractionalSector = sectorPos - sectorNumber;

                // calculate values for the three axes of the color. 
                double p = brightness * (1.0 - saturation);
                double q = brightness * (1.0 - (saturation * fractionalSector));
                double t = brightness * (1.0 - (saturation * (1 - fractionalSector)));

                // assign the fractional colors to r, g, and b based on the sector the angle is in.
                switch (sectorNumber)
                {
                    case 0:
                        r = brightness;
                        g = t;
                        b = p;
                        break;
                    case 1:
                        r = q;
                        g = brightness;
                        b = p;
                        break;
                    case 2:
                        r = p;
                        g = brightness;
                        b = t;
                        break;
                    case 3:
                        r = p;
                        g = q;
                        b = brightness;
                        break;
                    case 4:
                        r = t;
                        g = p;
                        b = brightness;
                        break;
                    case 5:
                        r = brightness;
                        g = p;
                        b = q;
                        break;
                }
            }
       
         
            return System.Windows.Media.Color.FromRgb(
                Convert.ToByte(( r * 255.0)),
                Convert.ToByte(( g * 255.0)),
                Convert.ToByte(( b * 255.0))
                );


        }

        public double HComponent(Color color)
        {
            System.Drawing.Color c = System.Drawing.Color.FromArgb(255,color.R,color.G,color.B );
            return  c.GetHue();
        }

        public Double SComponent(Color color)
        {
            return color.SaturationHSB();
        }

        public Double BComponent(Color color)
        {
            return color.Brightness();
        }

     public Double Component(Color color ,int pos)
     {
         if (pos == 0)
         {
             return HComponent(color);
         }
         else if (pos == 1)
         {
             return SComponent(color);
         }
         else if (pos == 2)
         {
             return BComponent(color);
         }
         else
         {
             throw new Exception("The HSB model has only 3 components");
         }
     }


     public Double Component(Color color, EHSBComponent component)
     {
         return Component(color,(int) component );
     }


    }
}

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)


Written By
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.

Comments and Discussions