From cdf531131eff490ee0885f6aa96a916a9f466e96 Mon Sep 17 00:00:00 2001
From: Mylloon <kennel.anri@tutanota.com>
Date: Mon, 9 May 2022 10:54:54 +0200
Subject: [PATCH] Determine the average color of the image

---
 src/quadtree.cpp | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/src/quadtree.cpp b/src/quadtree.cpp
index edb53d2..be6bf8e 100644
--- a/src/quadtree.cpp
+++ b/src/quadtree.cpp
@@ -58,9 +58,31 @@ SDL_Surface * QuadTree::image(short niveau_p) {
     return nouvelle_image;
 }
 
-SDL_Color QuadTree::calculeCouleur(SDL_Surface *) {
-    // Temporaire
-    return SDL_Color();
+SDL_Color QuadTree::calculeCouleur(SDL_Surface * s) {
+    int r = 0, g = 0, b = 0;
+    if(SDL_LockSurface(s) == 0) {
+        for(int x = 0; x < s->w; ++x) {
+            for(int y = 0; y < s->h; ++y) {
+                Uint8 r_temp, g_temp, b_temp;
+                SDL_GetRGB(*reinterpret_cast<Uint32 *>(
+                    static_cast<Uint8 *>(s->pixels) + y * s->pitch + x * s->format->BytesPerPixel
+                ), s->format, &r_temp, &g_temp, &b_temp);
+                r += r_temp;
+                g += g_temp;
+                b += b_temp;
+            }
+        }
+
+        SDL_UnlockSurface(s);
+    }
+
+    SDL_Color color;
+    int diviseur = s->w * s->h;
+    color.r = floor(r / diviseur);
+    color.g = floor(g / diviseur);
+    color.b = floor(b / diviseur);
+
+    return color;
 }
 
 bool QuadTree::verificationEgalitee(SDL_Surface *) {