Skip to content

Feature/500/color finder #501

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

Merged
merged 11 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added e2e/assets/dot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added e2e/assets/dots.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 18 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ import { LineHelper } from "./lib/util/linehelper.class";
import { createWindowApi } from "./lib/window.function";
import providerRegistry from "./lib/provider/provider-registry.class";
import { loadImageResource } from "./lib/imageResources.function";
import { LineQuery, WindowQuery, WordQuery } from "./lib/query.class";
import {
ColorQuery,
LineQuery,
WindowQuery,
WordQuery,
} from "./lib/query.class";
import { RGBA } from "./lib/rgba.class";

export {
AssertClass,
Expand Down Expand Up @@ -100,6 +106,16 @@ const windowWithTitle = (title: string | RegExp): WindowQuery => {
};
};

const pixelWithColor = (color: RGBA): ColorQuery => {
return {
type: "color",
id: `pixel-by-color-query-RGBA(${color.R},${color.G},${color.B},${color.A})`,
by: {
color,
},
};
};

export { fetchFromUrl } from "./lib/imageResources.function";

export {
Expand All @@ -121,4 +137,5 @@ export {
singleWord,
textLine,
windowWithTitle,
pixelWithColor,
};
47 changes: 35 additions & 12 deletions lib/match-request.class.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { Image, isImage } from "./image.class";
import { Region } from "./region.class";
import { OptionalSearchParameters } from "./optionalsearchparameters.class";
import { isLineQuery, isTextQuery, TextQuery } from "./query.class";
import { RegionResultFindInput } from "./screen.class";
import {
ColorQuery,
isColorQuery,
isLineQuery,
isTextQuery,
TextQuery,
} from "./query.class";
import { ProviderRegistry } from "./provider/provider-registry.class";
import { PointResultFindInput, RegionResultFindInput } from "./screen.class";

export class MatchRequest<NEEDLE_TYPE, PROVIDER_DATA_TYPE> {
public constructor(
Expand All @@ -26,42 +32,48 @@ export function isTextMatchRequest<PROVIDER_DATA_TYPE>(
return isTextQuery(matchRequest.needle);
}

export function isColorMatchRequest<PROVIDER_DATA_TYPE>(
matchRequest: any
): matchRequest is MatchRequest<ColorQuery, PROVIDER_DATA_TYPE> {
return isColorQuery(matchRequest.needle);
}

export function createMatchRequest<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
needle: TextQuery | Promise<TextQuery>,
needle: PointResultFindInput,
searchRegion: Region,
minMatch: number,
screenImage: Image,
params?: OptionalSearchParameters<PROVIDER_DATA_TYPE>
): MatchRequest<TextQuery, PROVIDER_DATA_TYPE>;
): MatchRequest<PointResultFindInput, PROVIDER_DATA_TYPE>;
export function createMatchRequest<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
needle: Image | Promise<Image>,
needle: RegionResultFindInput,
searchRegion: Region,
minMatch: number,
screenImage: Image,
params?: OptionalSearchParameters<PROVIDER_DATA_TYPE>
): MatchRequest<Image, PROVIDER_DATA_TYPE>;
): MatchRequest<RegionResultFindInput, PROVIDER_DATA_TYPE>;
export function createMatchRequest<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
needle: RegionResultFindInput | Promise<RegionResultFindInput>,
needle: RegionResultFindInput | PointResultFindInput,
searchRegion: Region,
minMatch: number,
screenImage: Image,
params?: OptionalSearchParameters<PROVIDER_DATA_TYPE>
):
| MatchRequest<TextQuery, PROVIDER_DATA_TYPE>
| MatchRequest<Image, PROVIDER_DATA_TYPE>;
| MatchRequest<RegionResultFindInput, PROVIDER_DATA_TYPE>
| MatchRequest<PointResultFindInput, PROVIDER_DATA_TYPE>;
export function createMatchRequest<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
needle: RegionResultFindInput | Promise<RegionResultFindInput>,
needle: RegionResultFindInput | PointResultFindInput,
searchRegion: Region,
minMatch: number,
screenImage: Image,
params?: OptionalSearchParameters<PROVIDER_DATA_TYPE>
):
| MatchRequest<Image, PROVIDER_DATA_TYPE>
| MatchRequest<TextQuery, PROVIDER_DATA_TYPE> {
| MatchRequest<RegionResultFindInput, PROVIDER_DATA_TYPE>
| MatchRequest<PointResultFindInput, PROVIDER_DATA_TYPE> {
if (isImage(needle)) {
providerRegistry
.getLogProvider()
Expand Down Expand Up @@ -90,6 +102,17 @@ export function createMatchRequest<PROVIDER_DATA_TYPE>(
minMatch,
params?.providerData
);
} else if (isColorQuery(needle)) {
const color = needle.by.color;
providerRegistry
.getLogProvider()
.info(
`Searching for color RGBA(${color.R},${color.G},${color.B},${
color.A
}) in region ${searchRegion.toString()}.`
);

return new MatchRequest(screenImage, needle, 1, params?.providerData);
}
throw new Error(`Unknown input type: ${JSON.stringify(needle)}`);
}
79 changes: 42 additions & 37 deletions lib/match-result.class.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,68 @@
import { Region } from "./region.class";
import { isImageMatchRequest, MatchRequest } from "./match-request.class";
import {
isColorMatchRequest,
isImageMatchRequest,
isTextMatchRequest,
MatchRequest,
} from "./match-request.class";
import { ProviderRegistry } from "./provider/provider-registry.class";
import { Image } from "./image.class";
import { TextQuery } from "./query.class";
import { Point } from "./point.class";
import { PointResultFindInput, RegionResultFindInput } from "./screen.class";

export class MatchResult {
export class MatchResult<LOCATION_TYPE> {
constructor(
public readonly confidence: number,
public readonly location: Region,
public readonly location: LOCATION_TYPE,
public readonly error?: Error
) {}
}

export async function getMatchResults<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
matchRequest: MatchRequest<TextQuery, PROVIDER_DATA_TYPE>
): Promise<MatchResult[]>;
matchRequest: MatchRequest<RegionResultFindInput, PROVIDER_DATA_TYPE>
): Promise<MatchResult<Region>[]>;
export async function getMatchResults<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
matchRequest: MatchRequest<Image, PROVIDER_DATA_TYPE>
): Promise<MatchResult[]>;
matchRequest: MatchRequest<PointResultFindInput, PROVIDER_DATA_TYPE>
): Promise<MatchResult<Point>[]>;
export async function getMatchResults<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
matchRequest:
| MatchRequest<Image, PROVIDER_DATA_TYPE>
| MatchRequest<TextQuery, PROVIDER_DATA_TYPE>
): Promise<MatchResult[]>;
export async function getMatchResults<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
matchRequest:
| MatchRequest<Image, PROVIDER_DATA_TYPE>
| MatchRequest<TextQuery, PROVIDER_DATA_TYPE>
): Promise<MatchResult[]> {
return isImageMatchRequest(matchRequest)
? providerRegistry.getImageFinder().findMatches(matchRequest)
: providerRegistry.getTextFinder().findMatches(matchRequest);
| MatchRequest<RegionResultFindInput, PROVIDER_DATA_TYPE>
| MatchRequest<PointResultFindInput, PROVIDER_DATA_TYPE>
): Promise<MatchResult<Point | Region>[]> {
if (isImageMatchRequest(matchRequest)) {
return providerRegistry.getImageFinder().findMatches(matchRequest);
} else if (isTextMatchRequest(matchRequest)) {
return providerRegistry.getTextFinder().findMatches(matchRequest);
} else if (isColorMatchRequest(matchRequest)) {
return providerRegistry.getColorFinder().findMatches(matchRequest);
}
throw new Error(
`Unknown match request type: ${JSON.stringify(matchRequest.needle)}`
);
}

