I play this game called GunBound, and have a nice C++ script for it.
But I need a function that can help me do the following:

int x=390;
int y=450;
for(int i = 0; i == 0; x++)
{
if(scanpixel(x, y) == YellowColor) i++;
}

I need to get the x coordinates of the pixel with the yellow color. I know the location of y will be the same every time for the color, so I just need to find the x location. I also know the x location of the pixel will be after 390, and before 780. I need a fast way of doing it too, because I need my program to be fast. So if you know of a faster way to do this, that would be great to know. :icon_wink: I also have no idea how to get the yellow color part either. :icon_rolleyes:

Thanks For Any Help,
Ryan :icon_cheesygrin:

So what's wrong with remembering where you last drew the pixel, rather than trying to find it again sometime later.

So what's wrong with remembering where you last drew the pixel, rather than trying to find it again sometime later.

The pixel changes. It's a slider that the user slides, and I need to find it's location. xD

Once of the biggest problems you are going to face... is how yellow is yellow? From this site alone I pulled up 7 different values of "yellow". So how yellow is your slider bar's yellow.... is it pure 255, 255, 0? I've got some basic code that will retrieve the pixel color under the mouse, which needs to modified to include a for loop for your x coordinate, a modified number for the y coordinate, and a pretty good value for yellow. You could certainly make it scan for a range of "yellow values" say between 170 to 255 red, 170 to 255 green and 0 to like 100 blue, but with ranges like that you start to significantly increase your possibility of false positives.

> It's a slider that the user slides
But this doesn't happen by magic.

Somewhere along the line, the mouse/key press has to be interpreted by some software into drawing the slider in the new position. Chances are, your software was involved.

If this is some kind of GUI tool, then for sure it will have a method called "getPosition" which you can use to find out what the user did, without you having to go grubbing around in the pixels.

Once of the biggest problems you are going to face... is how yellow is yellow? From this site alone I pulled up 7 different values of "yellow". So how yellow is your slider bar's yellow.... is it pure 255, 255, 0? I've got some basic code that will retrieve the pixel color under the mouse, which needs to modified to include a for loop for your x coordinate, a modified number for the y coordinate, and a pretty good value for yellow. You could certainly make it scan for a range of "yellow values" say between 170 to 255 red, 170 to 255 green and 0 to like 100 blue, but with ranges like that you start to significantly increase your possibility of false positives.

The only other colors it would see would be dark brown and red. So I wouldn't worry about false positives. ;P
The color is exactly RGB 214, 186, 099. I took a screen shot, pasted it in paint, zoomed in to where pixel was, got a program that tells you the RGB color of the pixel where your mouse is, and moved my mouse over the pixel to get the color. What's the next step? :)

> It's a slider that the user slides
But this doesn't happen by magic.

Somewhere along the line, the mouse/key press has to be interpreted by some software into drawing the slider in the new position. Chances are, your software was involved.

If this is some kind of GUI tool, then for sure it will have a method called "getPosition" which you can use to find out what the user did, without you having to go grubbing around in the pixels.

Good point! I'll try making something like that right now. :)

You might need to modify the constants a little, because the color you gave me isn't yellow at all.... it seems more like diarrhea orange (sorry for that). Anyway, this code works for me on windows XP Sp2, Code::Blocks IDE wth Mingw. It is overly commented for your sake, and a little untidy... but I'm sure you can handle it.

#include <iostream>
#include <cstdio>
#include <windows.h>

using namespace std;

int main(int argc, char **argv)
{
    // Device Context, Point Struct, and Color Struct
    HDC hDC = GetDC(HWND_DESKTOP);
    POINT pt;
    COLORREF rgb;

    // Numbers To Compare Against
    const int CMP_R = 214;
    const int CMP_G = 186;
    const int CMP_B = 99;

    // Values As byte
    BYTE rVal;
    BYTE gVal;
    BYTE bVal;

    // Same Values As Integers
    int red;
    int green;
    int blue;

    // Set This To Your Known Y Coord.
    // Pretty Sure 0,0 Is Top Left
    // Corner Of The Monitor.
    pt.y = 100;

    // Loop To 3000... Adjust For Higher
    // Resolution (Really?)
    for (int i=0; i < 3000; i++) {

        pt.x = i;   // Next Pixel

        // Moving From The Left To The Right,
        // Get The Pixel
        rgb = GetPixel(hDC, pt.x, pt.y);

        // Save Pixel Color In Byte Values
        // This Step Might Not Be Necessary..
        // Coult Probably Get It As An Int
        rVal = GetRValue(rgb);
        gVal = GetGValue(rgb);
        bVal = GetBValue(rgb);

        // Save Pixel Color As Decimal
        red = (int)rVal;
        green = (int)gVal;
        blue = (int)bVal;

        // Do All The Colors Match Our Control 
        // Constants?
        if (red == CMP_R && green == CMP_G && blue == CMP_B) {
            cout << "Found At: " << endl;
            cout << "X: " << pt.x << " Y: " << pt.y << endl;
        }
    }

    return 0;
}

