### WEBINAR:On-Demand

Virtual Developer Workshop: Containerized Development with Docker

Ever wanted one of those fancy color picking dialogs that apps like Corel PhotoHouse have ? If you did, here it is !!

First, some general stuff about how I got this stuff together.

The most important part, obviously , is the color space conversion from RGB to HSB. I got some help ( lots actually) on this from one of the discussion boards on Codeguru ( Thanks !)

Once you have this piece of code, you have the next problem, which is to create proper bitmaps which are required for displaying the color spaces.

Creating the Hue bitmap is quite easy as you just have to go through each of the hue values (0-360) and iterate through each of the saturation values (0-255). I used a standard brightness value of 255 for the bitmap as other values give pretty dull results.

Next , and this is the difficult part - comes the part where you create the RGB bitmap. This is a little tough as you have to not only create gradients but also fit them together seamlessly . I accomplished this by first creating rectangular gradients and then using some texture mapping code ( courtesy Micheal Abrash - Zen ) to fit them together .

Now the next part is to give the whole thing a user interface. Again the HSB part is easy as finding the angle of a point as well as its distance from another are pretty straightforward. And to those math gurus who are fuming at the code - please forgive me - I took the simplest approach I knew of - x^2 + y^2 = a^2 .

Then we come to the RGB space. Again, this requires a lot of 3d math - I again took the easiest route - As I knew which bitmap I was using as well as the coordinates of the vertices, I used some 2d math to map points from the 3d space to 2d and vice versa. It`s not terribly elegant , but it works , and thats what I get paid for.

If you look at the contructor of the CColorPickerDlg, you`ll notice that I set up certain values for the vertices and centre. This I got from the bitmap I`m using. You could quite easily use a different bitmap if you don`t like the one I provide ( can`t imagine why though !!!) , by just setting different values here . The code which actually does the math should adjust accordingly.

Unfortunately - this bit of code was written by burning a lot of midnight oil in a real big hurry !! So I didn`t comment too much. I`ll try to post an update in the coming weeks with a few upgrades as well as a lot of comments.

Now - for the important stuff - how do you use it ? Simple - Copy the Dialog resource - IDD_DIALOG_COLORS from the resource file of the demo project into your project.

Next - Copy the two Bitmap resources IDB_BITMAP_RGB and IDB_BITMAP_HSB from the demo project into your own workspace.

Then, include ColorPickerDlg.h in the file you want to call it from ( the view in the demo ) and use the following bit of code :

``` CColorPickerDlg dlg(m_color) // where m_color is the
// color you want to inialize
// with.
if(dlg.DoModal() == IDOK)
{
// User pressed ok
m_color = dlg.GetColor();
}
```

That is it. And yes, please include the following in your code.

• ColorPickerDlg.cpp
• Dib.cpp
• Common.cpp
The latter two are support files which provide some DIB manipulation routines as well as some of the math stuff.

The demo app which I have provided uses gray = (192,192,192) as the default background. Choose View | Color Space to invoke the color picker . When you choose a different color, it is used as the background.

The CColorPickerDlg will also automatically handle WM_SYSCOLORCHANGE messages to always make sure that no funny images are displayed.

Thanks to Chris Mauder for a few suggestions inclding the very good one of including the two gradient bitmaps as resources.

• #### Well done my young apprentice. You should be knighted!

Posted by Legacy on 07/25/2002 07:00am

Originally posted by: Grand Templar of Reality and All Derived Illusionary Works

I'll probably use this at some point but, for now, it shall be archived.

BTW - I'll talk to His Highness about that knighting. ;)

• #### The formulas are correct.

Posted by Legacy on 06/18/2002 07:00am

Originally posted by: Gerry Sarver

```Please refer to:

http://www.netnam.vn/unescocourse/computervision/12.htm

for an excellent overview of color theory (with only a couple of small copy and paste mistakes in the math ;-)

While shocklet is correct that white is (255,255,255) under RGB, under HSI white is (0,0,255) as represented by Rajiv's program.  Under HSI, the first 0 means "0 degrees", where the entire range of Hues is divided into 360 degrees.  The second 0, Saturation, indicates that there is no saturation (color) at all in white light. And actually, for pure white, which is just gray scale at full intensity, Hue is undefined.

BTW, great work Rajiv!

```

• #### I am looking for a CMYK color picker

Posted by Legacy on 05/13/2002 07:00am

Originally posted by: Mike Shupe

Your display looks nice. You should create a CMYK version -- piece of cake to create one base on your RGB picker. Also, maybe more useful since it will provide additional functionality over Windows pickers instead of just looking nicer. Let me know if you put one together.

• #### Your formula is not right.

Posted by Legacy on 02/21/2002 08:00am

Originally posted by: shocklet

If you input H(0) S(255) B(255)

It is not white( 255, 255, 255 ).

• #### Very Nice.

Posted by Legacy on 08/20/2001 07:00am

Originally posted by: Kevin Pinkerton

Thanks. I am impressed.

• #### Very Good!

Posted by Legacy on 03/13/2000 08:00am

Originally posted by: Alex J. Cuadros Vargas

```This is very good...
I use VC++ 6 and don't have problems...

Alex

```

• #### You da man!

Posted by Legacy on 03/07/2000 08:00am

Originally posted by: Drake

Thanks!

• #### Using VC6, getting an error I don't understand

Posted by Legacy on 03/06/2000 08:00am

Originally posted by: Drake

Converted the project using VC6. There are no bitmap resources. I opened the executable and pulled them out of there. Followed the instructions.

Getting an assertion error line 214 of dib.cpp, m_void != NULL.

Any thoughts??

• #### Very Cool !!!

Posted by Legacy on 03/05/2000 08:00am

Originally posted by: Charles

Thanks for the hard work.

• #### Small problem

Posted by Legacy on 02/27/2000 08:00am

Originally posted by: Sherry Ramos

```This is absolutely wonderful! Thanks for all of the hard work!!!! I did find one small memory leak when loading the bitmaps using the CCHSB.BMP and CCRGB.BMP files. I fixed it by adding one line of code to "dib.cpp":

{
DestroyDIB(); // TO CORRECT MEMORY LEAK

...

```

• You must have javascript enabled in order to post comments.