Combinaison de deux couleurs (AlphaBlend)

Contenu du snippet

function AlphaBlendProcess(const ColorA, ColorB: integer; const Alpha: single = 1 {1= 100%, 0.5= 50%}): integer;
var NAlpha : single;
    CA : array[0..3] of byte Absolute ColorA;
    CB : array[0..3] of byte Absolute ColorB;
    CR : array[0..3] of byte Absolute result;
begin
  NAlpha := 1 - alpha;
  CR[0]  := byte( round( (CA[0]*Alpha) + (CB[0]*NAlpha) ) );
  CR[1]  := byte( round( (CA[1]*Alpha) + (CB[1]*NAlpha) ) );
  CR[2]  := byte( round( (CA[2]*Alpha) + (CB[2]*NAlpha) ) );
  CR[3]  := 0;
end;
{ exemple :
  C := AlphaBlendProcess($54749C, $FFFFCC, 0.5); { C = $00AABAB4 }
}


{ version avec precalcul (+ performances / gain =~ 50%) }
type
  TAlphaTable = array[byte, byte] of byte;

procedure PreCalcAlphaTable(var AlphaTable: TAlphaTable; const Alpha: single= 1 {1= 100%, 0.5= 50%});
var A,B: integer;
    NAlpha: single;
begin
  NAlpha := 1 - Alpha;
  for A := 0 to 255 do
    for B := 0 to 255 do
      AlphaTable[A,B] := byte( round( (A*Alpha) + (B*NAlpha) ) );
end;

function AlphaBlendProcess(const ColorA, ColorB: integer; const AlphaTable: TAlphaTable): integer;
var
  CA : array[0..3] of byte Absolute ColorA;
  CB : array[0..3] of byte Absolute ColorB;
  CR : array[0..3] of byte Absolute result;
begin
  CR[0]  := AlphaTable[CA[0], CB[0]];
  CR[1]  := AlphaTable[CA[1], CB[1]];
  CR[2]  := AlphaTable[CA[2], CB[2]];
  CR[3]  := 0;
end;
{ exemple :
  PreCalcAlphaTable(AlphaTable, 0.5);
  for Y := 0 to MaxY do
    for X := 0 to MaxX do
      ColorPixelR[X,Y] := AlphaBlendProcess(ColorPixelA[X,Y], ColorPixelB[X,Y], AlphaTable); 
}


Compatibilité : Delphi 5

Disponible dans d'autres langages :

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.