{"id":155,"date":"2007-05-12T16:19:25","date_gmt":"2007-05-12T14:19:25","guid":{"rendered":" http:\/\/www.nsonic.de\/blog\/2007\/05\/bitmap-alpha-blending\/"},"modified":"2007-05-12T16:19:27","modified_gmt":"2007-05-12T14:19:27","slug":"bitmap-alpha-blending","status":"publish","type":"post","link":"https:\/\/www.nsonic.de\/blog\/2007\/05\/bitmap-alpha-blending\/","title":{"rendered":"Bitmap: Alpha blending"},"content":{"rendered":"<p>Here&#8217;s the code.<\/p>\n<p>This is particularly neat to use if you want to place text on a bitmap and<br \/>\nguarantee that the text is readable without completely obscuring the image<br \/>\nunderneath; simply tint the area underneath the text with clBlack, then draw<br \/>\nclWhite text on top (with Canvas.Brush.Style = bsClear).<\/p>\n<p>Note that the TintBitmapRect procedure below requires that you&#8217;re using<br \/>\npf32Bit bitmaps.  It can be modified to work with other pixel formats, but<br \/>\nthat is an exercise for the reader.<br \/>\n<!--more--><br \/>\n[code lang=&#8221;delphi&#8221;]{<br \/>\n  TColors have color components in blue-green-red order.<br \/>\n  32-bit bitmap pixels have color components in red-green-blue order.<br \/>\n  This function allows conversion between the two orders.<br \/>\n}<br \/>\nfunction SwapRedBlue(const Color: TColor): TColor;<\/p>\n<p>begin<br \/>\n  Result := (Color and $FF0000) shr 16 or<br \/>\n            (Color and $00FF00) or<br \/>\n            (Color and $0000FF) shl 16;<br \/>\nend;<\/p>\n<p>{<br \/>\n  Tint an arbitrary rectangular area of a bitmap with an arbitrary color.<\/p>\n<p>  Author: Gary Williams, gw (at) gw (dot) ro<br \/>\n}<br \/>\nprocedure TintBitmapRect(const Bitmap: TBitmap;<br \/>\n                         const Rect: TRect;<br \/>\n                         const Color: TColor);<\/p>\n<p>var<br \/>\n  Pixel: PLongWord;<\/p>\n<p>  I: Integer;<br \/>\n  J: Integer;<\/p>\n<p>  Color2: LongWord;<\/p>\n<p>const<br \/>\n  Mask: LongWord = $00FEFEFE;<\/p>\n<p>begin<br \/>\n  Assert(Bitmap.PixelFormat = pf32Bit);<\/p>\n<p>  Color2 := SwapRedBlue(Color) and Mask;<\/p>\n<p>  for I := Rect.Top to (Rect.Bottom &#8211; 1) do<br \/>\n    begin<br \/>\n    Pixel := Bitmap.ScanLine[I];<br \/>\n    Inc(Pixel, Rect.Left);<\/p>\n<p>    for J := Rect.Left to (Rect.Right &#8211; 1) do<br \/>\n      begin<br \/>\n      Pixel^ := ((Pixel^ and Mask) + Color2) shr 1;<br \/>\n      Inc(Pixel);<br \/>\n      end;<br \/>\n    end;<br \/>\nend;<\/p>\n<p>procedure TForm1.Button1Click(Sender: TObject);<\/p>\n<p>begin<br \/>\n  with Image1.Picture do<br \/>\n    begin<br \/>\n    Bitmap.PixelFormat := pf32Bit;<\/p>\n<p>    TintBitmapRect(Bitmap,<br \/>\n                   Rect(Bitmap.Width div 4,<br \/>\n                        Bitmap.Height div 4,<br \/>\n                        Bitmap.Width &#8211; Bitmap.Width div 4,<br \/>\n                        Bitmap.Height &#8211; Bitmap.Height div 4),<br \/>\n                   clRed);<br \/>\n    end;<br \/>\nend;<br \/>\n[\/code]<\/p>\n<p>[tags]Delphi, Graphic[\/tags]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s the code. This is particularly neat to use if you want to place text on a bitmap and guarantee that the text&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[11],"tags":[75,101],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p71Tml-2v","_links":{"self":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/155"}],"collection":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/comments?post=155"}],"version-history":[{"count":0,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/posts\/155\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/media?parent=155"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/categories?post=155"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nsonic.de\/blog\/wp-json\/wp\/v2\/tags?post=155"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}