Microsoft Visual C++ Color Dialog Box Problem

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

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The GetHue, GetBrightness, GetSaturation were returning a float.

Why not simply get them as floats?

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

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);
datopdogg7Author Commented:
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

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

>>>> 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?
datopdogg7Author Commented:
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.

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 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.
datopdogg7Author Commented:
Ok I updated the whole project. Thanks
If you send me the link I would download it.
datopdogg7Author Commented:
Took me a while to figure it out, I guess the comment field needed to be filled in.
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.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.