QImage::fill(Qt::red) 总是黑色?
QImage::fill(Qt::red) always black?
我想弄清楚 QImage 是如何工作的。一开始我只想创建一个 400x400 像素的 QImage 并尝试将其填充为红色。那么 QImage 已填充但黑色......
我还想创建一个单色 QImage。一种颜色应该是透明的,另一种颜色应该是其他颜色(例如:红色)。我怎样才能做到这一点?我用 setcolor 试过了,但这似乎不起作用...
scene = new QGraphicsScene(this);
ui.graphicsView->setScene(scene);
QImage *image = new QImage(400, 400, QImage::Format_Indexed8); //QImage::Format_Mono);
image->fill(Qt::red);
//image->setColor(1, Qt::transparent);
//image->setColor(0, Qt::red);
scene->addPixmap(QPixmap::fromImage(*image));
原因是您 QImage::Format
传递给了构造函数。使用例如QImage::Format_RGB32
获取接受颜色的图像。
要使用您的图像格式,您需要使用 setColor
方法,如 here 8 位情况所示。
QImage image(3, 3, QImage::Format_Indexed8);
QRgb value;
value = qRgb(122, 163, 39); // 0xff7aa327
image.setColor(0, value);
value = qRgb(237, 187, 51); // 0xffedba31
image.setColor(1, value);
value = qRgb(189, 149, 39); // 0xffbd9527
image.setColor(2, value);
image.setPixel(0, 1, 0);
image.setPixel(1, 0, 0);
image.setPixel(1, 1, 2);
image.setPixel(2, 1, 1);
结果
简答:
在 QImage
构造函数中使用 Format_RGB32
而不是 Format_Indexed8
。
详细答案:
Format_Indexed8
使用手动定义的颜色 table,其中每个索引代表一种颜色。您必须为图像创建自己的颜色 table:
QVector<QRgb> color_table;
for (int i = 0; i < 256; ++i) {
color_table.push_back(qRgb(i, i, i)); // Fill the color table with B&W shades
}
image->setColorTable(color_table);
您也可以手动设置当前颜色的每个索引table:
image->setColorCount(4); // How many colors will be used for this image
image->setColor(0, qRgb(255, 0, 0)); // Set index #0 to red
image->setColor(1, qRgb(0, 0, 255)); // Set index #1 to blue
image->setColor(2, qRgb(0, 0, 0)); // Set index #2 to black
image->setColor(3, qRgb(255, 255, 0)); // Set index #3 to yellow
image->fill(1); // Fill the image with color at index #1 (blue)
如您所见,Format_Indexed8
像素值代表 不是 RGB 颜色,而是索引值(它又代表您在颜色 [=40 中设置的颜色) =]).
Format_Mono
是另一种格式,它也使用颜色 table(请注意,它只允许使用两种颜色)。
补充回答:
One color should be transparent and the other any other (for example: red).
如果我理解正确的话,这段代码会做你想做的事:
// Create a 256x256 bicolor image which will use the indexed color table:
QImage *image = new QImage(256, 256, QImage::Format_Mono);
// Manually set our colors for the color table:
image->setColorCount(2);
image->setColor(0, qRgba(255, 0, 0, 255)); // Index #0 = Red
image->setColor(1, qRgba(0, 0, 0, 0)); // Index #1 = Transparent
// Testing - Fill the image with pixels:
for (short x = 0; x < 256; ++x) {
for (short y = 0; y < 256; ++y) {
if (y < 128) {
// Fill the part of the image with red color (#0)
image->setPixel(x, y, 0);
}
else {
// Fill the part of the image with transparent color (#1)
image->setPixel(x, y, 1);
}
}
}
我想弄清楚 QImage 是如何工作的。一开始我只想创建一个 400x400 像素的 QImage 并尝试将其填充为红色。那么 QImage 已填充但黑色...... 我还想创建一个单色 QImage。一种颜色应该是透明的,另一种颜色应该是其他颜色(例如:红色)。我怎样才能做到这一点?我用 setcolor 试过了,但这似乎不起作用...
scene = new QGraphicsScene(this);
ui.graphicsView->setScene(scene);
QImage *image = new QImage(400, 400, QImage::Format_Indexed8); //QImage::Format_Mono);
image->fill(Qt::red);
//image->setColor(1, Qt::transparent);
//image->setColor(0, Qt::red);
scene->addPixmap(QPixmap::fromImage(*image));
原因是您 QImage::Format
传递给了构造函数。使用例如QImage::Format_RGB32
获取接受颜色的图像。
要使用您的图像格式,您需要使用 setColor
方法,如 here 8 位情况所示。
QImage image(3, 3, QImage::Format_Indexed8);
QRgb value;
value = qRgb(122, 163, 39); // 0xff7aa327
image.setColor(0, value);
value = qRgb(237, 187, 51); // 0xffedba31
image.setColor(1, value);
value = qRgb(189, 149, 39); // 0xffbd9527
image.setColor(2, value);
image.setPixel(0, 1, 0);
image.setPixel(1, 0, 0);
image.setPixel(1, 1, 2);
image.setPixel(2, 1, 1);
结果
简答:
在 QImage
构造函数中使用 Format_RGB32
而不是 Format_Indexed8
。
详细答案:
Format_Indexed8
使用手动定义的颜色 table,其中每个索引代表一种颜色。您必须为图像创建自己的颜色 table:
QVector<QRgb> color_table;
for (int i = 0; i < 256; ++i) {
color_table.push_back(qRgb(i, i, i)); // Fill the color table with B&W shades
}
image->setColorTable(color_table);
您也可以手动设置当前颜色的每个索引table:
image->setColorCount(4); // How many colors will be used for this image
image->setColor(0, qRgb(255, 0, 0)); // Set index #0 to red
image->setColor(1, qRgb(0, 0, 255)); // Set index #1 to blue
image->setColor(2, qRgb(0, 0, 0)); // Set index #2 to black
image->setColor(3, qRgb(255, 255, 0)); // Set index #3 to yellow
image->fill(1); // Fill the image with color at index #1 (blue)
如您所见,Format_Indexed8
像素值代表 不是 RGB 颜色,而是索引值(它又代表您在颜色 [=40 中设置的颜色) =]).
Format_Mono
是另一种格式,它也使用颜色 table(请注意,它只允许使用两种颜色)。
补充回答:
One color should be transparent and the other any other (for example: red).
如果我理解正确的话,这段代码会做你想做的事:
// Create a 256x256 bicolor image which will use the indexed color table:
QImage *image = new QImage(256, 256, QImage::Format_Mono);
// Manually set our colors for the color table:
image->setColorCount(2);
image->setColor(0, qRgba(255, 0, 0, 255)); // Index #0 = Red
image->setColor(1, qRgba(0, 0, 0, 0)); // Index #1 = Transparent
// Testing - Fill the image with pixels:
for (short x = 0; x < 256; ++x) {
for (short y = 0; y < 256; ++y) {
if (y < 128) {
// Fill the part of the image with red color (#0)
image->setPixel(x, y, 0);
}
else {
// Fill the part of the image with transparent color (#1)
image->setPixel(x, y, 1);
}
}
}