0

I'm trying to create a purely JXA-ObjC approach to getting pixel colors from image paths. Here's what I have currently:

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)
c_img.lockFocus()     //Error - Undefined is not a function...?
var c_color = NSReadPixel(c_point)
c_img.unlockFocus()    //Error - Undefined is not a function...?
c_img.release()

var r; var g; var b; var a
c_img.getRegGreenBlueAlpha($(r),$(g),$(b),$(a))

r = ObjC.unwrap(r)
g = ObjC.unwrap(g)
b = ObjC.unwrap(b)
a = ObjC.unwrap(a)

This code is heavily based off of the code found here.

However, as shown above c_img.lockFocus() is undefined according to JXA. Oddly I can get access to c_img.lockFocusFlipped(), however I'm not sure how to use this and/or if it can be used for the same purpose as lockFocus().

Is there an obvious problem here? Or is there a better way to get the pixel colour of an image?

Any help would be grateful.

Sancarn
  • 2,575
  • 20
  • 45

1 Answers1

0

It looks like I am too used to methods requiring parenthesis. TylerGaw however told me that this is not necessarily the case.

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)
c_img.lockFocus
var c_color = NSReadPixel(c_point)
c_img.unlockFocus
c_img.release

appears to work as expected.

Sancarn
  • 2,575
  • 20
  • 45