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
A WPF color picker (like Adobe's) constructed in a modular fashion for easy modification.
ColorPickerDemo.zip
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.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)

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