2004. június 5., szombat

How to read the color of a pixel


Problem/Question/Abstract:

I need to determine if certain portions of my canvas are black. While this seemed easy enough at first, I'm at a roadblock trying to determine pixel color. As I usually do, I wrote a little test program and painted a certain area black. I tried the following code to read a pixel in that area:

if Canvas.Pixels[444, 399] = clBlack then
  MessageBeep(mb_ok);

I never get the beep. What am I doing wrong, and what would be the correct way to do this.

Answer:

Are you shure you hit the right pixel ? Try


canvas.pixels[444, 399] = clBlack
if canvas.pixels[444, 399] = clBlack then
  MessageBeep(mb_ok);


Windows converts colors (at least if you are using less than 24 bit screen mode). But black should exists on all palettes, I think. The color value of a pixel will be a DWORD (32 bit), encoded like this:

$XXRRGGBB

where XX are return values (as far as I remember, it even tells you , if it had to convert a color here). So you could try something like

(color and $FFFFFF) = clBlack

to get the return value out or

= $000000

to be sure it not depends on clBlack .

If that doesn't work, let it write the pixel color to screen an compare to value of clBlack. If worst case , get R,G and B and check if (near to ) 0.

Nincsenek megjegyzések:

Megjegyzés küldése