Skip to content

Instantly share code, notes, and snippets.

@niccolomineo
Last active August 7, 2018 15:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save niccolomineo/5d53356e80cbe10db661c579c57f70fe to your computer and use it in GitHub Desktop.
Save niccolomineo/5d53356e80cbe10db661c579c57f70fe to your computer and use it in GitHub Desktop.
Get NS RGB components at specific coordinates based on main display id
func getNSComponentsAt(x: Int, y: Int) -> NSColor {
let screenID = NSScreen.main!.deviceDescription[NSDeviceDescriptionKey("NSScreenNumber")] as! UInt32
let image: CGImage = CGDisplayCreateImage(CGDirectDisplayID(screenID), rect: CGRect(x: CGFloat(x), y: CGFloat(y), width: 1, height: 1))!
let bitmap = NSBitmapImageRep(cgImage: image)
let color = bitmap.colorAt(x: 0, y: 0)
self.redComponent = (color?.redComponent)!
self.greenComponent = (color?.greenComponent)!
self.blueComponent = (color?.blueComponent)!
return color!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment