Click here to Skip to main content
15,888,984 members
Articles / Desktop Programming / MFC

Cool Colour Selector Including Hue, Saturation, and Lumination

Rate me:
Please Sign up or sign in to vote.
3.67/5 (14 votes)
9 Nov 2006MIT1 min read 86.7K   2.3K   29   21
This colour selector helps to make selecting colours more efficient

Image 1

Introduction

When I created this colour selector I was creating a 3D graphics program; I was using the standard windows common colour selection dialog box however it became a bit tedious selecting the "Define Custom Colours >>" button to select custom colours. So I designed this colour selector which I find much easier to use.

Using the code

All colour boxes can be dragged and dropped onto the custom colour boxes. Custom colour values are automatically restored each time the dialog box is displayed. The colours can all be dragged onto the "New Colour:" box to select that colour.

Clicking on the "Current Colour:" colour box will select the original colour again. However it is not possible (without modifications) to drag a colour on-top of the "Current Colour:" colour box as that would defeat the object of having it.

It is possible to change the colour reference, hue value, saturation value, lumination value, red value, green value, and the blue value.

I hope that this is of some use to you.

// declare a variable to store our color
COLORREF rgbColor = RGB(255, 0, 0);
 
// create an instance of the color selector
CSelColorDlg dlgSelColor(GetRValue(rgbColor), GetGValue(rgbColor),
                         GetBValue(rgbColor));
// display the color changing dialog box
if(dlgSelColor.DoModal() == IDOK)
{
      // select the new color
      rgbColor = dlgSelColor.GetColor();
      // change the background color
      Invalidate();
}

Points of Interest

Most of the visual appearance of the colour selector has relied on the use of GetBitmapBits() and SetBitmapBits() to save on processing time. When the colour images are generated the image is stored within a CBitmap object; then later to be drawn as usual.

History

  • Fixed major bug regarding desktop colour depths.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer Rotorz Limited
United Kingdom United Kingdom
I have been fascinated by software and video games since a young age when I was given my first computer, a Dragon 32. Since then I have experimented with numerous methods of development ranging from point-and-click type packages to C++. I soon realized that software development was what I wanted to do.

Having invested a lot of time into programming with various languages and technologies I now find it quite easy to pickup new ideas and methodologies. I relish learning new ideas and concepts.

Throughout my life I have dabbled in game and engine development. I was awarded a first for the degree "BEng Games and Entertainment Systems Software Engineering" at the University of Greenwich. It was good to finally experience video games from a more professional perspective.

Due to various family difficulties I was unable to immediately pursue any sort of software development career. This didn't stop me from dabbling though!

Since then I formed a company to focus upon client projects. Up until now the company has primarily dealt with website design and development. I have since decided that it would be fun to go back to my roots and develop games and tools that other developers can use for their games.

