Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Adds `hs.image:colorAt(point)`
- Closes #1559
  • Loading branch information
latenitefilms authored and cmsj committed Jun 14, 2018
1 parent bf43dd7 commit 596a280
Showing 1 changed file with 26 additions and 0 deletions.
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

2 comments on commit 596a280

@chdiza
Copy link

@chdiza chdiza commented on 596a280 Jun 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, please don't make the commit message have a first line identical with the issue number. That messes up clicking on commits in the GitHub web interface---the link one clicks on points not to the commit, but to the issue.

Perhaps "Fix #NNNN" would be better, because then at least one can click on the word "Fix". But even then, such a commit message doesn't say what's being done (unless one expands the little triangle).

For this very commit, e.g., it probably would've been best to make the first line be "Adds hs.image:colorAt(point)"

@cmsj
Copy link
Member

@cmsj cmsj commented on 596a280 Jun 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. I should have fixed that when I merged it. Sorry!

Please sign in to comment.