program alphablend;
uses
FPImage, FPImgCanv, FPCanvas, FTFont, FPWritePNG;
var
img: TFPMemoryImage;
canvas: TFPImageCanvas;
writer: TFPWriterPNG;
font: TFreeTypeFont;
begin
// initialize free type font manager
FTFont.InitEngine;
{$IFNDEF MSWINDOWS}
FontMgr.SearchPath:='/usr/share/fonts/truetype/ttf-dejavu/'; // not needed on Windows
{$ENDIF}
font:=TFreeTypeFont.Create;
img := TFPMemoryImage.Create(200, 200);
canvas := TFPImageCanvas.Create(img);
try
canvas.DrawingMode := dmAlphaBlend; // This activates the alpha-blend mode
// Background
canvas.Pen.Style := psClear;
canvas.Brush.FPColor := colTransparent;
canvas.Clear;
// Yellow opaque rectangle
canvas.Brush.FPColor := FPColor($FFFF, $FFFF, 0);
canvas.Rectangle(10, 10, 190, 100);
// Overlapping semi-transparent red circle
canvas.Brush.FPColor := FPColor($FFFF, 0, 0, $4000);
canvas.Ellipse(60, 60, 140, 140);
// Overlapping semi-transparent blue circle
canvas.Brush.FPColor := FPColor(0, 0, $FFFF, $8000);
canvas.Ellipse(0, 90, 100, 190);
// Paint text
canvas.Font := font;
canvas.Font.Name := 'DejaVuSans';
canvas.Font.Size := 64;
canvas.Font.FPColor := FPColor($0FFF, $0FFF, $0FFF, $6000);
canvas.TextOut(5, 140, 'Test');
// Write to file
writer := TFPWriterPNG.Create;
try
writer.UseAlpha := true;
img.SaveToFile('alphablend.png', writer);
finally
writer.Free;
end;
finally
canvas.Free;
img.Free;
font.Free;
end;
end.