You might need to modify the constants a little, because the color you gave me isn't yellow at all.... it seems more like diarrhea orange (sorry for that). Anyway, this code works for me on windows XP Sp2, Code::Blocks IDE wth Mingw. It is overly commented for your sake, and a little untidy... but I'm sure you can handle it.

#include <iostream>
#include <cstdio>
#include <windows.h>

using namespace std;

int main(int argc, char **argv)
{
    // Device Context, Point Struct, and Color Struct
    HDC hDC = GetDC(HWND_DESKTOP);
    POINT pt;
    COLORREF rgb;

    // Numbers To Compare Against
    const int CMP_R = 214;
    const int CMP_G = 186;
    const int CMP_B = 99;

    // Values As byte
    BYTE rVal;
    BYTE gVal;
    BYTE bVal;

    // Same Values As Integers
    int red;
    int green;
    int blue;

    // Set This To Your Known Y Coord.
    // Pretty Sure 0,0 Is Top Left
    // Corner Of The Monitor.
    pt.y = 100;

    // Loop To 3000... Adjust For Higher
    // Resolution (Really?)
    for (int i=0; i < 3000; i++) {

        pt.x = i;   // Next Pixel

        // Moving From The Left To The Right,
        // Get The Pixel
        rgb = GetPixel(hDC, pt.x, pt.y);

        // Save Pixel Color In Byte Values
        // This Step Might Not Be Necessary..
        // Coult Probably Get It As An Int
        rVal = GetRValue(rgb);
        gVal = GetGValue(rgb);
        bVal = GetBValue(rgb);

        // Save Pixel Color As Decimal
        red = (int)rVal;
        green = (int)gVal;
        blue = (int)bVal;

        // Do All The Colors Match Our Control 
        // Constants?
        if (red == CMP_R && green == CMP_G && blue == CMP_B) {
            cout << "Found At: " << endl;
            cout << "X: " << pt.x << " Y: " << pt.y << endl;
        }
    }

    return 0;
}

Now that you mention it, it is kind of orange. xD
Thanks so much Comatose! This is more than I asked for. :icon_cheesygrin: You rock mate! I'll post again later and tell you if it all works in my program.(I'm sure I can get it to work.) :icon_wink:

Now that you mention it, it is kind of orange. xD
Thanks so much Comatose! This is more than I asked for. :icon_cheesygrin: You rock mate! I'll post again later and tell you if it all works in my program.(I'm sure I can get it to work.) :icon_wink:

Bad news! GetDC returns 0 when I use it with GunBound's handle.(I also tried it's child windows and other windows that might of worked.) I also tried GetWindowDC, but it returned NULL too. I guess GunBound blocks us from using it, any suggestions?

Don't use GunBound's handle. What I've found is that as long as the window is active on the screen HWND_DESKTOP will work for sub-windows too. In fact, to test it (the code as posted), I opened ms paint, drew a box and colored it with the paint fill thing. Then I moved the window so that it was at YPos 100, and run the app. It found it at the appropriate position. Have you tried it just as it is?

I have attempted to compile this code and get a:

C:\Learn2c_plusplus\pixel_checker\pixel_attempt_1.o:pixel_attempt_1.cc:(.text+0x185)||undefined reference to `_GetPixel@12'|
||=== Build finished: 1 errors, 0 warnings ===|
Error.

I am using Code::Blocks with Mingw. I am super newb with c++, and really need some help. Not sure what to do with this error.

Thank you much.

-Uri

This article has been dead for over six months. Start a new discussion instead.