1

I am currently trying to retrieve an rgb value based on the average of the colors displayed on a screen, the problem is that at runtime the values ​​retrieved in colors are always null, despite the fact that I run with or without sudo.

I give you my code, if anyone has an idea where it could come from, I specify that I am on a mac M1, I do not know if this can affect the operation.

#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>

using namespace std;

float moyenne(vector<int> entiers) {
    int somme = 0;
    for (int i = 0; i < entiers.size(); i++) {
        somme += entiers[i];
    }
    return (float)somme / entiers.size();
}

int main (void)
{
    // Création d'une fenêtre SFML invisible
    sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "SFML Window", sf::Style::None);
    window.setVisible(false);

    // Récupération des dimensions de l'écran
    sf::Vector2u screenSize = window.getSize();

    // Définition du nombre de pixels à sauter entre chaque vérification de couleur
    int pixelStep = 100;
    vector<int> r;
    vector<int> g;
    vector<int> b;

    sf::Texture texture;
    texture.create(screenSize.x, screenSize.y);
    while (1) {
        // Parcours de chaque pixel de l'écran
        texture.update(window);

        sf::Color color;
        // Parcours de chaque pixel de l'écran
        for (int x = 0; x < screenSize.x; x += pixelStep) {
            for (int y = 0; y < screenSize.y; y += pixelStep) {

                // Mise à jour de la texture à partir du contenu de la fenêtre

                // Récupération de la couleur du pixel
                color = texture.copyToImage().getPixel(x, y);

                // Ajout de la couleur à la somme
                r.push_back(color.r);
                g.push_back(color.g);
                b.push_back(color.b);
            }
        }

        // Calcul de la couleur moyenne
        int redAvg = moyenne(r);
        int greenAvg = moyenne(g);
        int blueAvg = moyenne(b);

        cout << "Red: " << redAvg << " green: " << greenAvg << " blue: " << blueAvg << endl;

        string color_tmp = to_string(redAvg) + ";" + to_string(greenAvg) + ";" + to_string(blueAvg);


        // Affichage de la couleur moyenne
        cout << "\033[38;2;" << color_tmp << "mColor!\033[0m" << endl;
        r.clear();
        g.clear();
        b.clear();
        sf::sleep(sf::seconds(1.0f));
    }

    return 0;
}

I tried using sfml's capture function in the first place, but it gave me the same problem, and it was inadvisable to use.your text

Mathieu
  • 11
  • 1
  • 1
    Are you trying to get a screenshot of the current desktop because you make the window invisible? Right now I see the creation of a blank window, drawing nothing on the window, making a texture that contains the still-blank window, and then expecting the texture to not be blank. – user4581301 Mar 29 '23 at 20:31
  • @user4581301 i'm looking to take a screenshot, if i'm on my desktop or on chrome, i want my screenshot to take the whole screen, and i read that making it invisible would work, it would take whatever there is under my window – Mathieu Mar 29 '23 at 20:50
  • I'm far from a SFML guru, but I would have expected the the behaviour you currently observe. SFML has no reason to know anything about what is going on outside of the windows it creates. I believe you will need to use a different tool. – user4581301 Mar 29 '23 at 20:54
  • If you want to get pixels not from your app window, then you need to use something else (API for OS). Check this out: https://stackoverflow.com/questions/3291167/how-can-i-take-a-screenshot-in-a-windows-application. Then you can extract pixels into your sfml app and do whatever you want. – Dnafiqvss May 20 '23 at 13:30

0 Answers0