Skip to content

Instantly share code, notes, and snippets.

@antimatter15
Last active July 16, 2023 16:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antimatter15/dc809b81abea21a69f2798ff5d24ca4f to your computer and use it in GitHub Desktop.
Save antimatter15/dc809b81abea21a69f2798ff5d24ca4f to your computer and use it in GitHub Desktop.
Read Pixel Under Cursor Mac
#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;
}
//: 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)
}
}
}
@osnr
Copy link

osnr commented Nov 26, 2020

@hlung
Copy link

hlung commented Jul 16, 2023

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