user1873073
user1873073

Reputation: 3670

How do you convert rgb to uint index_or_rgb

I am using

QImage::setPixel(x, y, uint index_or_rgb); // http://qt-project.org/doc/qt-4.8/qimage.html#setPixel-2

But I don't know how to convert rgb values into a uint. How do you convert rgb values into uint?

QColor *c = new QColor(185, 20, 120);
image.setPixel(i, i2, c->value()); // this doesnt work

Upvotes: 2

Views: 6980

Answers (2)

mtyong
mtyong

Reputation: 130

This way maybe works, just try it out.

setPixel(x, y, qRgb(185, 20, 120));

Upvotes: 1

Pavel Strakhov
Pavel Strakhov

Reputation: 40512

1. See the documentation of QImage::setPixel:

If the image's format is either monochrome or 8-bit, the given index_or_rgb value must be an index in the image's color table, otherwise the parameter must be a QRgb value.

I assume your image is neither monochrome nor 8-bit, so you need to obtain QRgb value (which is a typedef of unsigned int).

2. Next, if you look at QColor documentation, you can notice that QRgb value from a QColor should be obtained using rgb() (or rgba() if you use alpha channel in your image).

3. Note that QColor should not be created using new. It's usually created on stack and passed by value. So your code should be corrected as the following:

QColor c(185, 20, 120);
image.setPixel(i, i2, c.rgba());

Also note that value() doesn't return a representation of entire QColor. It just returns one of 3 components of HSV representation of QColor. It seems that value() is completely unrelated to your use case.

Upvotes: 4

Related Questions