Skip to content

Commit

Permalink
UI: Allow eyedropper outside of Blender (X11)
Browse files Browse the repository at this point in the history
Implements the GHOST_GetPixelAtCursor for the X11 backend, allowing
the user to sample any color displayed on the screen.

Noted some limitations in the code-comments.

Ref !111493.
  • Loading branch information
vitorboschi authored and ideasman42 committed Aug 26, 2023
1 parent 1985049 commit e5a0d11
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
39 changes: 39 additions & 0 deletions intern/ghost/intern/GHOST_SystemX11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1526,6 +1526,45 @@ void GHOST_SystemX11::processEvent(XEvent *xe)
}
}

GHOST_TSuccess GHOST_SystemX11::getPixelAtCursor(float r_color[3]) const
{
/* NOTE: There are known issues/limitations at the moment:
*
* - Blender has no control of the cursor outside of its window, so it is
* not going to be the eyedropper icon.
* - GHOST does not report click events from outside of the window, so the
* user needs to press Enter instead.
*
* Ref #111303. */

XColor c;
int32_t x, y;

if (getCursorPosition(x, y) == GHOST_kFailure) {
return GHOST_kFailure;
}
XImage *image = XGetImage(m_display,
XRootWindow(m_display, XDefaultScreen(m_display)),
x,
y,
1,
1,
AllPlanes,
XYPixmap);
if (image == nullptr) {
return GHOST_kFailure;
}
c.pixel = XGetPixel(image, 0, 0);
XFree(image);
XQueryColor(m_display, XDefaultColormap(m_display, XDefaultScreen(m_display)), &c);

/* X11 returns colors in the [0, 65535] range, so we need to scale back to [0, 1]. */
r_color[0] = c.red / 65535.0f;
r_color[1] = c.green / 65535.0f;
r_color[2] = c.blue / 65535.0f;
return GHOST_kSuccess;
}

GHOST_TSuccess GHOST_SystemX11::getModifierKeys(GHOST_ModifierKeys &keys) const
{

Expand Down
7 changes: 7 additions & 0 deletions intern/ghost/intern/GHOST_SystemX11.hh
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ class GHOST_SystemX11 : public GHOST_System {

GHOST_TSuccess setCursorPosition(int32_t x, int32_t y) override;

/**
* Get the color of the pixel at the current mouse cursor location
* \param r_color: returned sRGB float colors
* \return Success value (true == successful and supported by platform)
*/
GHOST_TSuccess getPixelAtCursor(float r_color[3]) const override;

/**
* Returns the state of all modifier keys.
* \param keys: The state of all modifier keys (true == pressed).
Expand Down

0 comments on commit e5a0d11

Please sign in to comment.