Browse Source

add getAverageColor function to bild

Kolja Strohm 1 year ago
parent
commit
166aba2c35
2 changed files with 27 additions and 2 deletions
  1. 24 1
      Bild.cpp
  2. 3 1
      Bild.h

+ 24 - 1
Bild.cpp

@@ -2868,11 +2868,34 @@ const Punkt& Bild::getDrawOff() const
     return drawOff[doa];
 }
 
-bool Bild::hasAlpha3D()
+bool Bild::hasAlpha3D() const
 {
     return alpha3D;
 }
 
+int Bild::getAverageColor() const
+{
+    double a = 0;
+    double r = 0;
+    double g = 0;
+    double b = 0;
+    for (int i = 0; i < size.x * size.y; i++)
+    {
+        float currentA = (fc[i] >> 24) / 255.f;
+        a = a + currentA;
+        r = r + ((fc[i] >> 16) & 0xFF) * currentA;
+        g = g + ((fc[i] >> 8) & 0xFF) * currentA;
+        b = b + (fc[i] & 0xFF) * currentA;
+    }
+    a = a / (size.x * size.y);
+    r = (r / (size.x * size.y)) / a;
+    g = (g / (size.x * size.y)) / a;
+    b = (b / (size.x * size.y)) / a;
+    a = a * 255;
+    return ((((int)a) & 0xFF) << 24) | ((((int)r) & 0xFF) << 16)
+         | ((((int)g) & 0xFF) << 8) | (((int)b) & 0xFF);
+}
+
 // Inhalt der BildZ Klasse aus Bild.h
 // Konstruktor
 BildZ::BildZ()

+ 3 - 1
Bild.h

@@ -479,7 +479,9 @@ namespace Framework
         //! Das ist sinnvoll für die Verwendung im 3DBildschirm, wo das
         //! Gezeichnette Bild später mittels Alpha Blending angezeigt wird. Der
         //! Flag wird im 3DBildschirm automatisch gesetzt
-        DLLEXPORT bool hasAlpha3D();
+        DLLEXPORT bool hasAlpha3D() const;
+        //! Berechnet die durchschnittliche Farbe aller Pixel des Bildes
+        DLLEXPORT int getAverageColor() const;
     };
 
     //! Eine Zeichnung des 2d GUI Frameworks, die ein Bild anzeigt.