Monir-SCS wrote:
So the following code :

var Red, Green, Blue:integer;
begin
  Red:=GetRValue( ColorToRGB(clBtnFace));
  Green:=GetGValue( ColorToRGB(clBtnFace));
  Blue:=GetBValue( ColorToRGB(clBtnFace));

Is equivalent to or much better than the following code, right?

It is not equivalent since the code above works and the code below doesn't.

type
    TRGB = record
        R: Integer;
        G: Integer;
        B: Integer;
end;

You already have a type like that in the form of the TRGBTriple record.

function RGBToColor(PR,PG,PB: Integer): TColor;
begin
    Result := TColor((PB * 65536) + (PG * 256) + PR);
end;

The RGB function in Windows.pas declares its parameters as Byte values, not Integers. The reason is that values larger than 255 are not valid. With your function, the following both return the same color:


RGBToColor(256, 0, 0);
RGBToColor(0, 1, 0);

Declare those parameters as Byte to avoid invalid values in the function.

function ColorToRGB(PColor: TColor): TRGB;
var
    i: Integer;
begin
    i := PColor;
    Result.R := 0;
    Result.G := 0;
    Result.B := 0;
    while i - 65536 >= 0 do begin i := i -
                             65536; Result.B := Result.B + 1; end;
    while i - 256 >= 0 do begin i := i -
                             256; Result.G := Result.G + 1; end;
    Result.R := i;
end;

That function will fail if you pass in a system color, such as clBtnFace. All the system colors will appear as very dark shades of red.


That function is also a very slow way of getting what you need.

Result.R := PColor and $ff;
Result.G := (PColor shr 8) and $ff;
Result.B := (PColor shr 16) and $ff;

This code does in three operations what your code might take *hundreds* of loop iterations to find.

function RGBToCol(PRGB: TRGB): TColor;
begin
    Result := RGBToColor(PRGB.R,PRGB.G,PRGB.B);
end;

Now, you can write;
var RGB:TRGB ;

begin
RGB:=ColorToRGB(clBtnFace);

You haven't really tested that code, have you?

--
Rob

__________________________________________________
Delphi-Talk mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi-talk

Reply via email to