Skip to content

Commit

Permalink
hs.image:colorAt() now takes into account scale
Browse files Browse the repository at this point in the history
- Closes #3184
  • Loading branch information
latenitefilms committed Apr 11, 2022
1 parent 6eda506 commit bd9870a
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions extensions/image/libimage.m
Original file line number Diff line number Diff line change
Expand Up @@ -1304,7 +1304,7 @@ static int getImageSize(lua_State* L) {
/// * A `hs.drawing.color` object
static int colorAt(lua_State* L) {
LuaSkin *skin = [LuaSkin sharedWithState:L] ;
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TTABLE, LS_TBREAK] ;
[skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TTABLE, LS_TBREAK];

NSImage *theImage = [skin luaObjectAtIndex:1 toClass:"NSImage"] ;
NSPoint point = [skin tableToPointAtIndex:2] ;
Expand All @@ -1313,9 +1313,13 @@ static int colorAt(lua_State* L) {
@autoreleasepool {
CGImageRef CGImage = [theImage CGImageForProposedRect:nil context:nil hints:nil];
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCGImage:CGImage];
pixelColor = [rep colorAtX:point.x y:point.y];
NSSize imageSize = [theImage size];
NSSize bitmapSize = [rep size];
CGFloat xScale = bitmapSize.width / imageSize.width;
CGFloat yScale = bitmapSize.height / imageSize.height;
pixelColor = [rep colorAtX:(point.x * xScale) y:(point.y * yScale)];
}

[skin pushNSObject:pixelColor];

return 1;
Expand Down

0 comments on commit bd9870a

Please sign in to comment.