export async function getMatchResult<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
matchRequest: MatchRequest<TextQuery, PROVIDER_DATA_TYPE>
): Promise<MatchResult>;
matchRequest: MatchRequest<RegionResultFindInput, PROVIDER_DATA_TYPE>
): Promise<MatchResult<Region>>;
export async function getMatchResult<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
matchRequest: MatchRequest<Image, PROVIDER_DATA_TYPE>
): Promise<MatchResult>;
export async function getMatchResult<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
matchRequest:
| MatchRequest<Image, PROVIDER_DATA_TYPE>
| MatchRequest<TextQuery, PROVIDER_DATA_TYPE>
): Promise<MatchResult>;
matchRequest: MatchRequest<PointResultFindInput, PROVIDER_DATA_TYPE>
): Promise<MatchResult<Point>>;
export async function getMatchResult<PROVIDER_DATA_TYPE>(
providerRegistry: ProviderRegistry,
matchRequest:
| MatchRequest<Image, PROVIDER_DATA_TYPE>
| MatchRequest<TextQuery, PROVIDER_DATA_TYPE>
): Promise<MatchResult> {
return isImageMatchRequest(matchRequest)
? providerRegistry.getImageFinder().findMatch(matchRequest)
: providerRegistry.getTextFinder().findMatch(matchRequest);
| MatchRequest<RegionResultFindInput, PROVIDER_DATA_TYPE>
| MatchRequest<PointResultFindInput, PROVIDER_DATA_TYPE>
): Promise<MatchResult<Point | Region>> {
if (isImageMatchRequest(matchRequest)) {
return providerRegistry.getImageFinder().findMatch(matchRequest);
} else if (isTextMatchRequest(matchRequest)) {
return providerRegistry.getTextFinder().findMatch(matchRequest);
} else if (isColorMatchRequest(matchRequest)) {
return providerRegistry.getColorFinder().findMatch(matchRequest);
}
throw new Error("Unknown match request type");
}
33 changes: 33 additions & 0 deletions lib/provider/color-finder.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ColorQuery } from "../query.class";
import { MatchResult } from "../match-result.class";
import { Point } from "../point.class";
import { MatchRequest } from "../match-request.class";

