CharMz
08-14-2009, 08:42 PM
type
PRGBQuadArray = ^TRGBQuadArray;
TRGBQuadArray = array [0..0] of TRGBQuad;
function FindColorTolerance(Client : HDC; TargetColor : TRGBQuad; var dx, dy : Integer; x1, y1, x2, y2, Tolerance : Integer) : Boolean;
const
Offset : array [0..0] of Integer = (1);
var
bmpW, bmpH : Integer;
bmi : TBitmapInfo;
DIBHandle : HBitmap;
CompDC : HDC;
PrevObj : HGDIOBJ;
Pixels : PRGBQuadArray;
i, l , HW : Integer;
j : Integer;
begin
Result := False;
bmpW := x2 - x1 + 1;
bmpH := y2 - y1 + 1;
FillMemory(@bmi, SizeOf(TBitmapInfo), 0);
with bmi.bmiHeader do
begin
biSize := SizeOf(TBitmapInfoHeader);
biWidth := bmpW;
biHeight := -bmpH;
biPlanes := 1;
biBitCount := 32;
biCompression := BI_RGB;
end;
try
DIBHandle := CreateDIBSection(0, bmi, DIB_RGB_COLORS, Pointer(Pixels), 0, 0);
CompDC := CreateCompatibleDC(Client);
PrevObj := SelectObject(CompDC, DIBHandle);
BitBlt(CompDC, 0, 0, bmpW, bmpH, Client, x1, y1, SRCCOPY);
l := High(Offset);
HW := ( bmpH - 1 ) * ( bmpW - 1 );
for j := 0 to l do
begin
if Result then
Break;
i := 0 - OffSet[j];
while ( i < ( HW ) - OffSet[j] ) and ( not Result ) do
begin
Inc(i, Length(OffSet));
if Sqr(Tolerance) >= (Sqr(TargetColor.rgbRed - Pixels^[i].rgbRed)
+ Sqr(TargetColor.rgbGreen - Pixels^[i].rgbGreen)
+ Sqr(TargetColor.rgbBlue - Pixels^[i].rgbBlue)) then
begin
dx := x1 + ( i mod bmpW );
dy := y1 + ( i div bmpW );
Result := True;
end;
end;
end;
finally
PrevObj := SelectObject(CompDC, PrevObj);
DeleteDC(CompDC);
DeleteObject(DIBHandle);
end;
end;
if you can make this faster, i will bake you a cake
PRGBQuadArray = ^TRGBQuadArray;
TRGBQuadArray = array [0..0] of TRGBQuad;
function FindColorTolerance(Client : HDC; TargetColor : TRGBQuad; var dx, dy : Integer; x1, y1, x2, y2, Tolerance : Integer) : Boolean;
const
Offset : array [0..0] of Integer = (1);
var
bmpW, bmpH : Integer;
bmi : TBitmapInfo;
DIBHandle : HBitmap;
CompDC : HDC;
PrevObj : HGDIOBJ;
Pixels : PRGBQuadArray;
i, l , HW : Integer;
j : Integer;
begin
Result := False;
bmpW := x2 - x1 + 1;
bmpH := y2 - y1 + 1;
FillMemory(@bmi, SizeOf(TBitmapInfo), 0);
with bmi.bmiHeader do
begin
biSize := SizeOf(TBitmapInfoHeader);
biWidth := bmpW;
biHeight := -bmpH;
biPlanes := 1;
biBitCount := 32;
biCompression := BI_RGB;
end;
try
DIBHandle := CreateDIBSection(0, bmi, DIB_RGB_COLORS, Pointer(Pixels), 0, 0);
CompDC := CreateCompatibleDC(Client);
PrevObj := SelectObject(CompDC, DIBHandle);
BitBlt(CompDC, 0, 0, bmpW, bmpH, Client, x1, y1, SRCCOPY);
l := High(Offset);
HW := ( bmpH - 1 ) * ( bmpW - 1 );
for j := 0 to l do
begin
if Result then
Break;
i := 0 - OffSet[j];
while ( i < ( HW ) - OffSet[j] ) and ( not Result ) do
begin
Inc(i, Length(OffSet));
if Sqr(Tolerance) >= (Sqr(TargetColor.rgbRed - Pixels^[i].rgbRed)
+ Sqr(TargetColor.rgbGreen - Pixels^[i].rgbGreen)
+ Sqr(TargetColor.rgbBlue - Pixels^[i].rgbBlue)) then
begin
dx := x1 + ( i mod bmpW );
dy := y1 + ( i div bmpW );
Result := True;
end;
end;
end;
finally
PrevObj := SelectObject(CompDC, PrevObj);
DeleteDC(CompDC);
DeleteObject(DIBHandle);
end;
end;
if you can make this faster, i will bake you a cake