PDA

View Full Version : Need advice on getting HSI color from an Image



ramstormrage
4th May 2008, 08:39
Hello experts.

I have an image viewer which has a dialog that has a frame whose color can be modified by the Color Dialog. However, I would also like to change the frame's color depending on the color chosen from the image on the Main Window (like the dropper tool from Windows Paint program).
Could someone kindly give me a general solution for this task?

I would like to know also where should I reimplement the mousePressEvent - in the mainwindow or the dialog?

Thank you very much for your help. It would be greatly appreciated.

marcel
4th May 2008, 08:45
Subclass the widget on which you paint the image(label, generic widget, etc) and override the mousePressEvent or the mouseReleaseEvent function.

Also you should still keep a reference to the pixmap that was set as background. When you get a mouse click just look in the pixamp at the click coordinates and get the RGB value.
You'lll have to convert that to HSI.

ramstormrage
4th May 2008, 09:36
.. coordinates and get the RGB value.
You'lll have to convert that to HSI.

This, I have taken care of already.


Subclass the widget on which you paint the image(label, generic widget, etc) and override the mousePressEvent or the mouseReleaseEvent function.

But this, I do not quite get. Do you mean I should reimplement the QLabel that holds the Pixmap and change the mouse events? Sorry im not quite familiar at mouse events.

marcel
4th May 2008, 09:43
Not reimplement entirely. Just subclass and override QWidget::mousePressEvent. Here you should use the pixmap() property of QLabel to get the QRgb at the position where the mouse was clicked inside the label.

Since you mentioned now that it is a QLabel, then you will need and additional signal to notice the outside world when the color should change, and to which color. The signal should take a Qrgb parameter and you should connect it to the object that takes care of changing the color of the frame.

Hope it's clear now.

ramstormrage
4th May 2008, 09:47
Thank you. Ill post again when I get stuck or confused again.

marcel
4th May 2008, 09:50
You're welcome...
Just one thing: the signal I mentioned earlier should be emitted from the label's mousePressEvent.

ramstormrage
4th May 2008, 11:30
Hello again.

I subclassed QLabel and got to the point where I assign the position of the mouse event to a QPoint. Now, I understand that I need to use this QPoint to get the QRgb value of whatever is underneath. Question, how do I do this?



Here you should use the pixmap() property of QLabel to get the QRgb at the position where the mouse was clicked inside the label.

marcel
4th May 2008, 11:37
Oh, yes... Forgot all about that. QPixmap doesn't expose pixel data. You could convert it to a QImage, although this adds a bit of extra overhead:



QImage img = pixmap()->toImage();

.... later, in mousePressEvent
QRgb color = img.pixel(mouseX, mouseY);
You could create a member QImage so you don't have to convert the pixmap at every mouse click.

See QImage::pixel (http://doc.trolltech.com/latest/qimage.html#pixel)
QPixmap::toImage (http://doc.trolltech.com/latest/qpixmap.html#toImage) and QLabel::pixmap() (http://doc.trolltech.com/latest/qlabel.html#pixmap)

ramstormrage
4th May 2008, 12:08
Ok Ill give it a go thanks.

Marcel, Id like to put in a few more questions if you dont mind. If my custom frame is on a dialog, whats the easiest way to return to Main Window to pick a color then return again to the dialog to display the color? I understand that the dialog should be modeless..

marcel
4th May 2008, 12:24
Yes, the dialog should be modeless, otherwise you couldn't interact with the main window. You could set the flags of the dialog to be Qt::Tool. This is usually done for palette windows, like in Photshop and Visual Studio.

Once you select a color in the main window you could transmit it to the dialog via a signal. The signal should be emitted by the main window itself or by an object the main window contains.

In order to add a custom slot to the dialog you will have to subclass it.

ramstormrage
4th May 2008, 12:53
Alright! Im learning new stuff..
Ok thanks again, youve been a great help.

marcel
4th May 2008, 13:26
no problem