We have recently released our first game on iPhone/iPad called "Munchy Bunny!" (see: http://itunes.apple.com/us/app/munchy-bunny!/id516575993?mt=8). We hope to expand the game and release to additional platforms.

Also, check out our tile system extension for Unity! (see: http://rotorz.com/tilesystem/)

Comments and Discussions

 
QuestionHow can I implement this application using slider instead of spectrum. Pin
rajesh_kapure22-Nov-07 1:40
rajesh_kapure22-Nov-07 1:40 
QuestionSuggestion: Alpha values? Pin
Jerry Evans10-Nov-06 3:04
Jerry Evans10-Nov-06 3:04 
GeneralGreat stuff - but some bugs. Pin
_gl8-Nov-06 14:39
_gl8-Nov-06 14:39 
GeneralRe: Great stuff - but some bugs. Pin
Lea Hayes9-Nov-06 9:18
Lea Hayes9-Nov-06 9:18 
GeneralRe: Great stuff - but some bugs. Pin
_gl9-Nov-06 10:53
_gl9-Nov-06 10:53 
GeneralRe: Great stuff - but some bugs. Pin
_gl10-Nov-06 3:52
_gl10-Nov-06 3:52 
GeneralRe: Great stuff - but some bugs. Pin
Rick York13-Nov-06 12:17
mveRick York13-Nov-06 12:17 
GeneralI like it Pin
StrayJay9-May-06 3:10
StrayJay9-May-06 3:10 
QuestionHow to include in project Pin
craig.miller2-Sep-05 10:48
craig.miller2-Sep-05 10:48 
AnswerRe: How to include in project Pin
StrayJay9-May-06 3:02
StrayJay9-May-06 3:02 
Since the original poster didn't respond to your message, I'll take a shot at it. I just imported the code in a VC6 project so I'll give directions for that compiler.

Download and unzip the source code using the link at top of this page.

Open your project. From the menu select Project >> Add to Project... >> Files, and select the files SelColorDlg.cpp and SelColorDlg.h that you just unzipped.

Open the file ColorSel.rc and copy the following 'dialog'-code from that file --unless you're using a newer version than I have, you can also copy it from below.
IDD_COLSEL DIALOG DISCARDABLE  0, 0, 277, 183
STYLE DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Select Colour"
FONT 8, "MS Shell Dlg"
BEGIN
    DEFPUSHBUTTON   "&OK",IDOK,168,166,50,14
    PUSHBUTTON      "&Cancel",IDCANCEL,224,166,50,14
    LTEXT           "Current Colour:",IDC_STATIC,166,7,48,8
    LTEXT           "New Colour:",IDC_STATIC,224,7,40,8
    LTEXT           "Hu&e:",IDC_STATIC,171,72,16,8
    EDITTEXT        IDC_HUE,189,70,24,12,ES_AUTOHSCROLL
    LTEXT           "&Sat:",IDC_STATIC,171,90,16,8
    EDITTEXT        IDC_SAT,189,88,24,12,ES_AUTOHSCROLL
    LTEXT           "&Lum:",IDC_STATIC,171,107,16,8
    EDITTEXT        IDC_LUM,189,106,24,12,ES_AUTOHSCROLL
    LTEXT           "&Red:",IDC_STATIC,223,72,16,8
    EDITTEXT        IDC_RED,241,70,24,12,ES_AUTOHSCROLL
    LTEXT           "&Green:",IDC_STATIC,216,90,23,8
    EDITTEXT        IDC_GREEN,241,88,24,12,ES_AUTOHSCROLL
    LTEXT           "&Blue:",IDC_STATIC,222,107,17,8
    EDITTEXT        IDC_BLUE,241,106,24,12,ES_AUTOHSCROLL
    LTEXT           "Ref:",IDC_STATIC,167,46,15,8
    EDITTEXT        IDC_REF,184,44,86,12,ES_AUTOHSCROLL
    PUSHBUTTON      "<< &Add Custom Colour",ID_ADDCOLOUR,174,136,89,14
END

Next, open the .rc-file from your own project. Look up the section that begins with the following remark:
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

Paste the 'dialog'-code right below this remark. Next, open the file Resource.h from the zip-file and copy the following code from it --again, you can also copy the code below.
#define ID_ADDCOLOUR                    3
#define IDD_COLSEL                      129
#define IDC_DRAGITEM                    130
#define IDC_HUE                         1001
#define IDC_SAT                         1002
#define IDC_LUM                         1003
#define IDC_RED                         1004
#define IDC_GREEN                       1005
#define IDC_BLUE                        1006
#define IDC_REF                         1007

Now open the resource.h file from your own project and copy the above #defines into it. Change the values from these defines so that they are unique; in other words, no value should be the same as any of the values that you already had in your own resource.h file.

And finally, at the bottom of your resource.h file you may find the following lines:
// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_3D_CONTROLS                     1
#define _APS_NEXT_RESOURCE_VALUE        131
#define _APS_NEXT_COMMAND_VALUE         32773
#define _APS_NEXT_CONTROL_VALUE         1008
#define _APS_NEXT_SYMED_VALUE           101
#endif
#endif

Important: Change the values to reflect the changes you've made in the previous step, so that your IDE knows which constant values it can use for any new resources, commands or controls.

HTH,

StrayJay.
GeneralRe: How to include in project Pin
Lea Hayes9-May-06 3:21
Lea Hayes9-May-06 3:21 
GeneralRe: How to include in project Pin
StrayJay9-May-06 3:40
StrayJay9-May-06 3:40 
GeneralWindowsCE Compatability Pin
mutpan21-Nov-04 7:18
mutpan21-Nov-04 7:18 
Generalvb code Pin
david28078331-Oct-04 19:24
david28078331-Oct-04 19:24 
GeneralRe: vb code Pin
Lea Hayes31-Oct-04 20:32
Lea Hayes31-Oct-04 20:32 
GeneralExcellent Job Pin
Ken Mazaika12-Jul-04 17:01
Ken Mazaika12-Jul-04 17:01 
GeneralVery bad example Pin
ardell.yandex27-Jun-04 12:44
ardell.yandex27-Jun-04 12:44 
GeneralRe: Very bad example Pin
Lea Hayes27-Jun-04 20:23
Lea Hayes27-Jun-04 20:23 
GeneralRe: Very bad example Pin
ardell.yandex27-Jun-04 22:39
ardell.yandex27-Jun-04 22:39 
GeneralRe: Very bad example Pin
Lea Hayes28-Jun-04 6:54
Lea Hayes28-Jun-04 6:54 
Generalnice Pin
Livid21-May-04 14:16
Livid21-May-04 14:16 

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

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