blendColor that doesn't need additional setup code

Andreas Pakulat apaku at gmx.de
Mon May 28 22:39:21 BST 2007


Hi,

as I said to Aaron already here's a patch that adds

blendColor(QColor,QColor, qreal, CompositionMode)

(simplified API). This needed a rename of the existing blendColor to
overlayColor (which fits its implementation much better). I'm not 100%
if the api dox of the overlayColor is proper, but it tells you what the
function does.

blendColor helps to reduce usages from:
QBrush fg = ret->foreground();
QColor bg = QApplication::palette().window().color();
if( background.alphaF() < 1 )
    fg.setColor(KGraphicsUtils::blendColor(QApplication::palette().highlight().color(), bg));
else
{
    background.setAlpha(128);
    foreground.setColor(KGraphicsUtils::blendColor(QApplication::palette().highlight().color(), bg));
}
ret->setForeground(fg);
//ret == QStandardItem pointer

to this:
QBrush fg = ret->foreground();
fg.setColor( KGraphicsUtils::blendColor(
   QApplication::palette().highlight().color(),
   QApplication::palette().window().color()
) );
ret->setForeground(fg);

Comments? Rants? Improvements? (I saw Ingo's mail about improving
overlayColor to include the alpha value of both colors but as I'm not a
graphics guy and didn't really understand that, I left it out).

Andreas

-- 
Let me put it this way: today is going to be a learning experience.
-------------- next part --------------
Index: kdeui/colors/kgraphicsutils.h
===================================================================
--- kdeui/colors/kgraphicsutils.h	(Revision 669186)
+++ kdeui/colors/kgraphicsutils.h	(Arbeitskopie)
@@ -28,16 +28,28 @@ class QColor;
  */
 namespace KGraphicsUtils {
     /**
-     * Blend 2 colors into a new color with the strength of the blend based on the alpha channel of the second color.
+     * Overlay 2 colors into a new color. If the alpha value of the second color is lower than 1.0 then this function does a blend from the first to the second color.
      * @code
         QColor white(Qt::White);
         white.setAlpha(130);
         QColor lighter = KGraphicsUtils::blendColor(myColor, white);
        @endcode
      * @param one the starting point
-     * @param two the end point where we blend towards.
+     * @param two the end point which is layed over the starting point.
      * @param comp the CompositionMode used to do the blending.
      */
-    KDEUI_EXPORT QColor blendColor(const QColor &one, const QColor &two, QPainter::CompositionMode comp = QPainter::CompositionMode_SourceOver);
+    KDEUI_EXPORT QColor overlayColor(const QColor &one, const QColor &two, QPainter::CompositionMode comp = QPainter::CompositionMode_SourceOver);
+
 
+    /**
+     * Blend 2 colors into a new color with the strength of the blend given as parameter using a default of 0.5.
+     * @code
+        QColor lighter = KGraphicsUtils::blendColor(myColor, Qt::White);
+       @endcode
+     * @param one the starting point
+     * @param two the end point where we blend towards.
+     * @param amount the strength of the blend to be used, is only considered if the end point has no alpha value set
+     * @param comp the CompositionMode used to do the blending.
+     */
+    KDEUI_EXPORT QColor blendColor( const QColor& one, const QColor& two, double amount = 0.5, QPainter::CompositionMode comp = QPainter::CompositionMode_SourceOver);
 }
Index: kdeui/colors/kgraphicsutils.cpp
===================================================================
--- kdeui/colors/kgraphicsutils.cpp	(Revision 669186)
+++ kdeui/colors/kgraphicsutils.cpp	(Arbeitskopie)
@@ -22,7 +22,7 @@
 #include <QColor>
 #include <QImage>
 
-QColor KGraphicsUtils::blendColor(const QColor &one, const QColor &two, QPainter::CompositionMode comp) {
+QColor KGraphicsUtils::overlayColor(const QColor &one, const QColor &two, QPainter::CompositionMode comp) {
     // This may not be super fast; but timing shows this is easilly fast enough.
     QImage img(1, 1, QImage::Format_ARGB32_Premultiplied);
     QPainter p(&img);
@@ -35,3 +35,15 @@ QColor KGraphicsUtils::blendColor(const 
     return img.pixel(0, 0);
 }
 
+QColor KGraphicsUtils::blendColor( const QColor& one, const QColor& two, qreal amount, QPainter::CompositionMode comp)
+{
+    if( two.alphaF() < 1 )
+    {
+        return KGraphicsUtils::overlayColor(one, two, comp);
+    }else
+    {
+        QColor second = two;
+        second.setAlphaF(amount);
+        return KGraphicsUtils::overlayColor( one, second, comp );
+    }
+}


More information about the kde-core-devel mailing list