PDA

View Full Version : Grab the color of a pixel of the screen?



hakermania
18th April 2011, 11:11
hell all :)
What I'm trying to do is to take the color of a specific pixel of the screen, compare it with the (html) values (blue:) 0022dd and (red:) dd2200 and if something of the two, do some action.
I don't know where to start actually. QRgb looks too general for some examples I saw previously, and QRgb QImage->pixel(x,y) constantly outputs

QImage::pixel: coordinate (640,392) out of range
So, how can I compare the colors?
This is what I've tried so far:

#include <QtGui/QCursor>
#include <QApplication>
#include <QObject>
#include <QRgb>
#include <QImage>

void b(){
QImage *a = new QImage;
QRgb b = a->pixel(640,392);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
b();
return a.exec();
}

Thx for any answers :)

stampede
18th April 2011, 11:15
QImage *a = new QImage;
QRgb b = a->pixel(640,392);
You want to take a pixel value from an empty image, thats not gonna work.
There is a "screenshot" example in Qt: link (http://doc.qt.nokia.com/latest/desktop-screenshot.html), you can grab the desktop to QPixmap or QImage and then take the pixel value.

hakermania
18th April 2011, 11:31
Thx for your reply, in the example you gave me, it saves it as QPixmap by grabbing the desktop (originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());) but there's no such function (pixel() or setpixel() or something like this) in QPixmap which QRgb wants in QRgb b = a->pixel(640,392);...

stampede
18th April 2011, 11:33
You can convert QPixmap to QImage with QImage img = originalPixmap.toImage(); and then use img.pixel(w,h) method.

hakermania
18th April 2011, 11:39
Nice, thx again, you are too helpful.
Now, how can I use this to compare it with the html color values? When I qDebug() the QPixmap I get a float, like 4284242774, how can I compare this with the html value?

high_flyer
18th April 2011, 11:49
QColor::name()

hakermania
18th April 2011, 11:53
QColor::name()

Thx, this will return the html color value, but how will I convert the RGB's value to HTML or the opposite (compare the values from the 1st post to RGB ones)

stampede
18th April 2011, 11:57
Html colors you've posted earlier are integers (just like QRgb), in hexadecimal format. Try to convert those strings to unsigned integer with QString::toUInt (set base = 16) and compare the result with pixel values taken from desktop screenshot. You can also use the qRed(), qGreen() and qBlue() access methods (search for QRgb in Qt Assistant for more info).

hakermania
18th April 2011, 12:11
QString qblue = "0022dd";
bool ok;
int blue = qblue.toUInt(&ok,16);
qDebug() << blue;
This outputs: 8925 which is not an RGB value (http://www.drpeterjones.com/colorcalc/)

:/ I'm quite confused :P

high_flyer
18th April 2011, 12:52
0x0022dd is a number in base 16.
It is indeed not an RGB value.
Thus the QString conversion is correct - 8925 is the base 10 representation of the input hex value.
If you want the rgb representation you will have to break the value to its components:
0x00 -> Red
0x22 -> Green
0xdd -> Blue

hakermania
18th April 2011, 13:15
hmm, OK I figured it out, thx for all of your replies :) The code goes:
QPixmap *a = new QPixmap;
*a = QPixmap::grabWindow(QApplication::desktop()->winId());
QImage *img = new QImage;
*img = a->toImage();
QRgb b = img->pixel(10,33);
QColor *c = new QColor;
c->setRgb(b);
qDebug() << c->name();
This will output the html value of the specified pixel, as I wanted :)