我想绘制不同颜色的图标(只有一种颜色)。为此,我想导入单个 alpha 纹理,然后将其与应用程序中的给定颜色组合。
结果应该是,当 alpha 贴图的不透明度为 0 并且应该绘制使用的颜色时,当不透明度为 1 时,背景上没有绘制任何内容。
一个解决方案应该隐藏在 QPainter
的某处,因为您可以手动设置 Composition-Mode ( QPainter::setCompositionMode
)。但我没有让它像我想要的那样工作。
有人有想法吗?
提前致谢。
编辑:这是一个解释我想做的小图形。我想使用如图所示的 Alpha-Map,然后使用颜色层来创建我的图标。重要的是,背景保持透明。
最佳答案
你可以像这样使用 QPainter 来做这些:
QColor color;
// set color value
// load gray-scale image (an alpha map)
QPixmap pixmap = QPixmap(":/images/earth.png");
// initialize painter to draw on a pixmap and set composition mode
QPainter painter(&pixmap);
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
painter.setBrush(color);
painter.setPen(color);
painter.drawRect(pixmap.rect());
// Here is our new colored icon!
QIcon icon = QIcon(pixmap);
这是我使用上面的代码(QPixmap.save() 方法)获得的灰度图像和两个彩色图标:
icons
关于Qt 使用颜色和 alpha-map 绘制图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24943711/