From beb307f24b1999a4cd90a4537b22167cd3a1e114 Mon Sep 17 00:00:00 2001 From: Raymond Date: Sun, 20 Sep 2009 02:19:44 +0000 Subject: [PATCH] Added FindColorsTolerance. git-svn-id: http://www.villavu.com/repositories/merlijn/mufasa@73 3f818213-9676-44b0-a9b4-5e4c4e03d09d --- Units/MMLAddon/PSInc/Wrappers/colour.inc | 5 ++ Units/MMLAddon/PSInc/pscompile.inc | 1 + Units/MMLCore/finder.pas | 84 +++++++++++++++++++++++- 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/Units/MMLAddon/PSInc/Wrappers/colour.inc b/Units/MMLAddon/PSInc/Wrappers/colour.inc index 8d19853..23ae0ec 100644 --- a/Units/MMLAddon/PSInc/Wrappers/colour.inc +++ b/Units/MMLAddon/PSInc/Wrappers/colour.inc @@ -32,3 +32,8 @@ function CountColorTolerance(Color, xs, ys, xe, ye, Tolerance: Integer): Integer begin; result := CurrThread.Client.MFinder.CountColorTolerance(color,xs,ys,xe,ye,tolerance); end; + +function FindColorsTolerance(var Points: TPointArray; Color, xs, ys, xe, ye, Tolerance: Integer): Boolean; +begin; + result := CurrThread.Client.MFinder.FindColorsTolerance(points,color,xs,ys,xe,ye,tolerance); +end; diff --git a/Units/MMLAddon/PSInc/pscompile.inc b/Units/MMLAddon/PSInc/pscompile.inc index d63d2ee..cdce253 100644 --- a/Units/MMLAddon/PSInc/pscompile.inc +++ b/Units/MMLAddon/PSInc/pscompile.inc @@ -20,6 +20,7 @@ Sender.AddFunction(@FindColorTolerance, 'function findcolortolerance(var x, y: i Sender.AddFunction(@FindColors, 'function findcolors(var TPA: TPointArray; color, x1, y1, x2, y2: integer): boolean;'); Sender.AddFunction(@SimilarColors,'function SimilarColors(Col1,Col2,Tolerance : integer) : boolean'); Sender.AddFunction(@CountColorTolerance,'function CountColorTolerance(Color, xs, ys, xe, ye, Tolerance: Integer): Integer;'); +Sender.AddFunction(@FindColorsTolerance,'function FindColorsTolerance(var Points: TPointArray; Color, xs, ys, xe, ye, Tolerance: Integer): Boolean;'); Sender.AddFunction(@MoveMouse, 'procedure MoveMouse(x, y: integer);'); Sender.AddFunction(@GetMousePos, 'procedure GetMousePos(var x, y: integer);'); diff --git a/Units/MMLCore/finder.pas b/Units/MMLCore/finder.pas index ff3c046..f137fde 100644 --- a/Units/MMLCore/finder.pas +++ b/Units/MMLCore/finder.pas @@ -30,7 +30,7 @@ type // Possibly turn x, y into a TPoint var. function FindColor(var x, y: Integer; Color, x1, y1, x2, y2: Integer): Boolean; function FindColorTolerance(var x, y: Integer; Color, x1, y1, x2, y2, tol: Integer): Boolean; - + function FindColorsTolerance(var Points: TPointArray; Color, xs, ys, xe, ye, Tol: Integer): Boolean; function FindColors(var TPA: TPointArray; Color, x1, y1, x2, y2: Integer): Boolean; protected Client: TObject; @@ -313,6 +313,85 @@ begin TClient(Client).MWindow.FreeReturnData; end; +function TMFinder.FindColorsTolerance(var Points: TPointArray; Color, xs, ys, + xe, ye, Tol: Integer): Boolean; +var + PtrData: TRetData; + Ptr: PRGB32; + PtrInc,C: Integer; + dX, dY, clR, clG, clB, xx, yy: Integer; + H1, S1, L1, H2, S2, L2: Extended; +begin + DefaultOperations(xs,ys,xe,ye); + + dX := xe - xs; + dY := ye - ys; + //next, convert the color to r,g,b + ColorToRGB(Color, clR, clG, clB); + ColorToHSL(Color, H1, S1, L1); + + PtrData := TClient(Client).MWindow.ReturnData(xs, ys, dX + 1, dY + 1); + + // Do we want to "cache" these vars? + // We will, for now. Easier to type. + Ptr := PtrData.Ptr; + PtrInc := PtrData.IncPtrWith; + c := 0; + case CTS of + 0: + for yy := ys to ye do + begin + for xx := xs to xe do + begin + if ((abs(clR-Ptr^.R) <= Tol) and (abs(clG-Ptr^.G) <= Tol) and (Abs(clG-Ptr^.B) <= Tol)) then + begin; + ClientTPA[c].x := xx; + ClientTPA[c].y := yy; + inc(c); + end; + inc(Ptr); + end; + Inc(Ptr, PtrInc); + end; + + 1: + for yy := ys to ye do + begin + for xx := xs to xe do + begin + if (Sqrt(sqr(clR-Ptr^.R) + sqr(clG - Ptr^.G) + sqr(clB - Ptr^.B)) <= Tol) then + begin; + ClientTPA[c].x := xx; + ClientTPA[c].y := yy; + inc(c); + end; + inc(ptr); + end; + Inc(Ptr, PtrInc); + end; + 2: + begin + for yy := ys to ye do + for xx := xs to xe do + begin + RGBToHSL(Ptr^.R,Ptr^.G,Ptr^.B,H2,S2,L2); + if ((abs(H1 - H2) <= (hueMod * tol)) and (abs(S1 - S2) <= (satMod * tol)) and (abs(L1 - L2) <= Tol)) then + begin; + ClientTPA[c].x := xx; + ClientTPA[c].y := yy; + inc(c); + end; + inc(Ptr); + end; + Inc(Ptr, PtrInc); + end; + end; + SetLength(Points, C); + Move(ClientTPA[0], Points[0], C * SizeOf(TPoint)); + Result := C > 0; + TClient(Client).MWindow.FreeReturnData; +end; + function TMFinder.FindColors(var TPA: TPointArray; Color, x1, y1, x2, y2: Integer): Boolean; var PtrData: TRetData; @@ -341,7 +420,8 @@ begin begin; if (Ptr^.R = clR) and (Ptr^.G = clG) and (Ptr^.B = clB) then begin - Self.ClientTPA[I] := Point(xx, yy); + Self.ClientTPA[I].x := xx; + Self.ClientTPA[i].y := yy; Inc(I); end; Inc(Ptr);