Mega Code Archive

 
Categories / Delphi / Examples
 

Blending 2 colours

How to blend 2 TColor's together with a specified strength (Opacity). function BlendColors(Color1, Color2: TColor; Opacity: Byte): TColor; var I : Integer; Val1, Val2 : Byte; RGB1, RGB2 : PByteArray; begin //First make sure they are RGB rather than a windows colour Color1 := ColorToRGB(Color1); Color2 := ColorToRGB(Color2); //Point our Byte arrays to the 2 colours RGB1 := @Color1; RGB2 := @Color2; //Blend the B,G,R for I:=0 to 2 do begin Val1 := RGB1[I] * (255 - Opacity) div 255; Val2 := RGB2[I] * Opacity div 255; RGB1[I] := Val1 + Val2; end; //Set the colour flag to specify $01 = actual RGB colour RGB1[3] := 1; Result := Color1; end