Colour of pixel under mouse position

HI there’s a way to get the rgb colour under the current mouse position on the screen? (as do color picker of photoshop for example)

Over any pixel on the screen? You are going to need to do something system specific.

Somewhere in your juce app:

            auto root = c->getTopLevelComponent();
            auto rootPos = root->getLocalPoint (c, pos);

            if (auto disp = juce::Desktop::getInstance().getDisplays().getDisplayForPoint (c->localPointToGlobal (pos)))
                scale = float (disp->scale);

            image = root->createComponentSnapshot ({rootPos.getX() - w / 2, rootPos.getY() - h / 2, w, h}, false, scale);
            image = image.rescaled (w * zoom, h * zoom, juce::Graphics::lowResamplingQuality);

Then grab the pixel from the image.

2 Likes

Thank you, I’d like to write this, but it doesn’t work (because my app doesn’t have a component visible on the screen it show me a little component on top of the screen when i press a key):

auto tempSnap = Component::createComponentSnapshot(Rectangle<int>(Desktop::getMousePosition().x, Desktop::getMousePosition().y, 1, 1), false);
auto colour = tempSnap.getPixelAt(0, 0);
g.setColour(Colour(colour));

…and do stuffs with this colour