Go Premium for a chance to win a PS4. Enter to Win


Microsoft Visual C++ Color Dialog Box Problem

Posted on 2009-06-30
Medium Priority
Last Modified: 2013-12-14
Hi Experts,

I am having trouble getting the correct values of the hue, saturation, and brightness when selecting a color.

Below is the snippet of code that I use to get the values.

Please let me know what I am doing wrong.

Thanks Dennis
				 this->textBox1->Text = safe_cast<IConvertible ^>(colorDialog1->Color.GetHue())->ToString(nullptr);
				 float x = colorDialog1->Color.GetHue();
				 this->textBox6->Text = safe_cast<IConvertible ^>(colorDialog1->Color.GetBrightness())->ToString(nullptr);
				 this->textBox2->Text = safe_cast<IConvertible ^>(colorDialog1->Color.GetSaturation())->ToString(nullptr);

Open in new window

Question by:datopdogg7
  • 6
  • 4
LVL 39

Expert Comment

ID: 24752170
The GetHue, GetBrightness, GetSaturation were returning a float.

Why not simply get them as floats?

   float hue = colorDialog1->Color.GetHue();

LVL 39

Expert Comment

ID: 24752283
If you need the values as String use the String::Format member. It is much more straight-forward than the IConvertible.

   float hue = colorDialog1->Color.GetHue();
   String^ shue = String::Format("{0}", hue);

Author Comment

ID: 24755866
The numbers that come out are still incorrect. Please try the code below, select a custom color and see if the hue, saturation, and lum values are the same as the color you selected before an answer is given. Thanks, Dennis
#pragma once
#include <iostream>
namespace DigitalImage {
	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace std;
	/// <summary>
	/// Summary for Form1
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
			//TODO: Add the constructor code here
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
			if (components)
				delete components;
	private: System::Windows::Forms::ColorDialog^  colorDialog1;
	private: System::Windows::Forms::TabControl^  ColorTab;
	private: System::Windows::Forms::TabPage^  tabPage1;
	private: System::Windows::Forms::TabPage^  tabPage2;
	private: System::Windows::Forms::Button^  ColorButton;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::TextBox^  textBox5;
	private: System::Windows::Forms::TextBox^  textBox4;
	private: System::Windows::Forms::TextBox^  textBox3;
	private: System::Windows::Forms::TextBox^  textBox2;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label5;
	private: System::Windows::Forms::Label^  label4;
	private: System::Windows::Forms::TextBox^  textBox6;
	private: System::Windows::Forms::Label^  label6;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
			this->colorDialog1 = (gcnew System::Windows::Forms::ColorDialog());
			this->ColorTab = (gcnew System::Windows::Forms::TabControl());
			this->tabPage1 = (gcnew System::Windows::Forms::TabPage());
			this->textBox6 = (gcnew System::Windows::Forms::TextBox());
			this->label6 = (gcnew System::Windows::Forms::Label());
			this->label5 = (gcnew System::Windows::Forms::Label());
			this->label4 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->textBox5 = (gcnew System::Windows::Forms::TextBox());
			this->textBox4 = (gcnew System::Windows::Forms::TextBox());
			this->textBox3 = (gcnew System::Windows::Forms::TextBox());
			this->textBox2 = (gcnew System::Windows::Forms::TextBox());
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->ColorButton = (gcnew System::Windows::Forms::Button());
			this->tabPage2 = (gcnew System::Windows::Forms::TabPage());
			// colorDialog1
			this->colorDialog1->FullOpen = true;
			// ColorTab
			this->ColorTab->Location = System::Drawing::Point(12, 12);
			this->ColorTab->Name = L"ColorTab";
			this->ColorTab->SelectedIndex = 0;
			this->ColorTab->Size = System::Drawing::Size(569, 336);
			this->ColorTab->TabIndex = 0;
			// tabPage1
			this->tabPage1->Location = System::Drawing::Point(4, 22);
			this->tabPage1->Name = L"tabPage1";
			this->tabPage1->Padding = System::Windows::Forms::Padding(3);
			this->tabPage1->Size = System::Drawing::Size(561, 310);
			this->tabPage1->TabIndex = 0;
			this->tabPage1->Text = L"Color Space";
			this->tabPage1->UseVisualStyleBackColor = true;
			// textBox6
			this->textBox6->Location = System::Drawing::Point(424, 181);
			this->textBox6->Name = L"textBox6";
			this->textBox6->Size = System::Drawing::Size(75, 20);
			this->textBox6->TabIndex = 12;
			// label6
			this->label6->AutoSize = true;
			this->label6->Location = System::Drawing::Point(354, 184);
			this->label6->Name = L"label6";
			this->label6->Size = System::Drawing::Size(62, 13);
			this->label6->TabIndex = 11;
			this->label6->Text = L"Luminoscity";
			// label5
			this->label5->AutoSize = true;
			this->label5->Location = System::Drawing::Point(114, 100);
			this->label5->Name = L"label5";
			this->label5->Size = System::Drawing::Size(36, 13);
			this->label5->TabIndex = 10;
			this->label5->Text = L"Green";
			this->label5->Click += gcnew System::EventHandler(this, &Form1::label5_Click);
			// label4
			this->label4->AutoSize = true;
			this->label4->Location = System::Drawing::Point(114, 139);
			this->label4->Name = L"label4";
			this->label4->Size = System::Drawing::Size(38, 13);
			this->label4->TabIndex = 9;
			this->label4->Text = L"Yellow";
			// label3
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(114, 184);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(27, 13);
			this->label3->TabIndex = 8;
			this->label3->Text = L"Red";
			// label2
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(354, 142);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(55, 13);
			this->label2->TabIndex = 7;
			this->label2->Text = L"Saturation";
			// label1
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(354, 100);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(27, 13);
			this->label1->TabIndex = 6;
			this->label1->Text = L"Hue";
			this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click_1);
			// textBox5
			this->textBox5->Location = System::Drawing::Point(174, 136);
			this->textBox5->Name = L"textBox5";
			this->textBox5->Size = System::Drawing::Size(75, 20);
			this->textBox5->TabIndex = 5;
			// textBox4
			this->textBox4->Location = System::Drawing::Point(174, 97);
			this->textBox4->Name = L"textBox4";
			this->textBox4->Size = System::Drawing::Size(75, 20);
			this->textBox4->TabIndex = 4;
			// textBox3
			this->textBox3->Location = System::Drawing::Point(174, 181);
			this->textBox3->Name = L"textBox3";
			this->textBox3->Size = System::Drawing::Size(75, 20);
			this->textBox3->TabIndex = 3;
			// textBox2
			this->textBox2->Location = System::Drawing::Point(424, 135);
			this->textBox2->Name = L"textBox2";
			this->textBox2->Size = System::Drawing::Size(75, 20);
			this->textBox2->TabIndex = 2;
			// textBox1
			this->textBox1->Location = System::Drawing::Point(424, 97);
			this->textBox1->Name = L"textBox1";
			this->textBox1->Size = System::Drawing::Size(75, 20);
			this->textBox1->TabIndex = 1;
			// ColorButton
			this->ColorButton->Location = System::Drawing::Point(424, 254);
			this->ColorButton->Name = L"ColorButton";
			this->ColorButton->Size = System::Drawing::Size(75, 23);
			this->ColorButton->TabIndex = 0;
			this->ColorButton->Text = L"Color Select";
			this->ColorButton->UseVisualStyleBackColor = true;
			this->ColorButton->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// tabPage2
			this->tabPage2->Location = System::Drawing::Point(4, 22);
			this->tabPage2->Name = L"tabPage2";
			this->tabPage2->Padding = System::Windows::Forms::Padding(3);
			this->tabPage2->Size = System::Drawing::Size(561, 310);
			this->tabPage2->TabIndex = 1;
			this->tabPage2->Text = L"Image Display";
			this->tabPage2->UseVisualStyleBackColor = true;
			// Form1
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(593, 360);
			this->Name = L"Form1";
			this->Text = L"Digital Image";
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 this->textBox1->Text = safe_cast<IConvertible ^>(colorDialog1->Color.GetHue())->ToString(nullptr);
				 float x = colorDialog1->Color.GetHue();
				 this->textBox6->Text = safe_cast<IConvertible ^>(colorDialog1->Color.GetBrightness())->ToString(nullptr);
				 this->textBox2->Text = safe_cast<IConvertible ^>(colorDialog1->Color.GetSaturation())->ToString(nullptr);
				 //this->textBox3->Text = safe_cast<IConvertible ^>(colorDialog1->Color.GetBrightness())->ToString(nullptr);
				 //this->textBox4->Text = safe_cast<IConvertible ^>(colorDialog1->Color.GetBrightness())->ToString(nullptr);
				// this->textBox5->Text = safe_cast<IConvertible ^>(colorDialog1->Color.GetBrightness())->ToString(nullptr);
				// colorDialog1->Color.Green
				 //ColorDialog::Color property.
