From bd04376bac4c66d01c19d7ec12726cec81b968c5 Mon Sep 17 00:00:00 2001 From: ccleavinger <96743974+ccleavinger@users.noreply.github.com> Date: Tue, 22 Aug 2023 10:11:15 -0500 Subject: [PATCH 1/5] Added getPixelUnderCursor for MacOS_X --- intern/ghost/intern/GHOST_SystemCocoa.hh | 9 +++++- intern/ghost/intern/GHOST_SystemCocoa.mm | 38 ++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/intern/ghost/intern/GHOST_SystemCocoa.hh b/intern/ghost/intern/GHOST_SystemCocoa.hh index 37340af489b..fde683e28c4 100644 --- a/intern/ghost/intern/GHOST_SystemCocoa.hh +++ b/intern/ghost/intern/GHOST_SystemCocoa.hh @@ -10,7 +10,7 @@ #pragma once #ifndef __APPLE__ -# error Apple OSX only! + # error Apple OSX only! #endif // __APPLE__ //#define __CARBONSOUND__ @@ -116,6 +116,13 @@ class GHOST_SystemCocoa : public GHOST_System { */ GHOST_IWindow *getWindowUnderCursor(int32_t x, int32_t y); + /** + * 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; + /*************************************************************************************** * Event management functionality ***************************************************************************************/ diff --git a/intern/ghost/intern/GHOST_SystemCocoa.mm b/intern/ghost/intern/GHOST_SystemCocoa.mm index b2fa4de47b9..de1b7cbf627 100644 --- a/intern/ghost/intern/GHOST_SystemCocoa.mm +++ b/intern/ghost/intern/GHOST_SystemCocoa.mm @@ -821,6 +821,42 @@ GHOST_IWindow *GHOST_SystemCocoa::getWindowUnderCursor(int32_t x, int32_t y) return m_windowManager->getWindowAssociatedWithOSWindow((void *)nswindow); } +GHOST_TSuccess GHOST_SystemCocoa::getPixelAtCursor(float r_color[3]) const +{ + @autoreleasepool { + CGPoint cursorPosition = [NSEvent mouseLocation]; + + // Create rect to get color at location + CGRect rect = CGRectMake(cursorPosition.x, cursorPosition.y, 1, 1); + + // Create Image from rect + CGImageRef image = CGDisplayCreateImageForRect(kCGDirectMainDisplay, rect); + + // Create bitmap from image & free it + NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image]; + if (bitmap == nil) { + return GHOST_kFailure; + } + CGImageRelease(image); + + // Get color from bitmap + NSColor *color = [bitmap colorAtX:0 y:0]; + + if (color == nil) { + return GHOST_kFailure; + } + + CGFloat red, green, blue, alpha; + [color getRed:&red green:&green blue:&blue alpha:&alpha]; + + r_color[0] = (float)red; + r_color[1] = (float)green; + r_color[2] = (float)blue; + + return GHOST_kSuccess; + } +} + /** * \note returns coordinates in Cocoa screen coordinates. */ @@ -921,8 +957,6 @@ GHOST_TCapabilityFlag GHOST_SystemCocoa::getCapabilities() const ~( /* Cocoa has no support for a primary selection clipboard. */ GHOST_kCapabilityPrimaryClipboard | - /* Cocoa has no support for sampling colors from the desktop. */ - GHOST_kCapabilityDesktopSample | /* This Cocoa back-end has not yet implemented image copy/paste. */ GHOST_kCapabilityClipboardImages)); } -- 2.30.2 From e676fe3ae93495ca85fd0b45524b535f09081479 Mon Sep 17 00:00:00 2001 From: ccleavinger <96743974+ccleavinger@users.noreply.github.com> Date: Tue, 22 Aug 2023 15:45:38 -0500 Subject: [PATCH 2/5] Fixed formatting back to original --- intern/ghost/intern/GHOST_SystemCocoa.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/intern/ghost/intern/GHOST_SystemCocoa.hh b/intern/ghost/intern/GHOST_SystemCocoa.hh index fde683e28c4..5d553036943 100644 --- a/intern/ghost/intern/GHOST_SystemCocoa.hh +++ b/intern/ghost/intern/GHOST_SystemCocoa.hh @@ -10,7 +10,7 @@ #pragma once #ifndef __APPLE__ - # error Apple OSX only! +# error Apple OSX only! #endif // __APPLE__ //#define __CARBONSOUND__ -- 2.30.2 From d6e88ebd472b49e2e2c9e161fa31036bd90728f8 Mon Sep 17 00:00:00 2001 From: ccleavinger <96743974+ccleavinger@users.noreply.github.com> Date: Wed, 23 Aug 2023 12:05:37 -0500 Subject: [PATCH 3/5] Changed method to allow multi monitor screenshot --- intern/ghost/intern/GHOST_SystemCocoa.mm | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/intern/ghost/intern/GHOST_SystemCocoa.mm b/intern/ghost/intern/GHOST_SystemCocoa.mm index de1b7cbf627..5aff323bd2f 100644 --- a/intern/ghost/intern/GHOST_SystemCocoa.mm +++ b/intern/ghost/intern/GHOST_SystemCocoa.mm @@ -826,11 +826,18 @@ GHOST_TSuccess GHOST_SystemCocoa::getPixelAtCursor(float r_color[3]) const @autoreleasepool { CGPoint cursorPosition = [NSEvent mouseLocation]; + uint32_t count = 0; + CGDirectDisplayID displayForPoint; + if (CGGetDisplaysWithPoint(cursorPosition, 1, &displayForPoint, &count) != kCGErrorSuccess) + { + return GHOST_kFailure; + } + // Create rect to get color at location - CGRect rect = CGRectMake(cursorPosition.x, cursorPosition.y, 1, 1); - + CGRect rect = CGRectMake(cursorPosition.x, cursorPosition.y - 2, 1, 1); + // Create Image from rect - CGImageRef image = CGDisplayCreateImageForRect(kCGDirectMainDisplay, rect); + CGImageRef image = CGDisplayCreateImageForRect(displayForPoint, rect); // Create bitmap from image & free it NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image]; -- 2.30.2 From 2daadafe261726dcff2b3854a0aab3dd65c7ab80 Mon Sep 17 00:00:00 2001 From: ccleavinger <96743974+ccleavinger@users.noreply.github.com> Date: Thu, 24 Aug 2023 09:29:47 -0500 Subject: [PATCH 4/5] Added y axis transformation & removed multimonitor --- intern/ghost/intern/GHOST_System.hh | 2 ++ intern/ghost/intern/GHOST_SystemWayland.cc | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/intern/ghost/intern/GHOST_System.hh b/intern/ghost/intern/GHOST_System.hh index bb4688f865f..12dd715ff49 100644 --- a/intern/ghost/intern/GHOST_System.hh +++ b/intern/ghost/intern/GHOST_System.hh @@ -62,6 +62,8 @@ class GHOST_System : public GHOST_ISystem { */ virtual uint64_t getMilliSeconds() const; + + /** * Installs a timer. * diff --git a/intern/ghost/intern/GHOST_SystemWayland.cc b/intern/ghost/intern/GHOST_SystemWayland.cc index c4cb40b5bb1..f5c400c6f46 100644 --- a/intern/ghost/intern/GHOST_SystemWayland.cc +++ b/intern/ghost/intern/GHOST_SystemWayland.cc @@ -1,3 +1,4 @@ +#include "GHOST_SystemWayland.hh" /* SPDX-FileCopyrightText: 2020-2023 Blender Authors * * SPDX-License-Identifier: GPL-2.0-or-later */ @@ -6286,6 +6287,12 @@ GHOST_TSuccess GHOST_SystemWayland::setCursorPosition(const int32_t x, const int return GHOST_kFailure; } +GHOST_TSuccess GHOST_SystemWayland::getPixelAtCursor(float r_color[3]) const +{ + wl_display *wlDisplay = display_->wl.display; + +} + void GHOST_SystemWayland::getMainDisplayDimensions(uint32_t &width, uint32_t &height) const { #ifdef USE_EVENT_BACKGROUND_THREAD -- 2.30.2 From 78b3fe45b7da219d7d4649c5493581c2e20ddd2d Mon Sep 17 00:00:00 2001 From: ccleavinger <96743974+ccleavinger@users.noreply.github.com> Date: Thu, 24 Aug 2023 09:42:10 -0500 Subject: [PATCH 5/5] Forgot to save file. The code should be updated --- intern/ghost/intern/GHOST_SystemCocoa.mm | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/intern/ghost/intern/GHOST_SystemCocoa.mm b/intern/ghost/intern/GHOST_SystemCocoa.mm index 5aff323bd2f..38afe10b89b 100644 --- a/intern/ghost/intern/GHOST_SystemCocoa.mm +++ b/intern/ghost/intern/GHOST_SystemCocoa.mm @@ -824,21 +824,19 @@ GHOST_IWindow *GHOST_SystemCocoa::getWindowUnderCursor(int32_t x, int32_t y) GHOST_TSuccess GHOST_SystemCocoa::getPixelAtCursor(float r_color[3]) const { @autoreleasepool { - CGPoint cursorPosition = [NSEvent mouseLocation]; - - uint32_t count = 0; - CGDirectDisplayID displayForPoint; - if (CGGetDisplaysWithPoint(cursorPosition, 1, &displayForPoint, &count) != kCGErrorSuccess) - { + CGPoint cursorPosition = CGEventGetLocation(CGEventCreate(NULL)); + + cursorPosition.y = mainScreen().frame.y - cursorPosition.y + 20; + + // Create rect to get color at location + CGRect rect = CGRectMake(cursorPosition.x, cursorPosition.y, 1, 1); + + // Create Image from rect + CGImageRef image = CGDisplayCreateImageForRect(CGMainDisplayID(), rect); + if (image == nil) { return GHOST_kFailure; } - // Create rect to get color at location - CGRect rect = CGRectMake(cursorPosition.x, cursorPosition.y - 2, 1, 1); - - // Create Image from rect - CGImageRef image = CGDisplayCreateImageForRect(displayForPoint, rect); - // Create bitmap from image & free it NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image]; if (bitmap == nil) { @@ -860,6 +858,8 @@ GHOST_TSuccess GHOST_SystemCocoa::getPixelAtCursor(float r_color[3]) const r_color[1] = (float)green; r_color[2] = (float)blue; + [bitmap release]; + return GHOST_kSuccess; } } -- 2.30.2