Click here to Skip to main content
12,294,775 members (65,900 online)
Click here to Skip to main content

Stats

62.1K views
5.3K 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.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace ColorPicker.ColorModels.RGB
{
 public   class Green : NormalComponent
    {
        public override int MinValue
        {
            get { return 0; }
        }

        public override int MaxValue
        {
            get { return 255; }
        }

        public override void UpdateNormalBitmap(WriteableBitmap bitmap, Color color)
        {
            unsafe
            {
                bitmap.Lock();
                int currentPixel = -1;
                byte* pStart = (byte*)(void*)bitmap.BackBuffer;
                for (int iRow = 0; iRow < bitmap.PixelHeight; iRow++)
                {
                    for (int iCol = 0; iCol < bitmap.PixelWidth; iCol++)
                    {
                        currentPixel++;
                        *(pStart + currentPixel * 3 + 0) = color.B; //Blue
                        *(pStart + currentPixel * 3 + 1) = (byte)(255 - iRow); //Green 
                        *(pStart + currentPixel * 3 + 2) = color.R; //red
                    }
                }

                bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight));
                bitmap.Unlock();
            }
        }

        public override void UpdateColorPlaneBitmap(WriteableBitmap bitmap, int normalComponentValue)
        {
            unsafe
            {
                bitmap.Lock();
                int currentPixel = -1;
                byte* pStart = (byte*)(void*)bitmap.BackBuffer;
                for (int iRow = 0; iRow < bitmap.PixelHeight; iRow++)
                {
                    for (int iCol = 0; iCol < bitmap.PixelWidth; iCol++)
                    {
                        currentPixel++;
                        *(pStart + currentPixel * 3 + 0) = (byte)(iCol); //Blue
                        *(pStart + currentPixel * 3 + 1) = (byte)normalComponentValue; //Green 
                        *(pStart + currentPixel * 3 + 2) = (byte)(255 - iRow); //red
                    }
                }
                bitmap.AddDirtyRect(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight));
                bitmap.Unlock();
            }
        }

        public override Color ColorAtPoint(Point selectionPoint, int colorComponentValue)
        {
            var blue = (byte)Math.Round(selectionPoint.X);
            var green = (byte)colorComponentValue;
            var red = (byte)Math.Round(255 - selectionPoint.Y);
            return Color.FromRgb(red, green, blue);
        }

        public override Point PointFromColor(Color color)
        {
            return new Point(color.B, 255 - color.R);
        }

        public override int Value(Color color)
        {
            return color.G;
        }

        public override string Name
        {
            get { return "RGB_Green"; }
        }

        public override bool IsNormalIndependantOfColor
        {
            get { return false; }
        }
    }
}

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

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