# GetPixel tolerance

• 12-06-2007
JordanCason
GetPixel tolerance
I need a GetPixel tolerance code. Here’s is what I have for searching for a pixel.

Code:

```int findcolor(int &x1, int &y1, int &x2, int &y2, unsigned int r, unsigned int g, unsigned int b, int &x, int &y) { int XL, YL, xcount, ycount, TLXBackup;           TLXBackup = x1;           XL = x2 - x1;           YL = y2 - y1;           xcount = 0;           ycount = 0;               POINT TR;               HDC hdc = GetDC(NULL);               Loop:                   DWORD color2 = GetPixel(hdc, x1, y1);                   unsigned int R = GetRValue(color2);                   unsigned int G = GetGValue(color2);                   unsigned int B = GetBValue(color2);                       if ( xcount == XL)                         {                           y1++;                           x1 = TLXBackup;                           ycount++;                           xcount = 0;                         }                         if ( ycount == YL )                           {                             ReleaseDC;                             return 0;                           }                           xcount++;                           x1++;                               if ((r == R) && (g == G) && (b == B))                               {                                 x = x1;                                 y = y1;                                 ReleaseDC;                                 return 1;                               }                               goto Loop; }```
i have ben searching google and still am but not finding mutch.
• 12-06-2007
kcpilot
Unless you comment your code, no one is going to make much of an effort to figure out what you're trying to do. Add some comments that show the logic of what you are trying to accomplish. Then you may get more assistance. And above all, what is a GetPixel tolerance code??
• 12-06-2007
Elysia
Why in the abyss are you using goto for such a simple loop!? Change it, please. It looks really bad.
• 12-06-2007
JordanCason
Ok well pixel tolerance would work like.
If tolerance = 10 than find a pixel with that color that was specified or 10 shads different from that color.
• 12-06-2007
JordanCason
yes ill be changing the goto in a little bit but for now it works
• 12-06-2007
Elysia
You shouldn't be using goto in the first place! You should not have written it at all for such a simple matter of using a loop here, where it is not complicated at all.
• 12-06-2007
iMalc
Get rid of that goto. a do..while(true) will work just the same.

This all depends on what you're using it for. I recently(ish) wrote something similiar, except that it converted RGB to the HSV colourspace before doing any tolerance testing. This improved the results a little. What are you using it for?

Btw "much" is spelt like this, and "been" has two E's.
Being not so good at spelling is fine, but I would hope that you would not purposefuly turn down assistance.
• 12-06-2007
JordanCason
Just searching in x1, y1 and x2, y2 for a pixel with a tolerance. so if you don’t mind I think that your code might help if you could post it or send it to me.
• 12-06-2007
iMalc
I can't I'm afraid, that code is at work and belongs to my employer.

btw I understand the description of the problem, but what do you intend to use it for? What do you plan to do once you find the pixel you are looking for?
If you were wanting to find the pixel that is the closest match to the specified colour then you would need to keep going until you reach the end, or find a perfect match.