Mixing colors in PyQt?
Is there a way to make a new QColor in PyQt by combining 2 different colors? Something like:
QColo开发者_StackOverflow社区r([30, QColor(123,0,45, 100)], [70, QColor(12, 34, 56, 100)])
where the 30 and 70 represent the percentage of the new color to take.
What I need is the equivalent color of layering a semi-transparent color on top of a opaque one.
It's been a while since you asked, but ofcourse there is a way to 'blend' these colors together; it may not be trivial, but it is certainly possible.
Combining two colors can be quite easily done by doing (assuming color1 and color2 are both QColors):
QColor(.3* color1.red() + .7 * color2.red(),
.3* color1.green() + .7 * color2.green(),
.3* color1.blue() + .7 * color2.blue(),
.3* color1.alpha() + .7 * color2.alpha()
)
which interpolates between the two colors.
However, this does not take into account that one color is on top of the other. Since the first color is opaque, the resulting color is opaque too. However, the fraction of the alpha value needs to be taken into account to compute the resulting color, which looks something like (depending on the interpretation of your question):
QColor(color1.red() + .color2.alpha() * color2.red(),
color1.green() + .color2.alpha() * color2.green(),
color1.blue() + .color2.alpha() * color2.blue(),
1
)
精彩评论