Solved

Microsoft Visual C++ Color Dialog Box Problem

Posted on 2009-06-30
10
506 Views
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->colorDialog1->ShowDialog();
 

				 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

0
Comment
Question by:datopdogg7
  • 6
  • 4
10 Comments
 
LVL 39

Expert Comment

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

Why not simply get them as floats?

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



 
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
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);
0
 

Author Comment

by:datopdogg7
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

	{

	public:

		Form1(void)

		{

			InitializeComponent();

			//

			//TODO: Add the constructor code here

			//

		}
 

	protected:

		/// <summary>

		/// Clean up any resources being used.

		/// </summary>

		~Form1()

		{

			if (components)

			{

				delete components;

			}

		}

	private: System::Windows::Forms::ColorDialog^  colorDialog1;

	private: System::Windows::Forms::TabControl^  ColorTab;

	protected: 
 

	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;
 
 
 

	private:

		/// <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());

			this->ColorTab->SuspendLayout();

			this->tabPage1->SuspendLayout();

			this->SuspendLayout();

			// 

			// colorDialog1

			// 

			this->colorDialog1->FullOpen = true;

			// 

			// ColorTab

			// 

			this->ColorTab->Controls->Add(this->tabPage1);

			this->ColorTab->Controls->Add(this->tabPage2);

			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->Controls->Add(this->textBox6);

			this->tabPage1->Controls->Add(this->label6);

			this->tabPage1->Controls->Add(this->label5);

			this->tabPage1->Controls->Add(this->label4);

			this->tabPage1->Controls->Add(this->label3);

			this->tabPage1->Controls->Add(this->label2);

			this->tabPage1->Controls->Add(this->label1);

			this->tabPage1->Controls->Add(this->textBox5);

			this->tabPage1->Controls->Add(this->textBox4);

			this->tabPage1->Controls->Add(this->textBox3);

			this->tabPage1->Controls->Add(this->textBox2);

			this->tabPage1->Controls->Add(this->textBox1);

			this->tabPage1->Controls->Add(this->ColorButton);

			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->Controls->Add(this->ColorTab);

			this->Name = L"Form1";

			this->Text = L"Digital Image";

			this->ColorTab->ResumeLayout(false);

			this->tabPage1->ResumeLayout(false);

			this->tabPage1->PerformLayout();

			this->ResumeLayout(false);
 

		}

#pragma endregion

	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
 

				 this->colorDialog1->ShowDialog();
 

				 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

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
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?
0
 

Author Comment

by:datopdogg7
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.

Thanks,
Dennis
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 39

Expert Comment

by:itsmeandnobodyelse
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.
0
 

Author Comment

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

Expert Comment

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

Author Comment

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

https://filedb.experts-exchange.com/incoming/ee-stuff/7679-DigitalImage.rar
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 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.




0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now