private: System::Void label1_Click(System::Object^  sender, System::EventArgs^  e) {
private: System::Void label1_Click_1(System::Object^  sender, System::EventArgs^  e) {
private: System::Void label5_Click(System::Object^  sender, System::EventArgs^  e) {

Open in new window

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 39

Expert Comment

ID: 24762510
>>>> Please try the code below
Could you post .rc and resource.h file?

>>>> this->textBox1->Text = safe_cast<IConvertible ^>(colorDialog1->Color.GetHue())->ToString(nullptr);
You still were using the IConvertible ? Why didn't you give my suggestions a trial?

Author Comment

ID: 24764783
I did give it a try, it just yielded the same results.
The resource.h file is empty, i.e. no dependencies)

I opened the .rc file, however there is no code, just two icons. I could send you the file via email, as I can't attach it here.

Please advise on how to proceed.

LVL 39

Expert Comment

ID: 24764917
I took the code above and replaced a freshly created cpp file of a new CLR application. But I neither get a menu nor a button but only an empty screen when envoking it. How did you invoke the Color Dialog if there are no resources?

>>>> Please advise on how to proceed.
You could zip the whole project folder and attach it as file. If there are restrictions you might go to www.ee-stuff.com where you should get a possibility to upload. I never did it myself but some other questions have links to that.

Or, you attach .rc, .vcproj, form1.h and resource.h as .txt files.

Author Comment

ID: 24764987
Ok I updated the whole project. Thanks
LVL 39

Expert Comment

ID: 24765346
If you send me the link I would download it.

Author Comment

ID: 24766103
Took me a while to figure it out, I guess the comment field needed to be filled in.

LVL 39

Accepted Solution

itsmeandnobodyelse earned 2000 total points
ID: 24771021
Ok, I could verify the issue. You get the right values for hue, saturation and brightness but in a different unit:

   hue(x) = x*(2/3);
   sat(y)  = y*240;
   btn(z)  = z*240;

The x, y, z are the floats you get when calling GetHue(), GetSaturation(), GetBrightness().

when applying the above formulas and round it to the next integer you'll get the integers you were seeing in the color dialog box.

I don't know why they did it that way, but probably there were two reasons: First, by using integers less than 255 they could store the values into bytes rather than 16bit words as it would be with float type. Second, the user always should have a similar range from 0 to 240 (for hue it actually is from 0 to 239) and should not be bothered with the real units which are much differently.


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question