Click here to Skip to main content
Click here to Skip to main content

A Fast, Compact C# Color Selection Control

, 21 Feb 2005
Rate this:
Please Sign up or sign in to vote.
A color selection control that quickly lets the users zero in on the color they want.

Color Selection Demonstration

Introduction

Have you ever wanted to pick a color, and thought to yourself, "I just want a light orangish-red that isn't glaringly bright"? Then you got a selection control like the Windows Color Select Dialog where you had to play with the controls for five minutes (or more) to finally arrive at the color you want? This control was designed with this problem in mind. It allows the user to rapidly zero in on the color they want to use, and it uses a relatively small screen footprint to do so. The control is resizable so you may be able to squeeze it even smaller than it is in the demo. It uses the .NET double-buffered control support for flicker-free update and instant feedback to the user.

To use the control, you first pick your color by clicking on the Hue bar. It's nice and linear so you can quickly get the right shade (it uses less screen space than a big circle that you commonly see). You can click the large left or right arrows to tweak the hue one step. Holding the button down engages a timer which acts a lot like keyboard repeating to move slowly through the hues. Speaking of keyboard, you can also use the right and left arrows on the keyboard to select the hue.

Once you have the hue, moving the sliders for Brightness (which is more formally called 'Luminance') and Saturation allows you to rapidly adjust the lightness and purity of the color. Want grayscale? Just turn saturation all the way to zero, and adjust brightness to get your gray shade.

If you want to type in red, green and blue values directly, left clicking the region showing the selected color will bring up the Windows Color Select Dialog. It will come up fully expanded so you can go directly to the red, green and blue edit boxes. Selecting OK will adjust the Color Select Control to the color you pick in the dialog.

Background

I've looked at a number of other color selection controls available. All of them either were too heavy because they popped up a complex dialog, or too difficult to locate a color with rapidity. When designing this control, I adapted the Hue/Luminance/Saturation (HLS) to Red/Green/Blue (RGB) conversion code from a C++ project named CColor by Christian Rodemeyer that is here on CodeProject. Thanks Christian!

Using the code

Include a #using CustomUIControls; statement at the top of your form code. Then add a reference to CustomUIControls.dll by right clicking on your project and selecting "Add Reference...", then browse to where the DLL is located. Then add the control to your form. Finally, if you wish to receive notice when the user changes the color in the control, derive your control from the IColorSelectCallback interface. An example of how to use the Color Select Control is provided in the source through the TextStylePicker control, which allows you to select a foreground color, background color and the font for text. Also thrown into the mix is a button that has a graphical arrow on it, which you may reuse as well. Here's a synopsis example of using the Color Select Control on a form:

public class MyCustomForm : System.Windows.Forms.Form, IColorSelectCallback
{
  // ... member variables ...
  private ColorSelectControl cscColorSelect;

  // ... form designer ...
  private void InitializeComponent()
  {
     // ... add this here once, then you can
     // mess with the properties in Form Designer ...
     this.cscColorSelect = new CustomUIControls.ColorSelectControl();
  }

  // ... form load, you can initialize properties if you wish ...
  private void OnFormLoad(object sender, System.EventArgs e)
  {

    // set the starting color
    cscColorSelect.SelectedColor = m_ForeColor;

    // put ourselves in as callback
    cscColorSelect.CallbackHost = this;
  }

  //////////////////////////////////////////////////////
  // Interface Implementation
  //////////////////////////////////////////////////////
  #region IColorSelectCallback Members

  /// <summary>
  /// One of the custom color select controls has changed its color
  /// </summary>
  /// <param name="ControlID">The unique ID of the control that changed</param>
  /// <param name="clrNewColor">The new color</param>
  public void ColorChanged(int ControlID, Color clrNewColor)
  {
      // ... do stuff here because the color has changed ...
  } // ColorChanged()

  #endregion

} // class MyCustomForm

History

  • Version 1.0
    • Initial release.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Kevin Menningen
Web Developer
United States United States
I've been developing software since about 1984, when I starting messing around with my friend's Timex Sinclair. Currently I'm the lead software engineer for a C++ project that reads Electroencephalograms (EEG), or brain waves.

Comments and Discussions

 
GeneralThemes support PinmemberLarry Allen10-Mar-07 9:32 
GeneralNeeds resize before it will work Pinmemberpathayward3-Oct-05 23:20 
GeneralRe: Needs resize before it will work PinsussAnonymous4-Oct-05 0:51 
GeneralWhy the callback PinmemberWillemM28-Sep-05 19:59 
GeneralRe: Why the callback PinmemberKevin Menningen29-Sep-05 0:24 
GeneralVery nice! PinprotectorMarc Clifton22-Feb-05 1:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140814.1 | Last Updated 22 Feb 2005
Article Copyright 2005 by Kevin Menningen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid