Finding color of a pixel

Hi guys, im trying to find the color of a pixel at coordinates x: -925 & y: 800

I have two scripts, but both fail to work.


I tried this script but it keeps giving errors:


import Foundation
func getPixel(_ p: UnsafeMutablePointer<Int>!, atX x: Int, y: Int)
{
  return
}
print(getPixel(_:atX:-925y800))



and this is the other script


import Foundation
var color: NSColor? = colorAt(x: -925, y: 800)


print(colorAt())


does anybody know how I can do this properly?

Accepted Answer

colorAt is an instance method of NSBitmapImageRep class.


So you need to call it as anNSBitmapImageRep.colorAt(x: 100, y: 100).


I don't understand your second script: you call colorAt without any parameter. I suupose you meant print(color).


Same issuen with getPixel.


Here again, don't understand your script.


You should not redefine getPixel, but just call it on an instance of NSBitmapImageRep class


In addition, why do you pass a negative x coordinate ?


Here is a solution (in objc) : h ttps://stackoverflow.com/questions/42124148/nsreadpixel-always-returns-nil



In Swift : h ttps://stackoverflow.com/questions/46961761/accurately-get-a-color-from-pixel-on-screen-and-convert-its-color-space

let dispID = CGMainDisplayID()     // Get the main display ID
// create a 1x1 image at the mouse position
if let image:CGImage = CGDisplayCreateImage(dispID, rect: CGRect(x: x, y: y, width: 1, height: 1)) {
    let bitmap = NSBitmapImageRep(cgImage: image)
    // get the color from the bitmap 
    let color = bitmap.colorAt(x: 0, y: 0)!
    print(color)
}


You'll get a result like :

NSCalibratedRGBColorSpace 0.803922 0.615686 0.235294 1

thank you so much

Finding color of a pixel
 
 
Q