Click here to Skip to main content
15,896,726 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.2K   7.7K   76  
A WPF color picker (like Adobe's) constructed in a modular fashion for easy modification.
using System;
using System.Windows.Media;

namespace ColorPicker.ExtensionMethods
{
    public static class ColorExtensionMethods
    {
        public static double Intensity(this Color color)
        {
            return (double)(color.R  + color.G  + color.B  ) /( 3 * 255);
        }

        public static double Brightness(this Color color)
        {
            return (double)Math.Max(Math.Max(color.R, color.G), color.B) / 255;
        }

        public static double SaturationHSB(this Color color)
        {
            var max = (double)Math.Max(Math.Max(color.R, color.G), color.B) / 255;
            if (max == 0) return 0;
            var min = (double)Math.Min(Math.Min(color.R, color.G), color.B) / 255;
            return (max - min)/max;
        }

        public static double Lightness(this Color color)
        {
             var max = (double)Math.Max(Math.Max(color.R, color.G), color.B) / 255;
             var min = (double)Math.Min(Math.Min(color.R, color.G), color.B) / 255;
             return (max + min)/2;
        }

        public static double Chroma(this Color color)
        {
            var max = (double)Math.Max(Math.Max(color.R, color.G), color.B) / 255;
            var min = (double)Math.Min(Math.Min(color.R, color.G), color.B) / 255;
            return max - min;
        }

        public static double SaturationHSL(this Color color)
        {
            var max = (double)Math.Max(Math.Max(color.R, color.G), color.B) / 255;
            var min = (double)Math.Min(Math.Min(color.R, color.G), color.B) / 255;
            var chroma = max - min;

            var lightness = (max + min)/2;
            if (lightness <= .5)
            {
                return chroma/(2*lightness);
            }
            return chroma / (2 - 2 * lightness);
        }


        public static Color WithAlpha(this Color color, byte alpha)
        {
            return Color.FromArgb(alpha, color.R, color.G, color.B);
        }

        public static Color WithR(this Color color, byte r)
        {
            return Color.FromArgb(color.A , r, color.G, color.B);
        }

        public static Color WithG(this Color color, byte g)
        {
            return Color.FromArgb(color.A, color.R,g, color.B);
        }
        public static Color WithB(this Color color, byte b)
        {
            return Color.FromArgb(color.A, color.R, color.G, b);
        }
       
    }
}

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