Skip to content

Commit

Permalink
Merge pull request #394 from friction2d/safe-color-picker
Browse files Browse the repository at this point in the history
Update Color Picker
  • Loading branch information
rodlie authored Dec 25, 2024
2 parents 168c7ce + 9fc0cff commit 5ffb5ca
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/core/canvasmouseinteractions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,13 +518,21 @@ void Canvas::drawPathFinish(const qreal invScale) {

const QColor Canvas::pickPixelColor(const QPoint &pos)
{
// try the "safe" option first
if (QApplication::activeWindow()) {
const auto nPos = QApplication::activeWindow()->mapFromGlobal(pos);
return QApplication::activeWindow()->grab(QRect(QPoint(nPos.x(), nPos.y()),
QSize(1, 1))).toImage().pixel(0, 0);
}

// "insecure" fallback (will not work in a sandbox or wayland)
// will prompt for permissions on macOS
// Windows and X11 don't care
QScreen *screen = QApplication::screenAt(pos);
if (!screen) { return QColor(); }
WId wid = QApplication::desktop()->winId();
QImage img = screen->grabWindow(wid,
pos.x(), pos.y(),
1, 1).toImage();
return QColor(img.pixel(0, 0));
const auto pix = screen->grabWindow(wid, pos.x(), pos.y(), 1, 1);
return QColor(pix.toImage().pixel(0, 0));
}

void Canvas::applyPixelColor(const QColor &color,
Expand Down

0 comments on commit 5ffb5ca

Please sign in to comment.