## How do I get the RGB values from P[x]?

### How do I get the RGB values from P[x]?

Graphics newbie...

From the Delphi Help and this ng I was able to get 99% of my task
done, except, I need to compare the color of a pixel with the color
I'm looking for.

From Delphi 7 help:

for x := 0 to BitMap.Width -1 do
P[x] := y;

Now, I don't want to assign anything, my code looks like this:

MyColor: TColor
...
MyColor := clNavy;
...
for x := 0 to BitMap.Width -1 do
if P[x] := clNavy then // <= this is the line I need help with
begin
ShowMessage('Foundit');
Exit;
end;

P is a PByteArray, whatever that is. clNavy is a TColor ( or
Integer);

like... RGB(P[x]) = RGB(MyColor).... is this correct? I don't think
so.

Am I supposed to be using some kind of RGB() function to compare the
colors... one contained as a PByteArray element and the other as an
integer?

Thanks in advance.

### How do I get the RGB values from P[x]?

Hi red,
maybe the following is helpful...
CompareR:= yourcolor and \$000000ff; //Strip out separate RGB values
CompareG := (yourcolor shr 8) and \$000000ff;
CompareB := (yourcolor shr 16) and \$000000ff;

pointcolor:=p[x];

PointR:= pointcolor and \$000000ff; //Strip out separate RGB values
PointG := (pointcolor shr 8) and \$000000ff;
PointB := (pointcolor shr 16) and \$000000ff;

...
something like that:

if (compareR=pointR) and (compareG=pointG) and (compareB=pointB) then
begin
// do something
end;

best regards
Michael

Red schrieb:

### How do I get the RGB values from P[x]?

Thanks Michael,

I just didn't know if the GetRValue(p[x]) would work like it does on a
"real" color, like GetRValue(MyColor). If so, your code will work...
so, I was wondering.. will this work: if RGB(p[x]) = RGB(MyColor)
then...

or do I have to split it up into red value, green value and blue
value...

of course then I could just recombine them using RGB() anyway...

Comments?

### How do I get the RGB values from P[x]?

It depends on the pixelformat of your bitmap.

If your bitmap is pf24bit, the Scanline[] property of TBitmap points to
memory that contains each time 3 bytes with a color value.

If your bitmap is pf8bit, the value is just an index into the bitmap's color
palette, one byte per color. Obviously, since a byte can only hold 256
different values, there can only be a max of 256 different colors in the
bitmap (or 256 greyscales).

There are also some other pixelformats, but they aren't used that often
anymore. One exception is pf32bit, for that format there's a very good
library called "Graphics32".

Try to check out EFG's graphics and bitmap pages for more information and
examples, e.g:
http://www.yqcomputer.com/

Hope that helps,

Nils Haeck
www.simdesign.nl

### How do I get the RGB values from P[x]?

Hi Nils, thanks for the help, but I'm still getting it wrong. All I
can "see" is red. This is such a simple task, that all the help on
the Web and in this ng is for more advanced problems. No seems to be
able to answer my question which I think even the most newbiest of
newbies learns how to overcome in their first graphics lesson, but
I've had no lessons. Here's my problem I've titled....

"Seeing RED"

I iterate through a list of "area" names, and I look for pixels of a
certain color in a given image. Here is the calling function:

FindAreaRgn( img.Picture.Bitmap, area, r, g, b, pxl_lst)

The procedure below, looks for the given color in the image. The
problem is that the procedure always thinks that every pixel in the
image is red. The only area that gets "found" is the one that happens
to be red also, so none of the other areas get recorded as being
present in the image.

The r2, g2, b2 I'm feeding the FindAreaRgn are correct. They change
according to the color I'm looking for. It's the lines:

r := GetRValue(P[x]);
g := GetGValue(P[x]);
b := GetBValue(P[x]);

which always report every pixel in the multi-colored image as being:

r (255)
g (0)
b (0)

I can see that the image is not all red.

What am I doing wrong? I'm using something incorrectly here.

procedure TfrmPxlMpr.FindAreaRgn(bmp: TBitMap; area: String; r2, g2,
b2: Integer; pxl_lst: TStringList);
var
x, y: Integer;
r, g, b: Integer;
P : PByteArray;
begin
if not bmp.PixelFormat = pf24bit then
bmp.PixelFormat := pf24bit;

for y := 0 to bmp.Height -1 do
begin
P := bmp.ScanLine[y];

for x := 0 to bmp.Width -1 do
begin
r := GetRValue(P[x]);
g := GetGValue(P[x]);
b := GetBValue(P[x]);

if RGB(r, g, b) = RGB(r2, g2, b2) then
pxl_lst.Add(IntToStr(x) + ',' + IntToStr(y) + '=' + area);
end;
end;
end;

### How do I get the RGB values from P[x]?

You shouldn't use a PByteArray in this case, but something like

TRgbColor = packed record
R: byte;
G: byte;
B: byte;
end;
TRgbArray = array[0..0] of TRgbColor;
PRgbArray = ^TRgbArray;

Perhaps you have to swap R and B above (not sure, but I think they're the
other way around in Windows bitmaps).

Then, in your program, change to:

You can also work with the PByteArray but then realise that each P[x] just
points to *one* byte, not a 3-byte sequence.

Kind regards,

Nils Haeck
www.simdesign.nl