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

Regression - hs.image:colorAt() doesn't handle scale correctly #3184

Closed
latenitefilms opened this issue Apr 11, 2022 · 0 comments · Fixed by #3185
Closed

Regression - hs.image:colorAt() doesn't handle scale correctly #3184

latenitefilms opened this issue Apr 11, 2022 · 0 comments · Fixed by #3185

Comments

@latenitefilms
Copy link
Contributor

The changes made here:

5d34265

...don't seem to take into account the correct scale (because the NSImage size method returns size information that is screen resolution dependent).

@randomeizer has come up with the suggested fix, which I'll do a pull request for shortly:

/// 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 sharedWithState:L] ;
    [skin checkArgs:LS_TUSERDATA, USERDATA_TAG, LS_TTABLE, LS_TBREAK];

    NSImage *theImage = [skin luaObjectAtIndex:1 toClass:"NSImage"] ;
    NSPoint point  = [skin tableToPointAtIndex:2] ;

    NSColor *pixelColor = nil;
    @autoreleasepool {
        CGImageRef CGImage = [theImage CGImageForProposedRect:nil context:nil hints:nil];
        NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithCGImage:CGImage];
        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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant