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