How to mix two colors represented by QColor type ?



  • Hi, i want to mix two colors represented by QColor type in my C++ code. How to do it 'simple way' ? Like

    @QColor a, b, c;

    c=a+b@


  • Moderators

    Hi,

    You can't do it by adding. There's no overloaded "+" operator for QColor.
    Instead you can try this,
    @
    QColor a(Qt::red);
    QColor b(Qt::green);

    QColor c;
    c.setRgb(a.red(),b.green(),0);
    @

    So c becomes yellow.



  • Actually when you use color in qt. It is already mixture of 3 colors based on rgb or any other system.

    If you are using rgb system then there will be 3 colors mixture.

    Example taken by p3c0 is showed that there are 3 colors one is Qt::red, second is Qt::green and third one and last is 0 means black.

    So it is mixture of red,green and black.



  • This is how I usually blend two QColors togheter

    QColor(
    color1.red()* (1-r) + color2.red()r,
    color1.green()
    (1-r) + color2.green()r,
    color1.blue()
    (1-r) + color2.blue()*r,
    255));

    where r is the ratio of the color for example 0.5 will give you a 50-50 blend


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.