@@ -266,6 +266,32 @@ Main record used to find and interact with complex color objects.
266266 Transformer: TColorFinderTransformer;
267267 end;
268268
269+ function TColorFinder._FindHelper(area: TBox): TPointArray;
270+ var
271+ i: Integer;
272+ primary, secondary: TPointArray;
273+ begin
274+ for i := 0 to High(Self.Colors) do //Find TColorFinder.Colors
275+ Result += Target.FindColor(Self.Colors[i], area);
276+
277+ for i := 0 to High(Self.ColorClusters) do //Find TColorFinder.ColorClusters
278+ begin
279+ if Self.ColorClusters[i].Distance <= 0 then
280+ begin
281+ WriteLn GetDebugLn('TColorFinder', 'Skipping cluster with 0 distance, make it higher than 0!', ELogLevel.WARN);
282+ Continue;
283+ end;
284+
285+ primary := Target.FindColor(Self.ColorClusters[i].Primary, area);
286+ if primary = [] then Continue;
287+
288+ secondary := Target.FindColor(Self.ColorClusters[i].Secondary, area);
289+ if secondary = [] then Continue;
290+
291+ Result += primary.PointsNearby(secondary, 0, MainScreen.NormalizeDistance(Self.ColorClusters[i].Distance));
292+ end;
293+ end;
294+
269295(*
270296### TColorFinder.Find
271297```pascal
@@ -283,32 +309,12 @@ function TColorFinder.Find(out atpa: T2DPointArray; areas: TBoxArray): Boolean;
283309var
284310 transformer: TColorFinderTransformer;
285311 area: TBox;
286- i: Integer;
287- tpa, primary, secondary: TPointArray;
312+ tpa: TPointArray;
288313begin
289314 transformer := Self.Transformer.Normalize();
290315
291316 for area in areas do
292- begin
293- for i := 0 to High(Self.Colors) do // Find TColorFinder.Colors
294- tpa += Target.FindColor(Self.Colors[i], area);
295-
296- for i := 0 to High(Self.ColorClusters) do // Find TColorFinder.ColorClusters
297- begin
298- if Self.ColorClusters[i].Distance <= 0 then
299- begin
300- WriteLn GetDebugLn('TColorFinder', 'Skipping cluster with 0 distance, make it higher than 0!', ELogLevel.WARN);
301- Continue;
302- end;
303-
304- primary := Target.FindColor(Self.ColorClusters[i].Primary, area);
305- if primary = [] then Continue;
306- secondary := Target.FindColor(Self.ColorClusters[i].Secondary, area);
307- if secondary = [] then Continue;
308-
309- tpa += primary.PointsNearby(secondary, 0, MainScreen.NormalizeDistance(Self.ColorClusters[i].Distance));
310- end;
311- end;
317+ tpa += Self._FindHelper(area);
312318
313319 atpa := transformer.Process(tpa);
314320 Result := atpa <> [];
@@ -318,34 +324,14 @@ function TColorFinder.Find(out atpa: T2DPointArray; areas: TPolygonArray): Boole
318324var
319325 transformer: TColorFinderTransformer;
320326 area: TPolygon;
321- bounds: TBox;
322- i: Integer;
323- tpa, primary, secondary, tmp: TPointArray;
327+ tpa, tmp: TPointArray;
324328begin
325329 transformer := Self.Transformer.Normalize();
326330
327331 for area in areas do
328332 begin
329- bounds := area.Bounds();
330- for i := 0 to High(Self.Colors) do // Find TColorFinder.Colors
331- tmp += Target.FindColor(Self.Colors[i], bounds);
332-
333- for i := 0 to High(Self.ColorClusters) do // Find TColorFinder.ColorClusters
334- begin
335- if Self.ColorClusters[i].Distance <= 0 then
336- begin
337- WriteLn GetDebugLn('TColorFinder', 'Skipping cluster with 0 distance, make it higher than 0!', ELogLevel.WARN);
338- Continue;
339- end;
340-
341- primary := Target.FindColor(Self.ColorClusters[i].Primary, bounds);
342- if primary = [] then Continue;
343- secondary := Target.FindColor(Self.ColorClusters[i].Secondary, bounds);
344- if secondary = [] then Continue;
345-
346- tmp += primary.PointsNearby(secondary, 0, MainScreen.NormalizeDistance(Self.ColorClusters[i].Distance));
347- end;
348-
333+ tmp := Self._FindHelper(area.Bounds());
334+ if tmp = [] then Continue;
349335 tpa += tmp.ExtractPolygon(area);
350336 tmp := [];
351337 end;
@@ -358,34 +344,14 @@ function TColorFinder.Find(out atpa: T2DPointArray; areas: TCuboidArray): Boolea
358344var
359345 transformer: TColorFinderTransformer;
360346 area: TCuboid;
361- bounds: TBox;
362- i: Integer;
363- tpa, primary, secondary, tmp: TPointArray;
347+ tpa, tmp: TPointArray;
364348begin
365349 transformer := Self.Transformer.Normalize();
366350
367351 for area in areas do
368352 begin
369- bounds := area.Bounds();
370- for i := 0 to High(Self.Colors) do // Find TColorFinder.Colors
371- tmp += Target.FindColor(Self.Colors[i], bounds);
372-
373- for i := 0 to High(Self.ColorClusters) do // Find TColorFinder.ColorClusters
374- begin
375- if Self.ColorClusters[i].Distance <= 0 then
376- begin
377- WriteLn GetDebugLn('TColorFinder', 'Skipping cluster with 0 distance, make it higher than 0!', ELogLevel.WARN);
378- Continue;
379- end;
380-
381- primary := Target.FindColor(Self.ColorClusters[i].Primary, bounds);
382- if primary = [] then Continue;
383- secondary := Target.FindColor(Self.ColorClusters[i].Secondary, bounds);
384- if secondary = [] then Continue;
385-
386- tmp += primary.PointsNearby(secondary, 0, MainScreen.NormalizeDistance(Self.ColorClusters[i].Distance));
387- end;
388-
353+ tmp := Self._FindHelper(area.Bounds());
354+ if tmp = [] then Continue;
389355 tpa += tmp.ExtractPolygon(area.Polygon());
390356 tmp := [];
391357 end;
@@ -398,48 +364,20 @@ end;
398364function TColorFinder.FindEx(out atpa: T2DPointArray; out found: TBooleanArray; areas: TBoxArray): Boolean;
399365var
400366 transformer: TColorFinderTransformer;
401- area: TBox;
402367 i: Integer;
403- tpa, primary, secondary : TPointArray;
368+ tpa: TPointArray;
404369begin
405370 transformer := Self.Transformer.Normalize();
406371
407- for area in areas do
372+ SetLength(found, Length(areas));
373+ for i := 0 to High(areas) do
408374 begin
409- for i := 0 to High(Self.Colors) do // Find TColorFinder.Colors
410- tpa += Target.FindColor(Self.Colors[i], area);
411-
412- for i := 0 to High(Self.ColorClusters) do // Find TColorFinder.ColorClusters
413- begin
414- if Self.ColorClusters[i].Distance <= 0 then
415- begin
416- WriteLn GetDebugLn('TColorFinder', 'Skipping cluster with 0 distance, make it higher than 0!', ELogLevel.WARN);
417- Continue;
418- end;
419-
420- primary := Target.FindColor(Self.ColorClusters[i].Primary, area);
421- if primary = [] then Continue;
422- secondary := Target.FindColor(Self.ColorClusters[i].Secondary, area);
423- if secondary = [] then Continue;
424-
425- tpa += primary.PointsNearby(secondary, 0, MainScreen.NormalizeDistance(Self.ColorClusters[i].Distance));
426- end;
427-
428- if tpa = [] then
429- begin
430- found += False;
431- Continue;
432- end;
433-
375+ tpa := Self._FindHelper(areas[i]);
376+ if tpa = [] then Continue;
434377 tpa := transformer.Process(tpa).Merge();
378+ if tpa = [] then Continue;
435379
436- if tpa = [] then
437- begin
438- found += False;
439- Continue;
440- end;
441-
442- found += True;
380+ found[i] := True;
443381 atpa += tpa;
444382 tpa := [];
445383 end;
@@ -450,49 +388,20 @@ end;
450388function TColorFinder.FindEx(out atpa: T2DPointArray; out found: TBooleanArray; areas: TPolygonArray): Boolean; overload;
451389var
452390 transformer: TColorFinderTransformer;
453- area: TPolygon;
454- bounds: TBox;
455391 i: Integer;
456- tpa, primary, secondary : TPointArray;
392+ tpa: TPointArray;
457393begin
458394 transformer := Self.Transformer.Normalize();
459395
460- for area in areas do
396+ SetLength(found, Length(areas));
397+ for i := 0 to High(areas) do
461398 begin
462- bounds := area.Bounds();
463- for i := 0 to High(Self.Colors) do // Find TColorFinder.Colors
464- tpa += Target.FindColor(Self.Colors[i], bounds);
465-
466- for i := 0 to High(Self.ColorClusters) do // Find TColorFinder.ColorClusters
467- begin
468- if Self.ColorClusters[i].Distance <= 0 then
469- begin
470- WriteLn GetDebugLn('TColorFinder', 'Skipping cluster with 0 distance, make it higher than 0!', ELogLevel.WARN);
471- Continue;
472- end;
473-
474- primary := Target.FindColor(Self.ColorClusters[i].Primary, bounds);
475- if primary = [] then Continue;
476- secondary := Target.FindColor(Self.ColorClusters[i].Secondary, bounds);
477- if secondary = [] then Continue;
478-
479- tpa += primary.PointsNearby(secondary, 0, MainScreen.NormalizeDistance(Self.ColorClusters[i].Distance));
480- end;
481-
482- if tpa = [] then
483- begin
484- found += False;
485- Continue;
486- end;
487-
488- tpa := transformer.Process(tpa.ExtractPolygon(area)).Merge();
489- if tpa = [] then
490- begin
491- found += False;
492- Continue;
493- end;
399+ tpa := Self._FindHelper(areas[i].Bounds());
400+ if tpa = [] then Continue;
401+ tpa := transformer.Process(tpa.ExtractPolygon(areas[i])).Merge();
402+ if tpa = [] then Continue;
494403
495- found + = True;
404+ found[i] : = True;
496405 atpa += tpa;
497406 tpa := [];
498407 end;
@@ -503,54 +412,25 @@ end;
503412function TColorFinder.FindEx(out atpa: T2DPointArray; out found: TBooleanArray; areas: TCuboidArray): Boolean; overload;
504413var
505414 transformer: TColorFinderTransformer;
506- area: TCuboid;
507- bounds: TBox;
508415 i: Integer;
509- tpa, primary, secondary, tmp : TPointArray;
416+ tpa: TPointArray;
510417begin
511418 transformer := Self.Transformer.Normalize();
512419
513- for area in areas do
420+ SetLength(found, Length(areas));
421+ for i := 0 to High(areas) do
514422 begin
515- bounds := area.Bounds();
516- for i := 0 to High(Self.Colors) do // Find TColorFinder.Colors
517- tmp += Target.FindColor(Self.Colors[i], bounds);
518-
519- for i := 0 to High(Self.ColorClusters) do // Find TColorFinder.ColorClusters
520- begin
521- if Self.ColorClusters[i].Distance <= 0 then
522- begin
523- WriteLn GetDebugLn('TColorFinder', 'Skipping cluster with 0 distance, make it higher than 0!', ELogLevel.WARN);
524- Continue;
525- end;
526-
527- primary := Target.FindColor(Self.ColorClusters[i].Primary, bounds);
528- if primary = [] then Continue;
529- secondary := Target.FindColor(Self.ColorClusters[i].Secondary, bounds);
530- if secondary = [] then Continue;
531-
532- tmp += primary.PointsNearby(secondary, 0, MainScreen.NormalizeDistance(Self.ColorClusters[i].Distance));
533- end;
534-
535- if tmp = [] then
536- begin
537- found += False;
538- Continue;
539- end;
423+ tpa := Self._FindHelper(areas[i].Bounds());
540424
541- tmp := transformer.Process(tmp.ExtractPolygon(area.Polygon())).Merge();
542- if tmp = [] then
543- begin
544- found += False;
545- Continue;
546- end;
425+ if tpa = [] then Continue;
426+ tpa := transformer.Process(tpa.ExtractPolygon(areas[i].Polygon())).Merge();
427+ if tpa = [] then Continue;
547428
548- found + = True;
549- atpa += tmp ;
550- tmp := [];
429+ found[i] : = True;
430+ atpa += tpa ;
431+ tpa := [];
551432 end;
552433
553- atpa := transformer.Process(tpa);
554434 Result := atpa <> [];
555435end;
556436
0 commit comments