Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1559 - Adds hs.image:colorAt(point) #1868

Merged
merged 7 commits into from
Jun 14, 2018
Merged
26 changes: 26 additions & 0 deletions extensions/image/internal.m
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,31 @@ static int getImageSize(lua_State* L) {
return 1 ;
}

/// hs.image:colorAt(point) -> table
/// Method
/// Reads the color of the pixel at the specified location.
///
/// Parameters:
/// * `point` - a `hs.geometry.point`
///
/// Returns:
/// * A `hs.drawing.color` object
static int colorAt(lua_State* L) {
LuaSkin *skin = [LuaSkin shared] ;
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TTABLE, LS_TBREAK] ;
NSImage *theImage = [skin luaObjectAtIndex:1 toClass:"NSImage"] ;

NSPoint point = [skin tableToPointAtIndex:2] ;

// Source: https://stackoverflow.com/a/48400410
[theImage lockFocus];
NSColor *pixelColor = NSReadPixel(point);
[theImage unlockFocus];

[skin pushNSObject:pixelColor];
return 1;
}

/// hs.image:croppedCopy(rectangle) -> object
/// Method
/// Returns a copy of the portion of the image specified by the rectangle specified.
Expand Down Expand Up @@ -1624,6 +1649,7 @@ static int userdata_gc(lua_State* L) {
{"croppedCopy", croppedCopy},
{"saveToFile", saveToFile},
{"encodeAsURLString", encodeAsString},
{"colorAt", colorAt},

{"__tostring", userdata_tostring},
{"__eq", userdata_eq},
Expand Down