Color Detection

Hello!

I'm trying to build a small program that tells me the RGB values of the pixels on which my cursor is currently located at.

So far I've made this, however, I'm not sure how to continue:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <string>
using namespace std;

int main() {
  
	POINT p;
	int x;
	int y;
	string red, green, blue;

	while(0==0){
	GetCursorPos(&p);
	x = p.x;
	y = p.y;

	
	//[insert color detector here]
	cout >> red >> green >> blue;
   
	}
	_getch();
}



I've already gotten some help from General Programming:
(http://www.cplusplus.com/forum/general/76052/)

Athar (4003) Jul 26, 2012 at 6:07pm
This belongs into Windows programming.
See: http://msdn.microsoft.com/en-us/library/windows/desktop/dd144871%28v=vs.85%29.aspx
and: http://msdn.microsoft.com/en-us/library/windows/desktop/dd144909%28v=vs.85%29.aspx


But I'm unsure how to use them.

Any ideas?
Thanks!
Last edited on
The response from Athar is accurate. Why were you unable to follow it? Basically you use GetPixel() to obtain the color. But in order to call it, you need a device context (HDC). To get a DC you use GetDC().

Which part is proving difficult?
Haha.

Somehow your explanation just lit my bulb!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;

int main() {
  
	POINT p;
	int x;
	int y;


	while(0==0){
	GetCursorPos(&p);
	x = p.x;
	y = p.y;
	HDC hDC;

	hDC = GetDC(NULL);
	
	cout << GetPixel(hDC, x, y) << endl;

	}
}


Works incredibly!

Thanks you two!
Note that every call to GetDC should have a matching call to ReleaseDC:

1
2
3
4
5
	hDC = GetDC(NULL);
	
	cout << GetPixel(hDC, x, y) << endl;

	ReleaseDC(NULL, hDC);  // <- don't forget this 
Yeah, was just about to fix that. :)
Topic archived. No new replies allowed.