I made a few changes to the code @Andreik provided!
#define AUT_MANUALSTEP 0
#define AUT_AUTOSTEP 1
bool PixelSearch(LPRECT lpRect, int nCol, int nVar, int nStep, LPPOINT pPointResult, int stepMode = AUT_MANUALSTEP, int nCoordPixelMode = AUT_COORDMODE_SCREEN) {
int q, r;
int col;
BYTE red, green, blue;
BYTE red_low, red_high, green_low, green_high, blue_low, blue_high;
HDC hdc;
RECT relrect;
POINT ptOrigin;
bool m_bColorModeBGR = false;
relrect.left = lpRect->left;
relrect.top = lpRect->top;
relrect.right = lpRect->right;
relrect.bottom = lpRect->bottom;
ConvertCoords(nCoordPixelMode, ptOrigin);
relrect.left += ptOrigin.x;
relrect.top += ptOrigin.y;
relrect.right += ptOrigin.x;
relrect.bottom += ptOrigin.y;
col = nCol;
if (m_bColorModeBGR == false)
Util_RGBtoBGR(col);
red = GetRValue(col);
green = GetGValue(col);
blue = GetBValue(col);
if (nVar < 0)
nVar = 0;
else if (nVar > 0xff)
nVar = 0xff;
if (nVar == 0)
{
red_low = red_high = red;
green_low = green_high = green;
blue_low = blue_high = blue;
}
else
{
red_low = (nVar > red) ? 0 : red - nVar;
green_low = (nVar > green) ? 0 : green - nVar;
blue_low = (nVar > blue) ? 0 : blue - nVar;
red_high = (nVar > 0xFF - red) ? 0xFF : red + nVar;
green_high = (nVar > 0xFF - green) ? 0xFF : green + nVar;
blue_high = (nVar > 0xFF - blue) ? 0xFF : blue + nVar;
}
hdc = GetDC(NULL);
if (stepMode == AUT_MANUALSTEP) {
for (q = relrect.left; q <= relrect.right; q = q + nStep)
{
for (r = relrect.top; r <= relrect.bottom; r = r + nStep)
{
col = GetPixel(hdc, q, r);
red = GetRValue(col);
green = GetGValue(col);
blue = GetBValue(col);
if (red >= red_low && red <= red_high && green >= green_low && green <= green_high
&& blue >= blue_low && blue <= blue_high)
{
q -= ptOrigin.x;
r -= ptOrigin.y;
pPointResult->x = q;
pPointResult->y = r;
ReleaseDC(NULL, hdc);
return 0;
}
}
}
}
else if (stepMode == AUT_AUTOSTEP) {
nStep = lpRect->right - lpRect->left;
for (int i = nStep; i > 0; i -= (i / 2)) {
for (q = relrect.left; q <= relrect.right; q = q + i)
{
for (r = relrect.top; r <= relrect.bottom; r = r + i)
{
col = GetPixel(hdc, q, r);
red = GetRValue(col);
green = GetGValue(col);
blue = GetBValue(col);
if (red >= red_low && red <= red_high && green >= green_low && green <= green_high
&& blue >= blue_low && blue <= blue_high)
{
q -= ptOrigin.x;
r -= ptOrigin.y;
pPointResult->x = q;
pPointResult->y = r;
ReleaseDC(NULL, hdc);
return 0;
}
}
}
}
}
ReleaseDC(NULL, hdc);
return 1;
}
Changed the return type of the PixelSearch function to return a bool. If it finds the color it returns 0, if not it returns 1. I also added a parameter stepMode, its default is AUT_MANUALSTEP or 0. The AUT_AUTOSTEP basically just takes the difference of lpRect->right - lpRect->left and uses that as the step amount. It divides the difference by 2 each time it cycles through those for loops. If anyone can add to this or make this even better that would be awesome!