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