Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi TColor - Zusammenfügen der Farben (https://www.delphipraxis.net/160486-tcolor-zusammenfuegen-der-farben.html)

Sir Rufo 16. Mai 2011 17:55

AW: TColor - Zusammenfügen der Farben
 
Liste der Anhänge anzeigen (Anzahl: 2)
Sodele, da habe ich doch mal einen kleinen süßen Record zusammengebaut, der nun einen Farbwert mit einem Alphakanal beherbergt.
Benutzt werden kann der wie TColor (durch überladene Operatoren) hat aber noch eine Zusatzfunktion (PaintColor), um etwas mit dem Alpha-Wert anzufangen.

Eine Demo ist im Anhang.
Anhang 34216
(Exe, kompletter Source)

Delphi-Quellcode:
unit insARGBColor;

interface

uses
  Graphics;

type
  TARGBColor = record
    R, G, B, A : Byte;
    class operator implicit( ARGB : TARGBColor ) : TColor;
    class operator implicit( Color : TColor ) : TARGBColor;
    function PaintColor( Color : TColor ) : TColor;
  end;

implementation

{ TARGBColor }

class operator TARGBColor.implicit( ARGB : TARGBColor ) : TColor;
begin
  Result := ARGB.R or ( ARGB.G shl 8 ) or ( ARGB.B shl 16 );
end;

class operator TARGBColor.implicit( Color : TColor ) : TARGBColor;
begin
  Result.R := Byte( Color );
  Result.G := Byte( Color shr 8 );
  Result.B := Byte( Color shr 16 );
end;

function TARGBColor.PaintColor( Color : TColor ) : TColor;
var
  Col, res : TARGBColor;
begin
  Col   := Color;
  res.R := ( Self.R * Self.A + Col.R * ( 255 - Self.A ) ) div 255;
  res.G := ( Self.G * Self.A + Col.G * ( 255 - Self.A ) ) div 255;
  res.B := ( Self.B * Self.A + Col.B * ( 255 - Self.A ) ) div 255;
  Result := res;
end;

end.

Satty67 16. Mai 2011 18:14

AW: TColor - Zusammenfügen der Farben
 
Ok, das sieht gut aus. Aber weil ich auch gerade nebenher gebastelt hab (natürlich zeitlich total versagt ;)) poste ich auch mein Ergebnis, dass Deinen angebotenen Funktionsrumpf als Vorlage hatte:
Delphi-Quellcode:
type
  TARGB = packed record
    A,R,G,B : Byte;
  end;

function MixColor(ARGB : TARGB; Color : TColor): TColor;
var
  R, G, B : Byte;
begin
  R := Color and $FF;
  G := (Color shr 8) and $FF;
  B := (Color shr 16) and $FF;
  R := ((ARGB.R * ARGB.A) + (R * ($FF - ARGB.A))) div $FF;
  G := ((ARGB.G * ARGB.A) + (G * ($FF - ARGB.A))) div $FF;
  B := ((ARGB.B * ARGB.A) + (B * ($FF - ARGB.A))) div $FF;
  Result := R + (G shl 8) + (B shl 16);
end;
Zumindest bei der Berechnung/Mixen scheint es richtig (war da garnicht so sicher)


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:22 Uhr.
Seite 2 von 2     12   

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