Last active
December 29, 2024 18:55
-
-
Save antimatter15/dc809b81abea21a69f2798ff5d24ca4f to your computer and use it in GitHub Desktop.
Read Pixel Under Cursor Mac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <Cocoa/Cocoa.h> | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
// Grab the current mouse location. | |
CGPoint mouseLoc = CGEventGetLocation(CGEventCreate(NULL)); | |
// NSPoint mouseLoc = [NSEvent mouseLocation]; | |
// CGPoint mouseLocation = CGEventGetLocation(CGEventCreate(NULL)); | |
// Grab the display for said mouse location. | |
uint32_t count = 0; | |
CGDirectDisplayID displayForPoint; | |
if (CGGetDisplaysWithPoint(mouseLoc, 1, &displayForPoint, &count) != kCGErrorSuccess) | |
{ | |
NSLog(@"Oops."); | |
return 0; | |
} | |
// Grab the color on said display at said mouse location. | |
CGImageRef image = CGDisplayCreateImageForRect(displayForPoint, CGRectMake(mouseLoc.x, mouseLoc.y - 2, 1, 1)); | |
NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image]; | |
CGImageRelease(image); | |
NSColor* color = [bitmap colorAtX:0 y:0]; | |
NSLog(@"%@", color); | |
// [bitmap release]; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import Cocoa | |
var mouseLoc = CGEventGetLocation(CGEventCreate(nil).takeRetainedValue()) | |
var display = UnsafeMutablePointer<CGDirectDisplayID>.alloc(1) | |
var count = UnsafeMutablePointer<UInt32>.alloc(1); | |
CGGetDisplaysWithPoint(mouseLoc, 1, display, count) | |
let windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, CGWindowID.allZeros) | |
//CGWindowListCreateImage(screenBounds: CGRect, listOption: CGWindowListOption, <#windowID: CGWindowID#>, <#imageOption: CGWindowImageOption#>) | |
var image = CGDisplayCreateImageForRect(display.memory, CGRectMake(mouseLoc.x, mouseLoc.y, 100, 100)).takeRetainedValue() | |
//var image = CGWindowListCreateImage(CGRectInfinite, nil, <#windowID: CGWindowID#>, <#imageOption: CGWindowImageOption#>) | |
let dest = CGImageDestinationCreateWithURL(NSURL(fileURLWithPath: "merp.png"), kUTTypePNG, 1, nil) | |
CGImageDestinationAddImage(dest, image, nil) | |
CGImageDestinationFinalize(dest) | |
//https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBitmapImageRep_Class/index.html#//apple_ref/occ/instm/NSBitmapImageRep/initWithCGImage: | |
var bitmap = NSBitmapImageRep(CGImage: image) | |
bitmap.colorAtX(0, y: 0) | |
display.dealloc(1) | |
count.dealloc(1) | |
//setBrightnessLevel(0.9) | |
//http://bl.ocks.org/mbostock/981b42034400e48ac637 | |
//var image = CGDisplayCreateImageForRect( | |
//http://www.alecjacobson.com/weblog/?p=1127 | |
private func setBrightnessLevel(level: Float) { | |
var iterator: io_iterator_t = 0 | |
let result = IOServiceGetMatchingServices(kIOMasterPortDefault, | |
IOServiceMatching("IODisplayConnect").takeUnretainedValue(), | |
&iterator) | |
if result == kIOReturnSuccess { | |
var service: io_object_t = 1 | |
for ;; { | |
service = IOIteratorNext(iterator) | |
if service == 0 { | |
break | |
} | |
IODisplaySetFloatParameter(service, 0, kIODisplayBrightnessKey, level) | |
IOObjectRelease(service) | |
} | |
} | |
} | |
Update for Swift 5.8
import AppKit
class Agent {
func readPixelUnderMouse() throws -> NSColor {
var mouseLoc = NSEvent.mouseLocation
// Cocoa and Core Graphics (Quartz) use different coordinate systems. So need to flip y.
mouseLoc.y = NSHeight(NSScreen.screens[0].frame) - mouseLoc.y
print(mouseLoc)
let display = UnsafeMutablePointer<CGDirectDisplayID>.allocate(capacity: 1)
let count = UnsafeMutablePointer<UInt32>.allocate(capacity: 1)
CGGetDisplaysWithPoint(mouseLoc, 1, display, count)
guard let image = CGDisplayCreateImage(display.pointee, rect: CGRectMake(mouseLoc.x, mouseLoc.y, 1, 1)) else { throw NSError() }
let bitmap = NSBitmapImageRep(cgImage: image)
guard let color = bitmap.colorAt(x: 0, y: 0) else { throw NSError() }
display.deallocate()
count.deallocate()
return color
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi https://github.com/osnr/pixels-under-mouse