/**
* A WindowFinder should provide an abstraction layer to perform window searches
*
* @interface ColorFinderInterface
*/
export interface ColorFinderInterface {
/**
* findMatch should provide an abstraction to search for a color on screen
*
* @param {ColorQuery} query A {@link ColorQuery} containing needed data
* @returns {Promise<number>} A single window handle
* @memberof WindowFinderInterface
*/
findMatch<PROVIDER_DATA_TYPE>(
query: MatchRequest<ColorQuery, PROVIDER_DATA_TYPE>
): Promise<MatchResult<Point>>;

/**
* findMatches should provide an abstraction to search for a window on screen
*
* @param {ColorQuery} query A {@link ColorQuery} containing needed data
* @returns {Promise<number[]>} A list of window handles
* @memberof WindowFinderInterface
*/
findMatches<PROVIDER_DATA_TYPE>(
query: MatchRequest<ColorQuery, PROVIDER_DATA_TYPE>
): Promise<MatchResult<Point>[]>;
}
68 changes: 68 additions & 0 deletions lib/provider/color/color-finder.class.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import ColorFinder from "./color-finder.class";
import { join } from "path";
import {
ColorQuery,
loadImage,
MatchRequest,
MatchResult,
Point,
RGBA,
} from "../../../index";

describe("color finder", () => {
describe("find", () => {
it("should resolve", async () => {
// GIVEN
const screenImage = await loadImage(
join(__dirname, `../../../e2e/assets/dot.png`)
);
const SUT = new ColorFinder();
const colorQuery: ColorQuery = {
id: "colorFinderTest",
type: "color",
by: {
color: new RGBA(255, 0, 0, 255),
},
};
const matchRequest = new MatchRequest(screenImage, colorQuery, 0.9);

const expectedResult = new MatchResult(1, new Point(60, 60));

// WHEN
const result = await SUT.findMatch(matchRequest);

// THEN
expect(result).toEqual(expectedResult);
});
});

describe("findAll", () => {
it("should resolve", async () => {
// GIVEN
const screenImage = await loadImage(
join(__dirname, `../../../e2e/assets/dots.png`)
);
const SUT = new ColorFinder();
const colorQuery: ColorQuery = {
id: "colorFinderTest",
type: "color",
by: {
color: new RGBA(255, 0, 0, 255),
},
};
const matchRequest = new MatchRequest(screenImage, colorQuery, 0.9);

const expectedResult = [
new MatchResult(1, new Point(60, 60)),
new MatchResult(1, new Point(60, 80)),
new MatchResult(1, new Point(60, 100)),
];

// WHEN
const result = await SUT.findMatches(matchRequest);

// THEN
expect(result).toEqual(expectedResult);
});
});
});
Loading