Mega Code Archive

 
Categories / Delphi / Examples
 

Transparent Fill

Title: Transparent Fill Question: This example show how to make a transparent fill. The routine is greatly inspired by a transparent fill eample by Yosmany Sez Concepcin but optimized by use of scanlines. Answer: ABitmap : The bitmap to draw the transparent fill on ARect : The rectangle to fill AColor : The color of the fill AAlpha : Alphavalue (transparency) 0 = Full transparent, 100 = No transparency Please note that the bitmap is converted to 24bit colors. The alphavalues are calculated and stored in AlphaTable. The table is then used to find the correct blend value for each color of the original image. uses graphics, windows, extctrls, classes, sysutils, controls; const PIXELCOUNTMAX = 32768; type pRGBArray = ^TRGBArray; TRGBArray = array[0..PIXELCOUNTMAX] of TRGBTriple; procedure FillTransparency( ABitmap : graphics.TBitmap; ARect : TRect; AColor : TColor; AAlpha : integer ); var i,x,y : integer; Scan : pRGBArray; Alpha : real; r,g,b : byte; AlphaTable : array[1..2,0..255] of integer; begin ABitmap.PixelFormat := pf24bit; r := fColor and $000000FF; g := (fColor and $0000FF00) shr 8; b := (fColor and $00FF0000) shr 16; Alpha := AAlpha / 100; for i := 0 to 255 do begin AlphaTable[1,i] := Trunc( i*Alpha ); AlphaTable[2,i] := Trunc( i*(1-Alpha) ); end; for y := ARect.Top to ARect.Bottom do begin Scan := pRGBArray( ABitmap.ScanLine[y] ); for x := ARect.Left to ARect.Right do begin Scan[x].rgbtBlue := AlphaTable[1, b] + AlphaTable[2, Scan[x].rgbtBlue]; Scan[x].rgbtGreen := AlphaTable[1, g] + AlphaTable[2, Scan[x].rgbtGreen]; Scan[x].rgbtRed := AlphaTable[1, r] + AlphaTable[2, Scan[x].rgbtRed]; end; end; end;