Skip to content

Commit 4fbc831

Browse files
authored
(#327) Apply pixel density scaling when querying pixel color information (#332)
1 parent 6ab5de6 commit 4fbc831

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/screen.class.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,10 @@ export class ScreenClass {
303303
* @param point Location to query color information from
304304
*/
305305
public async colorAt(point: Point | Promise<Point>) {
306-
const screenContent = this.providerRegistry.getScreen().grabScreen();
307-
return this.providerRegistry.getImageProcessor().colorAt(screenContent, point);
306+
const screenContent = await this.providerRegistry.getScreen().grabScreen();
307+
const inputPoint = await point;
308+
const scaledPoint = new Point(inputPoint.x * screenContent.pixelDensity.scaleX, inputPoint.y * screenContent.pixelDensity.scaleY);
309+
return this.providerRegistry.getImageProcessor().colorAt(screenContent, scaledPoint);
308310
}
309311

310312
private async saveImage(

lib/screen.colorAt.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,27 @@ describe("colorAt", () => {
4141
expect(white).toStrictEqual(expectedWhite);
4242
expect(black).toStrictEqual(expectedBlack);
4343
});
44+
45+
it("should account for pixel density when retrieving pixel color", async () => {
46+
// GIVEN
47+
const screenshot = await loadImage(`${__dirname}/../e2e/assets/checkers.png`);
48+
screenshot.pixelDensity.scaleX = 2.0;
49+
screenshot.pixelDensity.scaleY = 2.0;
50+
const grabScreenMock = jest.fn(() => Promise.resolve(screenshot));
51+
providerRegistryMock.getScreen = jest.fn(() => mockPartial<ScreenProviderInterface>({
52+
grabScreen: grabScreenMock
53+
}));
54+
providerRegistryMock.getImageProcessor()
55+
const SUT = new ScreenClass(providerRegistryMock);
56+
const expectedWhite = new RGBA(255, 255, 255, 255);
57+
const expectedBlack = new RGBA(0, 0, 0, 255);
58+
59+
// WHEN
60+
const white = await SUT.colorAt(new Point(32, 32));
61+
const black = await SUT.colorAt(new Point(96, 32));
62+
63+
// THEN
64+
expect(white).toStrictEqual(expectedWhite);
65+
expect(black).toStrictEqual(expectedBlack);
66+
});
4467
});

0 commit comments

Comments
 (0)