JXA ObjC - Get Color of pixel

Hi folks,

I found myself needing a method of getting the color of a pixel using JXA. After a bit of browsing I came across this post which gave a solution back in 2008. From that post I also came across this ObjC code.

I’ve had little to no experience with AppleScript/JXA-ObjC bridge, so was wondering if anyone could help me port the application to pure JXA-ObjC?

function getPixelColor(x,y,picturePath){
    ObjC.import('Foundation')
    ObjC.import('AppKit')
    //some JXA-ObjC code...?
    return [r,g,b,a]
}

Edit here is the updated code from various advice I have been given around the internet. Many thanks to @TylerGaw on Twitter for the help here. See https://gist.github.com/tylergaw/1c7ba1d8480224278a8b3d32bbd83b4b for details

ObjC.import('Foundation')
ObjC.import('AppKit')
var c_filePath = $(picturePath)
var c_img = $.NSImage.alloc.initWithContentsOfFile(c_filePath)
if(c_img==$()){
    return []
}
var c_point = $.NSMakePoint(x,y)
// Don't use parens "()" when an Obj-C method takes zero arguments
c_img.lockFocus
var c_color = $.NSReadPixel(c_point)
c_img.unlockFocus
c_img.release

var r=$(); var b=$(); var g=$(); var a=$();
c_color.getRedGreenBlueAlpha(r,g,b,a) //Crash caused here.
return [r,g,b,a]

Does anyone know why the crash at c_color.getRedGreenBlueAlpha() occurs?

My guess is, to solve the crash, I need to subclass the application…