Qt 如何将rgb转换为uint索引或rgb
我正在使用Qt 如何将rgb转换为uint索引或rgb,qt,Qt,我正在使用 QImage::setPixel(x, y, uint index_or_rgb); // http://qt-project.org/doc/qt-4.8/qimage.html#setPixel-2 但我不知道如何将rgb值转换为uint。如何将rgb值转换为uint QColor *c = new QColor(185, 20, 120); image.setPixel(i, i2, c->value()); // this doesnt work 1.请参阅QIma
QImage::setPixel(x, y, uint index_or_rgb); // http://qt-project.org/doc/qt-4.8/qimage.html#setPixel-2
但我不知道如何将rgb值转换为uint。如何将rgb值转换为uint
QColor *c = new QColor(185, 20, 120);
image.setPixel(i, i2, c->value()); // this doesnt work
1.
请参阅QImage::setPixel
的文档:
如果图像的格式为单色或8位,则给定的索引_或_rgb值必须是图像颜色表中的索引,否则该参数必须是QRgb值
我假设您的图像既不是单色的,也不是8位的,因此您需要获得QRgb
值(它是unsigned int
的typedef)
2.
接下来,如果您查看QColor
文档,您会注意到QColor
中的QRgb
值应该使用rgb()
(或者rgba()
如果您在图像中使用alpha通道)
3.
请注意,不应使用new
创建QColor
。它通常在堆栈上创建并按值传递。因此,您的代码应更正如下:
QColor c(185, 20, 120);
image.setPixel(i, i2, c.rgba());
还要注意,
value()
不会返回整个QColor
的表示形式。它只返回HSV表示的QColor
的三个组件之一。似乎value()
与您的用例完全无关。这种方法可能有效,请尝试一下
setPixel(x, y, qRgb(185, 20, 120));