Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Grafik / Sound / Multimedia (https://www.delphipraxis.net/21-library-grafik-sound-multimedia/)
-   -   Delphi RGB -> TColor -> RGB (https://www.delphipraxis.net/4067-rgb-tcolor-rgb.html)

sakura 13. Apr 2003 21:43


RGB -> TColor -> RGB
 
Wird ja oft genug nach gefragt ;-) Und nicht alle brauchen gleich die HTML Lösung, also, hier ist die einfache Variante.

Delphi-Quellcode:
function RGB2TColor(const R, G, B: Byte): Integer;
begin
  // convert hexa-decimal values to RGB
  Result := R + G shl 8 + B shl 16;
end;


procedure TColor2RGB(const Color: TColor; var R, G, B: Byte);
begin
  // convert hexa-decimal values to RGB
  R := Color and $FF;
  G := (Color shr 8) and $FF;
  B := (Color shr 16) and $FF;
end;
...:cat:...

CalganX 27. Aug 2006 09:31

Re: RGB -> TColor -> RGB
 
himitsu hat noch folgende Ergänzungen:

ColorToRGB ist zwar in Delphi enthalten, allerdings wurde es dort so definiert, was IMHO nicht korreckt ist,
Delphi-Quellcode:
function ColorToRGB(Color: TColor): Longint;
begin
  if Color < 0 then
    Result := GetSysColor(Color and $000000FF) else
    Result := Color;
end;
Mögliche Farbpaletten:
Delphi-Quellcode:
const
  cpSystemPalette = $00;
  cpActingPalette = $01;
  cpLogicalPalette = $02;
  cpNoColor       = $1F;
  cpDefaultColor  = $20;
  cpSystemColor   = $FF;
Dieses if Color < 0 then würde zwar auf cpSystemColor ansprechen, aber vorallem bei cpNoColor und cpDefaultColor würde es falsche Werte liefen, denn dieses sind erstens keine Farbwerte und zweitens sind dort zwar die RGB-Bytes vordefiniert, werden aber nicht als Farbe interpretiert.
Ebenso werden "falsche" Farbpaletten nicht ausgewertet.

Als Beispiel die entsprechenden Farbkonstanten:
Delphi-Quellcode:
const
  clNone   = TColor(cpNoColor      shl 24 or $FFFFFF);
  clDefault = TColor(cpDefaultColor shl 24 or $000000);
Weißt einfach mal clNone irgendwo zu ... laut ColorToRGB und sakuras TColor2RGB müßte da Weiß zu sehen sein, ist es aber bestimmt nicht. :zwinker:

Demnach sähe es laut Definition dann in etwa so aus:
Delphi-Quellcode:
function ColorToRGB(Color: TColor): TColor;
begin
  if Color shr 24 = $FF then Result := GetSysColor(Color and $FF) and $FFFFFF
  else if Color shr 24 in [$00..$02] then Result := Color and $FFFFFF
  else Result := $000000;
end;

Welches man dann auch noch etwa so zusammenfassen könnte: :mrgreen:
Delphi-Quellcode:
function RGB2TColor(R, G, B: Byte): Integer;
begin
  // convert hexa-decimal values to RGB
  Result := R or (G shl 8) or (B shl 16); // ich mochte das OR halt viel lieber
end;

procedure TColor2RGB(Color: TColor; var R, G, B: Byte);
begin
  // convert hexa-decimal values to RGB
  if Color shr 24 = $FF then
    Color := GetSysColor(Color and $FF)
  else if Color shr 24 > $02 then
    Color := 0;
  R := Color;
  G := (Color shr 8);
  B := (Color shr 16);
end;

{procedure TColor2RGB(Color: TColor; var R, G, B: Byte);
begin
  // convert hexa-decimal values to RGB
  if Color shr 24 = $FF then begin
    Color := GetSysColor(Color and $FF);
    R := Color;
    G := (Color shr 8);
    B := (Color shr 16);
  end else if Color shr 24 in [$00..$02] then begin
    R := Color;
    G := (Color shr 8);
    B := (Color shr 16);
  end else begin
    R := 0;
    G := 0;
    B := 0;
  end;
end;}
[edit=fkerber]Link gefixt. Mfg, fkerber[/edit]


Alle Zeitangaben in WEZ +1. Es ist jetzt 17:16 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz